在我的游戏中,我放置了连续移动的平台和一个玩家。玩家在每个平台上跳跃,但平台从左向右移动,当玩家坐在移动的平台上时,平台也会随着平台滑动。
但问题是,当平台与屏幕左侧边缘碰撞并开始向右移动时,我的玩家不断向左侧移动,而没有与平台停止并掉落。如何停止玩家的后移动?就好像我们从车上摔下来时,会跳过玻璃:(
在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=平台3位置;CGPoint extPoint=ccp([CCCDirector sharedDirector].viewSize.width-平台3.contentSize.width*2,平台3.position.y);CGPoint zrPoint=ccp(0,平台3.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]];
有人能帮我吗?
我解决了这个问题。从这里的链接中获得的想法:Cocos2d 3.0+花栗鼠+CCA动画:用动画对象移动物理体。怎样
我取了一个临时变量来保持球员所站的地面值。该代码是用碰撞检测方法编写的。
-(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;
}
现在触摸开始方法
-(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)];
}
现在应用与地面速度相同的速度,但确保在平台仅水平移动时仅更改x值。
- (void) update:(CFTimeInterval)currentTime {
if (_ground) {
CGPoint veloc = _ground.physicsBody.velocity;
_player.physicsBody.velocity = ccp(veloc.x, _player.physicsBody.velocity.y);
}
}
工作对我来说很好。但另一个问题是我无法提高跳跃速度。例如,我的选手在2秒内跳了100分,我想在0.5秒内完成,我能完成吗?