这是enemyShip 类的init 方法,我在下面的enemyShoot 方法中发射之前分配/初始化子弹。
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
CGSize windowSize =[[CCDirector sharedDirector] winSize];
[self initWithFile:@"Spaceship_tut.png"];
[self setPosition:ccp(windowSize.width/2, windowSize.height - windowSize.height/9)];
[self moveAround];
self.bullet = [[Bullets alloc] init];
self.bullet1 = [[Bullets alloc] init];
self.bullet2 = [[Bullets alloc] init];
self.bullet3 = [[Bullets alloc] init];
self.bullet4 = [[Bullets alloc] init];
self.bullet5 = [[Bullets alloc] init];
myBullets = [[NSArray alloc] initWithObjects:bullet1,bullet2,bullet3,bullet4,bullet5, nil];
index = 0;
}
return self;
}
-(void)enemyShoot:(CCLayer*)theScene withThePoint:(CGPoint)whereTo
{
NSArray *myBullets = [[NSArray alloc] initWithObjects:bullet,bullet1,bullet2,bullet3,bullet4,bullet5, nil];
bullet = [[Bullets alloc ] init];
self.bullet = [myBullets objectAtIndex:index];
self.bullet.position = self.position;
[theScene addChild:bullet];
id action = [CCMoveTo actionWithDuration:2.0 position:ccp(whereTo.x,whereTo.y)];
[self.bullet runAction:action];
index++;
if (index == 4) {
index = 0;
}
}
这是在enemyShip 类中;这艘船能够发射子弹,这就是它能够做到这一点的方法。下面是bullet.m文件中子弹的init方法
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
self = [CCSprite spriteWithFile:@"enemyBullet2.png"];
}
return self;
}
问题是为什么你们认为我得到这个错误
"Assertion failure in -[Level2 addChild:z:tag:], /Users/dulybon1/Documents/DEVELOPER/lesson1/lesson1/libs/cocos2d/CCNode.m:355"
我认为这意味着在敌人开火几枪后,该物体在内存中不存在。下面是这个函数在游戏层中的调用方式;这个函数在 init 方法中被连续调用,使用:
[自我计划:@selector(continuousShooting:)];
-(void)continuousShooting:(ccTime)dt
{
[enemyShip enemyShoot:self withThePoint:ccp(myShip.position.x,myShip.position.y -200)];
}