0

我的 cocos2d 游戏有问题。我试图每 5 秒删除一次敌人射出的弹丸(每个弹丸的寿命应该为 5 秒),但我不知道该怎么做。我得到的错误

Assertion failure in -[CCTimer initWithTarget:selector:interval:]

这是我的代码:

-(void)projectileShooting:(ccTime)dt
{
    [self schedule:@selector(projectileShooting:) interval:2.5];
    projcount++;
    if([proj count] <= 15 ){
        if(enemy1.position.y < 320){
            v = ccp(player.position.x,player.position.y);
            for(CCSprite *enemies in enemy){
            CCSprite * projectilebullet = [CCSprite spriteWithFile:@"Projectile.png"];
            [proj addObject:projectilebullet];
            [self addChild:projectilebullet];
            CGPoint MyVector = ccpSub(enemies.position,player.position );
            MyVector = ccpNormalize(MyVector);
            MyVector = ccpMult(MyVector, enemies.contentSize.width/2);
            MyVector = ccpMult(MyVector,-1);
            projectilebullet.position = ccpAdd(enemies.position, MyVector);
            for(CCSprite *projectile in proj){
                [self schedule:@selector (deleteprojectile:projectile:) interval:5];
            }
        }
    }
}

for(CCSprite *enem2 in enemytwo)
{
    if(  [proj count] <= 15){
        CCSprite * projectilebull = [CCSprite spriteWithFile:@"Projectile.png"];
        CGPoint MyVector = ccpSub(enem2.position,player.position );
        MyVector = ccpNormalize(MyVector);
        MyVector = ccpMult(MyVector, enem2.contentSize.width/2+10);
        MyVector = ccpMult(MyVector,-1);
        projectilebull.position = ccpAdd(enem2.position, MyVector);
        [self addChild:projectilebull];
        [proj addObject:projectilebull];
        for(CCSprite *projectile in proj){
        }
    }
}



}

-(void)deleteprojectile:(CCSprite *)protime:(ccTime)dt{
    NSMutableArray *timepro = [[NSMutableArray alloc]init];
    [timepro addObject:protime];
    for(CCSprite *objecttime in timepro){
        [proj removeObject:objecttime];
        [self removeChild:objecttime cleanup:YES];
    }
}
4

2 回答 2

1

这有点小技巧,但这是我在程序中使用的,直到我找到更优雅的解决方案。我的游戏层中有一个方法,我调用它来从其父节点中删除一个节点,如下所示:

-(void)removeNode:(CCNode*)node {
    [node removeFromParentAndCleanup:YES];
}

当我想在延迟后安排一个节点删除时,我这样称呼它:

[self performSelector:@selector(removeNode:) withObject:node afterDelay:delay];

很简单,而且很有效。

于 2012-12-26T20:42:50.130 回答
0

将选择器中的参数名称更改为protime而不是projectile。选择器必须与对象的类定义中定义的签名匹配。您的选择器未正确定义,可能 Timer 正在检查对象是否实现给定的选择器。

我没有时间测试它,所以感谢@RamyAlZuhouri 的确认。

于 2012-12-26T15:41:48.927 回答