我是 Cocos2D 的新手。我正在为 iPhone 制作一个简单的游戏,我希望我的 Sprite 随着一些动画消失。到目前为止,我可以使用以下代码让它消失:-
-(void)selectSpriteForTouch:(CGPoint)touchLocation
{
for (CCSprite *sprite in targets)
{
if (CGRectContainsPoint(sprite.boundingBox, touchLocation))
{
NSLog(@"sprite was touched");
[sprite.parent removeChild:sprite cleanup:YES];
[[SimpleAudioEngine sharedEngine] playEffect:@"pop.wav"];
[[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f];
}
}
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for( UITouch *touch in touches )
{
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
[self selectSpriteForTouch:location];
NSLog(@"touch was detected");
}
}
现在我想让 Sprite 消失并带有一些动画或任何效果。我该怎么做?
最佳答案
例如,这会使您的 Sprite 缩小直到消失,然后将其从其父级中移除:
-(void)selectSpriteForTouch:(CGPoint)touchLocation
...
if (CGRectContainsPoint(sprite.boundingBox, touchLocation))
{
[sprite runAction:[CCSequence actions:
[CCScaleTo actionWithDuration:0.4 scale:0],
[CCCallFuncO actionWithTarget:self selector:@selector(removeSprite:) object:sprite],
nil]];
...//play audio etc
}
....
}
-(void) removeSprite:(CCSprite*) s
{
[s.parent removeChild:s cleanup:YES];
}
对于其他操作,请尝试 CCMoveTo
或 CCJumpTo
或 CCRotateBy
。您可以一次运行多个操作,因此在我提供的 runAction:
行上方,尝试另一个 [sprite runAction:[CCRotateBy actionWithDuration:0.4 angle:360]]
关于iphone - 如何让 Sprite 在点击时随着各种动画消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7736922/