ゲームでスプライトをアニメーション化しています (スプライト シートなどを使用)。これは、ゲームの生涯を通じて実現したい単純なアニメーションです。スプライトも物理体です。そして、画面がタッチされたときに、スプライトに何らかのアクションを実行してもらいたいです。
アニメーションとアクションを追加しましたが、すべて問題ありません。しかし問題は、スプライトが他のアクションを実行すると (タッチされたときに) アニメーションが停止することです。
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)]];
}
私を助けてください。画面をタップして飛行機を飛ばすと、アニメーションが停止する理由がわかりません。
期待に感謝!