我的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];
}
}
答案 0 :(得分:1)
这有点像黑客,但这是我在我的程序中使用的,直到找到更优雅的解决方案。我在游戏层中有一个方法,我调用它从父节点中删除一个节点,如下所示:
-(void)removeNode:(CCNode*)node {
[node removeFromParentAndCleanup:YES];
}
当我想在延迟后安排节点删除时,我称之为:
[self performSelector:@selector(removeNode:) withObject:node afterDelay:delay];
简单,而且有效。
答案 1 :(得分:0)
将选择器中的参数名称更改为 protime 而不是 projectile 。选择器必须与对象的类定义中定义的签名匹配。 您的选择器未正确定义,并且Timer可能正在检查对象是否实现了给定的选择器。
我没有时间测试它,所以感谢@RamyAlZuhouri确认。