문제

I just started using the SpriteBuilder with Cocos2d-v3 For the exercise of the SpriteBuilder, I practice in the tutorial of the following WebSite for the time being.

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

As a flow of the games of the tutorials,at first the catapult and the catapultArm are located on stage,the object(Penguin) which was attached to the arm flies forward when I pull the arm to the rear with a finger and separate a finger from a screen. When the object(Penguin) hits other objects(is called Seal),output log,"Something collided with a seal!"

In a "Implementing a delegate method" of tutorial, I was going to try to display a letter,"Something collided with a seal!" in Consoll using CCLOG method.

In the Gameplay.m, I implemented ccPhysicsCollisionPostSolve:seal:wildcard method that is called to dispaly a letter "Something collide with a seal!",but this method isn't called when Penguin object hits other objects(seal).

I certainly made Penguin.ccbi and Seal.ccbi in SpriteBuilder,and thier ccbi file is Physics body.

Why ccPhysicsCollisionPostSolve:seal:wildcard method isn't called ?

It's my code that I really implemented as follows

This is Gameplay.m file

#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
도움이 되었습니까?

해결책

Most likely you did not set a collision type for the Sealclass. Double check that you have:

  • Set the custom class for the Seal in SpriteBuilder to Seal
  • Have set the collisionType within the didLoadFromCCB method inside the Seal class to "seal"

That should fix you issue.

다른 팁

Also, in Spritebuilder, make sure the seals already loaded in the level (Level1.ccb) have Collision type set to seal.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top