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?