不断向上和向下移动精灵Cocos2d 3.0

时间:2014-02-13 01:26:34

标签: cocos2d-iphone sprite

我试图不停地上下移动我的精灵

这是我的代码,但应用程序到达此时仍然崩溃

// Add a sprite
_player = [CCSprite spriteWithImageNamed:@"player.png"];
_player.position  = ccp(self.contentSize.width/30,self.contentSize.height/2);
_player.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _player.contentSize} cornerRadius:0]; // 1
_player.physicsBody.collisionGroup = @"playerGroup"; //
CCActionProgressFromTo *upanddown = [CCActionProgressFromTo actionWithDuration:1.0 from:.8f to:.1f];
[_player runAction:[CCActionRepeatForever actionWithAction:upanddown]];
[_physicsWorld addChild:_player];

感谢您的任何建议:D

1 个答案:

答案 0 :(得分:3)

如果您尝试上下移动,请尝试:

[CCActionMoveBy actionWithDuration:1.0f position:ccp(moveX, moveY)];

或者

[CCActionMoveTo actionWithDuration:1.0f position:ccp(moveX, moveY)];

Move-by会将其移动偏移量,而move-to会将其移动到特定位置。例如,使用您可以执行的移动偏移来上下移动它:

CCActionMoveBy* moveUp = [CCActionMoveBy actionWithDuration:1.0f position:ccp(0.0f, 100.0f)];
CCActionMoveBy* moveDown = [CCActionMoveBy actionWithDuration:1.0f position:ccp(0.0f, -100.0f)];
CCActionSequence* upAndDown = [CCActionSequence actions:moveUp, moveDown, nil];

[_player runAction:[CCActionRepeatForever actionWithAction:upAndDown]];