iphoneios7cocos2d-iphoneccspritecocos2d-iphone-3

CCSprite (Player) falls off from Platform while moving left side of the device


In my game, I have placed continuously moving platforms and a player. Player goes up jumping through each platform but platforms are moving from left to right and when player sits on moving platform, it slides with platform as well.

But the problem is when platform collides with left edge of screen and starts going right side, my player continuously moves toward left side, not stopping with platform and falls off. How can I stop the after movement of player? It's like when we break off the car we jump through glass. :(

In Init method I am creating player. CCSprite *_player is declared in header file.

// Creates player
_player = [CCSprite spriteWithImageNamed: @"Assets.atlas/Player.png"];
[_player setPosition: ccp(160.0f, 160.0f)];

_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1
_player.physicsBody.collisionGroup = @"playerGroup"; // 2
_player.physicsBody.collisionType = @"player";
//[_physicsWorld addChild:_player];
[_foreground addChild: _player];

_foreground is the CCNode in which platforms are added so they can be scrolled down while player moves up to give effect of player is moving upwards.

//This is how platform node is added in init method.
PlatformNode *platform3 = (PlatformNode *)[PlatformNode node];
[platform3 createPlatformAtPosition:CGPointMake(110, 50) ofType: PLATFORM_NORMAL];
[_foreground addChild: platform3];

createPlatform... method is implemented to place a sprite and physics body. See the code for the method which is written in PlatformNode.m.

- (void) createPlatformAtPosition:(CGPoint) position ofType:(PlatformType) type {
    //PlatformNode *node = [PlatformNode node];

    if (type == PLATFORM_BREAK) {
        _sprite = [CCSprite spriteWithImageNamed: @"PlatformBreak.png"];
    } else {
        _sprite = [CCSprite spriteWithImageNamed: @"Platform.png"];
    }
    [_sprite setPosition: ccp(_sprite.contentSize.width/2, _sprite.contentSize.height/2)];
    [self addChild: _sprite];

    [self setPosition: position];
    [self setName: @"NODE_PLATFORM"];
    [self setPlatformType: type];

    self.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _sprite.contentSize} cornerRadius: 0.0];
    self.physicsBody.collisionGroup = @"platform";
    self.physicsBody.type = CCPhysicsBodyTypeStatic;
    self.physicsBody.collisionType = @"obstacle";

//    if (type == PLATFORM_MOVE) {
//    // Animate projectile
//    CCActionMoveBy* actionMove = [CCActionMoveBy actionWithDuration:4.0f position: CGPointMake(-100, 0)];
//    CCActionReverse *actionRev = [CCActionReverse actionWithAction: actionMove];
//    CCActionSequence *seq = [CCActionSequence actionWithArray: @[actionMove, actionRev]];
//    [self runAction:[CCActionRepeatForever actionWithAction: seq]];
//    }

    //return self;
}

To make the platform move continuously from left to right, following logic is implemented.

CGPoint actPoint = platform3.position; CGPoint extPoint = ccp([CCDirector sharedDirector].viewSize.width - platform3.contentSize.width*2, platform3.position.y); CGPoint zrPoint = ccp(0, platform3.position.y);

    //CCActionMoveBy* actionMove = [CCActionMoveTo actionWithDuration:((ccpSub(actPoint, zrPoint).x * 8.0) / extPoint.x) position: CGPointMake(0, platform3.position.y)];
    CCActionMoveBy* actionMoveF = [CCActionMoveTo actionWithDuration:8.0f position: extPoint];
CCActionMoveBy* actionMoveR = [CCActionMoveTo actionWithDuration:8.0f position: zrPoint];
    CCActionSequence *seq = [CCActionSequence actionWithArray: @[actionMoveF, actionMoveR]];
//CCActionReverse *actR = [CCActionReverse actionWithAction: seq];
//CCActionSequence *act = [CCActionSequence actionWithArray: @[seq, actR]];
    [platform3 runAction:[CCActionRepeatForever actionWithAction: seq]];

Can anyone help me here?


Solution

  • I solved the issue. Taken idea from the link here: Cocos2d 3.0 + Chipmunk + CCAnimation: moving physics body with animated object. How?

    I have taken a temporary variable to hold ground value on which player stands. The code is written in collision detection method.

    -(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair player:(CCSprite *)player obstacle:(CCCustomSprite *)obstacle {
        // Taken the ground as temporary variable when player collides with the obstacle
        _ground = obstacle;
        return YES;
    }
    

    Now in touchBegan method

    -(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        // Remove the reference from the ground as the player jumps up from the ground, needs to release the ground first in order to avoid its velocity
        _ground = nil;
    
        // Making a jump
        [_player.physicsBody applyImpulse: ccp(0, _player.physicsBody.mass * JUMP_IMPULSE)];
    }
    

    Now apply velocity same as ground's velocity but make sure you change only x value as the platform moves horizontally only.

    - (void) update:(CFTimeInterval)currentTime {
      if (_ground) {
            CGPoint veloc = _ground.physicsBody.velocity;
            _player.physicsBody.velocity = ccp(veloc.x, _player.physicsBody.velocity.y);
        }
    }
    

    Working fine for me. However another issue is I am unable to increase speed of jump. e.g. my player jumps 100 points in 2 seconds, I want to make it in 0.5 seconds, can I make it?