这是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;
}
}
这是在敌人的船级;这艘船能够发射子弹,这是允许它做到这一点的方法。以下是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)];
}
答案 0 :(得分:0)
您可以在init方法中将项目符号添加到场景中,并在不使用时将它们放在屏幕外或隐藏它们。您可以在操作结束时隐藏项目符号,并在操作开始时显示它们。
你可能想要清理你的代码,你一直保留子弹而不释放任何子弹。为什么要在init中创建一个数组,然后在enemyShoot中创建一个新数组?
如果你没有对变量做任何事情,为什么你在enemyShoot中有以下行?
bullet = [[Bullets alloc ] init];
在这里,您可以按照包括射击子弹(激光)的教程进行操作:http://www.raywenderlich.com/3611/how-to-make-a-space-shooter-iphone-game
答案 1 :(得分:0)
谢谢你们,我清理了一下代码,这就是新的敌人看起来像什么,它似乎工作。我还从init方法中删除了子弹和nsarray的初始化。 再次谢谢你们
- (void)enemyShoot:(CCLayer *)theScene withThePoint:(CGPoint)whereTo
{
self.bullet = [Bullets spriteWithFile:@"enemyBullet2.png"];
self.bullet1 = [Bullets spriteWithFile:@"enemyBullet2.png"];
self.bullet2 = [Bullets spriteWithFile:@"enemyBullet2.png"];
self.bullet3 = [Bullets spriteWithFile:@"enemyBullet2.png"];
self.bullet4 = [Bullets spriteWithFile:@"enemyBullet2.png"];
self.bullet5 = [Bullets spriteWithFile:@"enemyBullet2.png"];
myBullets = [[NSArray alloc] initWithObjects:bullet,bullet1,bullet2,bullet3,bullet4,bullet5, nil];
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;
}
NSLog(@"%@",bullet);
}