在我的游戏中,我放置了不断移动的平台和一个玩家。玩家向上跳跃通过每个平台,但平台从左到右移动,当玩家坐在移动平台上时,它也会随着平台滑动。
但是问题是当平台与屏幕的左边缘碰撞并开始向右移动时,我的播放器会不断地向左侧移动,而不是与平台停止并掉落。如何停止播放器的后移动?这就像当我们打破汽车时,我们跳过了玻璃。:(
在Init
我创建播放器的方法中。CCSprite *_player
在头文件中声明。
// 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
是添加平台的 CCNode,因此它们可以在玩家向上移动时向下滚动以产生玩家向上移动的效果。
//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...
方法被实现来放置一个精灵和物理体。请参阅编写的方法的代码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;
}
为了使平台从左到右连续移动,实现以下逻辑。
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]];
有人能帮我一下吗?