0

我正在为我的游戏中的精灵制作动画(通过精灵表等)。这是一个简单的动画,我希望它贯穿游戏的整个生命周期。精灵也是一个物理体。当屏幕被触摸时,我希望精灵执行一些动作。

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

这是我在方法中的精灵代码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)]];
}

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

期待中的感谢!

4

0 回答 0