CCSprite not responding to Collision

103 Views Asked by At

I am a newbie to Spritebuilder and trying to make MARIO Game prototype. What i am doing is I am having 2 sprites, one sprite has fast walking character and other has normally walking character. I am changing the sprite through a button. When i click on the button the normally walking sprite is replaced by fast walking sprite and again clicking back on that button make the normal sprite appear.

Problem :

  1. For the very first time when the game starts, collision is detected between ground and character on jumping. But as soon as i change the sprite to fast walking character, no collision is detected on jumping. When i again switch back to normal walking character, then also no collision is detected on jumping.

  2. When i make the sprite jump and it starts falling down, to ground some sort of activity (i would prefer saying bouncing) is observed which i also can't say whether it is bouncing back or not. I dont know why it is coming and how to rectify it. I searched stack overflow and some forums. What i found was to use

CCPhysicsBody.iterations

But i dont know how to use it. For more detailed explanation you can see the video at this link to undersatnd the full problem. https://www.dropbox.com/s/0fz3qehie76r60k/Game.mp4?dl=0

Please help. Any suggestions are welcomed.

Edit : Here is the code where i am changing the sprites:

-(void)setCharacterAnimationFast
{
    if (_turboButton.selected)
    {
        //changing button state;
        _turboButton.selected=NO;

        //changing value of background speed from screen;
        speeedParameter = 50.0;
        speedParameterOfBackgroundObjects=10.0;

        //loading new chracter
        _character =(RightCharacter*)[CCBReader load:@"RightCharacter"];

        //position the laser at helicopter origin
        CGPoint _characterPosition = _fastCharacter.position;

        //transform the world position to the node space to which the laser will be added (_physicsNode)
        _character.position = [_physicsNode convertToNodeSpace:_characterPosition];

        //defining zorder for moving it to background
        _character.zOrder= _fastCharacter.zOrder;

        // add the laser to the physicsNode of this scene (because it has physics enabled)
        [_physicsNode addChild:_character];

        //removing the existing character
        [_fastCharacter removeFromParentAndCleanup:YES];



        CCLOG(@"ground : %@", _ground);
        CCLOG(@"ground : %@", _ground.physicsBody);
        CCLOG(@"ground : %@", _ground.physicsNode);



        _ground.physicsBody.collisionMask=@[@"character"];
        _character.physicsBody.collisionMask=@[@"ground"];
        _ground.physicsBody.collisionType=@"ground";
        _ground.physicsBody.elasticity=0;
        _character.physicsBody.collisionType=@"character";
        _character.physicsBody.elasticity=0;
        _physicsNode.iterations=1000;

        _fastCharacter=nil;
    }
    else
    {
        //changing button state
        _turboButton.selected=YES;

        //changing value of background speed from screen;
        speeedParameter = 80.0;
        speedParameterOfBackgroundObjects= 20.0;

        //loading new fast character
        _fastCharacter =(RightCharacterFast*)[CCBReader load:@"RightCharacterFast"];

        //position the laser at helicopter origin
        CGPoint _fastCharacterPosition = _character.position;

        //transform the world position to the node space to which the laser will be added (_physicsNode)
        _fastCharacter.position = [_physicsNode convertToNodeSpace:_fastCharacterPosition];

        //defining zorder for moving it to background
        _fastCharacter.zOrder= _character.zOrder;

        // add the laser to the physicsNode of this scene (because it has physics enabled)
        [_physicsNode addChild:_fastCharacter];

        //removing the existing character
        [_character removeFromParentAndCleanup:YES];



        CCLOG(@"ground : %@", _ground);
        CCLOG(@"ground : %@", _ground.physicsBody);
        CCLOG(@"ground : %@", _ground.physicsNode);


        _ground.physicsBody.collisionMask=@[@"fastCharacter"];
        _fastCharacter.physicsBody.collisionMask=@[@"ground"];
        _ground.physicsBody.elasticity=0;
        _ground.physicsBody.collisionType=@"ground";
        _fastCharacter.physicsBody.collisionType=@"fastCharacter";
        _fastCharacter.physicsBody.elasticity=0;
        _physicsNode.iterations=1000;

        _character=nil;
    }
}

And here is the link to the video of debug draw: https://www.dropbox.com/s/vt557ngpyj6z9g3/DebugDraw.mp4?dl=0

1

There are 1 best solutions below

0
On

Use the same proxy body node for collisions, its very efficient using less intensive nodes for collision:

characterNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];
fastWalkingCharacterNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width/2];