我尝试删除具有延迟时间的目标,代码喜欢这个
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
targetsToRemove = [[NSMutableArray array] init];
for (CCSprite *target in _targets) {// here _targets is NSMutableArray
if (CGRectContainsPoint(target.boundingBox, location)) {
[targetsToRemove addObject:target];
}
}
for (CCSprite *target in targetsToRemove) {
if (target.tag == 1) {
[target setTexture:[[CCTextureCache sharedTextureCache] addImage:@"1.png"]];
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
}
else {
CCLOG(@"remove target immediately");
}
}
}
如果target.tag = 1,我想延迟0.5以显示其图像,然后将其删除,我尝试使用scheduleOnce,但它会崩溃,所以我该怎么办?
答案 0 :(得分:0)
我不确定如何在Objective-C中编写这个(我只熟悉C#的Cocos2D-XNA),但看看是否有效:
创建一个CCSequence,序列中的第一个动作是CCDelayTime,其中0.5为参数,序列中的第二个动作是CCRemoveSelf。然后在目标精灵上运行此序列。
希望这对你有所帮助。您可以尝试https://gamedev.stackexchange.com/获取更多帮助。
答案 1 :(得分:0)
尝试这种方式:
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
...
for (CCSprite *target in targetsToRemove) {
if (target.tag == 1) {
[target setTexture:[[CCTextureCache sharedTextureCache] addImage:@"1.png"]];
CCDelayTime *delay =[CCDelayTime actionWithDuration:3.0f];
CCCallFunc *removeTarget = [CCCallFunc actionWithTarget:self selector:@selector(removeSprites:data:)data:target];
[target runAction:[CCSequence actionWithActions:delay, removeTarget, nil]];
}
}
-(void)removeSprites:(id)sender data:(CCSPrite *)sprtToDelete
{
if(!_targets)
[_targets removeObject:sprtToDelete];
[sprtToDelete removeFromParentAndCleanUp:YES];
}
在这里,您要对每个要删除的精灵运行两个动作。因此,在第一个操作中,您等待3秒,在第二个操作中,您将使用参数作为此精灵调用方法。在那种方法中你清理你的精灵。
希望这会有所帮助。如果没有,请知道。