1

刚开始使用 Cocos2d-v3 的 SpriteBuilder 对于 SpriteBuilder 的练习,我暂时在以下网站的教程中练习。

https://www.makegameswith.us/tutorials/getting-started-with-spritebuilder/collision-detection/

作为教程游戏的流程,首先弹射器和弹射器臂位于舞台上,当我用手指将手臂向后拉并分开手指时,附着在手臂上的物体(企鹅)向前飞从屏幕上。当物体(企鹅)撞到其他物体(称为海豹)时,输出日志,“Something collided with a seal!”

在教程的“实现委托方法”中,我打算尝试显示一个字母,“Something collided with a seal!” 在Consoll 中使用CCLOG 方法。

在 Gameplay.m 中,我实现了 ccPhysicsCollisionPostSolve:seal:wildcard 方法,该方法被调用来显示一个字母“Something collide with a seal!”,但是当 Penguin 对象撞击其他对象(seal)时不会调用此方法。

我当然在 SpriteBuilder 中制作了 Penguin.ccbi 和 Seal.ccbi,他们的 ccbi 文件是物理体。

为什么不调用 ccPhysicsCollisionPostSolve:seal:wildcard 方法?

这是我真正实现的代码如下

这是 Gameplay.m 文件

#import "Gameplay.h"


@implementation Gameplay{

    CCPhysicsNode *_physicsNode;

//To joint betweent catapult and catapultArm
    CCNode *_catapultArm;
    CCNode *_catapult;
    CCPhysicsJoint *_catapultJoint;

//Invisible Physics force
    CCNode *_pullbackNode;
    CCPhysicsJoint *_pullbackJoint;

//to move catapultArm
    CCNode *_mouseJointNode;
    CCPhysicsJoint *_mouseJoint;

//to fly penguin
    CCNode *_currentPenguin;
    CCPhysicsJoint *_penguinCatapultJoint;

//Object
    CCNode *_levelNode;

//To Prevent a 'retry' button from moving with a fly penguin
    CCNode *_contentNode;
}



//is called when CCB file has completed loading
-(void)didLoadFromCCB{


    _physicsNode.collisionDelegate = self;


    //tell this scene to accept touches
    self.userInteractionEnabled = TRUE;

    //loads the Levels/Leve1.ccb we have set up in SpriteBuilder
    CCScene *level = [CCBReader loadAsScene:@"Levels/Level1"];

    [_levelNode addChild:level];

    //visualize physics bodies & joints
    _physicsNode.debugDraw = TRUE;

    //catapultArm and catapult shall not collide
    [_catapultArm.physicsBody setCollisionGroup:_catapult];
    [_catapult.physicsBody setCollisionGroup:_catapult];

    //create a joint to connect the catapult arm with the catapult
    _catapultJoint = [CCPhysicsJoint connectedPivoJointWithBodyA:_catapultArm.physicsBody
                                                           bodyB:_catapult.physicsBody
                                                anchorA:_catapultArm.anchorPointInPoints];



    //nothing shall collide with our invisible nodes
    _pullbackNode.physicsBody.collisionMask = @[];
    //nothing shall collide with our invisible nodes
    _mouseJointNode.physicsBody.collisionMask = @[];


   _pullbackJoint = [CCPhysicsJointconnectedSpringJointWithBodyA:_pullbackNode.physicsBody
                                                           bodyB:_catapultArm.physicsBody
                                                         anchorA:ccp(0,0)
                                                         anchorB:ccp(34,138)
                                                      restLength:60.f
                                                       stiffness:500.f
                                                         damping:40.f
                    ];




}

//called on every touch in this scene (called every touch)
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

   CCLOG(@"touch Began");


   CGPoint touchLcation = [touch locationInNode:_contentNode];


   //start catapult dragging when a touch inside of the catapult arm occurs
  if(CGRectContainsPoint([_catapultArm boundingBox], touchLcation))
  {

   //move the mouseJointNode to the touch position
   _mouseJointNode.position = touchLcation;

   _mouseJoint = [CCPhysicsJoint connectedSpringJointWithBodyA:_mouseJointNode.physicsBody
                                                        bodyB:_catapultArm.physicsBody    
                                                      anchorA:ccp(0,0)
                                                      anchorB:ccp(34,138)
                                                   restLength:0.f
                                                    stiffness:3000.f
                                                      damping:150.f
                 ];



    //create a penguin from the ccbFile
    _currentPenguin = [CCBReader load:@"Penguin"];

    CGPoint penguinPosition = [_catapultArm convertToWorldSpace:ccp(34, 138)];

    _currentPenguin.position = [_physicsNode convertToNodeSpace:penguinPosition];

    //add it to the physics world
    [_physicsNode addChild:_currentPenguin];

    //we don't want the penguin to rotate in the scoop
    _currentPenguin.physicsBody.allowsRotation = FALSE;

    //create a joint to keep the penguin fixed to the scoop until the catapult is released
    _penguinCatapultJoint = 
    [CCPhysicsJoint connectedPivoJointWithBodyA:_currentPenguin.physicsBody
                                          bodyB:_catapultArm.physicsBody
                                        anchorA:_currentPenguin.anchorPointInPoints
    ];


   }


 }


-(void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event{

    CCLOG(@"MOVING!!!!!!!!!!");

    //whenever touches move,update the position of the mousejointNode to touch position
    CGPoint touchLocation = [touch locationInNode:_contentNode];

    _mouseJointNode.position = touchLocation;




}



-(void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{

//when touches end,meaning the user releases their finger,release the catapult
[self releaseCatapult];

}


-(void)touchCancelled:(UITouch *)touch withEvent:(UIEvent *)event{

[self releaseCatapult];

}


//destroy our joint and let the catapult snap when a touch ends
-(void)releaseCatapult{

 CCLOG(@"Release");


 if(_mouseJoint != nil)
 {



    //releases the joint and lets the catapult snap back
    [_mouseJoint invalidate];
    _mouseJoint = nil;


    //release the joint and lets the penguin fly
    [_penguinCatapultJoint invalidate];
    _penguinCatapultJoint = nil;

    //after snapping rotation is fine
    _currentPenguin.physicsBody.allowsRotation = TRUE;

    //follow the flying penguin
    CCActionFollow *follow = 

 }



}


-(void)retry
{

     [[CCDirector sharedDirector]replaceScene:[CCBReader loadAsScene:@"Gameplay"]];

}


-(void)ccPhysicsCollisionPostSolve:(CCPhysicsCollisionPair *)pair seal:(CCNode *)nodeA  
                                                              wildcard:(CCNode *)nodeB 
{
     CCLOG(@"Something collided with a seal!");
}



@end
4

2 回答 2

2

很可能您没有为Seal该类设置碰撞类型。仔细检查您是否拥有:

  • 将 SpriteBuilder 中的 Seal 的自定义类设置为Seal
  • 已将类collisionType内的didLoadFromCCB方法内设置Seal为“密封”

那应该可以解决您的问题。

于 2014-04-03T15:00:11.657 回答
1

此外,在 Spritebuilder 中,确保已在关卡 (Level1.ccb) 中加载的封印已Collision type设置为seal.

于 2016-02-10T20:24:23.053 回答