在Cocos2D v3.x iOS中运行其他操作时,CCSprite动画停止

时间:2014-09-23 17:35:34

标签: ios objective-c animation cocos2d-iphone ccaction

我在游戏中制作精灵(通过精灵表等)。这是一个简单的动画,我想在游戏的整个生命中发生这种动画。精灵也是一个物理身体。 当触摸屏幕时,我希望精灵执行一些操作。

我添加了动画和动作,一切都很好。但问题是,当精灵执行其他操作(触摸时)时动画停止。

以下是init方法中的精灵代码:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"plane1.plist"];

NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i)
{
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"planee%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithSpriteFrames:walkAnimFrames delay:0.1]; //Speed in which the frames will go at

//Adding png to sprite
plane = [CCSprite spriteWithImageNamed:@"plane1.png"];

//Positioning the sprite
plane.position  = ccp(self.contentSize.width/2,self.contentSize.height/2);

//Repeating the sprite animation
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim];
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];

//Animation continuously repeating
[plane runAction:repeatingAnimation];

//Adding the Sprite to the Scene
CGRect aRectangle = (CGRect){ccp(0,-10), plane.contentSize};
CGRect smallerRectangle = CGRectInset(aRectangle, 30, 20);
plane.physicsBody = [CCPhysicsBody bodyWithRect:smallerRectangle  cornerRadius:0];
plane.physicsBody.collisionGroup = @"playerGroup";
plane.physicsBody.collisionType  = @"planeCollision";
[_physicsWorld addChild:plane];

这是我的touchBegan方法(触摸屏幕时执行其他操作:

/*The following code makes the Plane go up when touched anywhere on the screen and then starts downward movement*/

[plane stopAllActions];

float currentRotation = plane.rotation;

if (currentRotation==10) {
   [plane runAction:[CCActionSequence actions:
                      [CCActionRotateBy actionWithDuration:.1 angle:-30],
                      [CCActionMoveBy actionWithDuration:.1 position:ccp(0,30)],
                      [CCActionRotateBy actionWithDuration:.2 angle:30],
                      [CCActionCallBlock actionWithBlock:
                       ^{
                           [self startDownMovement];


                       }],nil]
     ];
}

else {
    [plane runAction:[CCActionSequence actions:
                      [CCActionMoveBy actionWithDuration:.1 position:ccp(0,30)],
                      [CCActionRotateTo actionWithDuration:.1 angle:10],
                      [CCActionCallBlock actionWithBlock:
                       ^{
                           [self startDownMovement];


                       }],nil]
     ];

}

以下是startDownMovement代码:

float currentRotation = plane.rotation;

if (currentRotation!=10) {

    [plane runAction:[CCActionSequence actions:[CCActionRotateBy actionWithDuration:0.1 angle:30],
                      [CCActionMoveBy actionWithDuration:1 position:ccp(0,-plane.position.y)],
                      nil]];

}

else {
    plane.rotation = 10;
    [plane runAction:[CCActionMoveBy actionWithDuration:1 position:ccp(0,-plane.position.y)]];
}

请帮帮我。当我点击屏幕使飞机起飞时,我不知道为什么我的动画会停止。

满怀期待!

0 个答案:

没有答案