iphone - 如何让 Sprite 在点击时随着各种动画消失?

标签 iphone objective-c ios cocos2d-iphone

我是 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];
}

对于其他操作,请尝试 CCMoveToCCJumpToCCRotateBy。您可以一次运行多个操作,因此在我提供的 runAction: 行上方,尝试另一个 [sprite runAction:[CCRotateBy actionWithDuration:0.4 angle:360]]

关于iphone - 如何让 Sprite 在点击时随着各种动画消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7736922/

相关文章:

iphone - 无法使用 Three20 删除表中的行

iphone - 苹果是否会皱眉或不允许特定于设备的编码?

ios - 为什么将其[__NSCFString objectAtIndex:]:无法识别的选择器发送到实例?

objective-c - 如何自动验证所有方法都在 xcode 中声明

iphone - NSnumberFormatter 在模拟器上工作但不在设备上工作

ios - iOS 中的 Alamofire 正在接收字典而不是数组

ios - 在 iOS/Xcode 中将单个 View 转换为选项卡式应用程序

ios - UITableView 以编程方式在 UIScrollView 内部不调用委托(delegate)方法

iphone - 单击时如何使自定义按钮键像iPhone按钮键一样大

ios - 在 cllocationdistance 中转换数据时出现问题