0

这是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)];

}
4

2 回答 2

0

谢谢你们,我稍微清理了代码,这就是新的enemyShoot 的样子,它似乎可以工作。我还从 init 方法中删除了项目符号和 nsarray 的初始化。再次感谢你们

-(void)enemyShoot:(CCLayer*)theScene with ThePoint:(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);

}

于 2013-08-14T17:00:01.510 回答
0

您可以在 init 方法中将子弹添加到场景中,并在不使用时将它们放置在屏幕外或将它们隐藏。您可以在动作结束时隐藏项目符号,并在动作开始时显示它们。

您可能想清理代码,保留项目符号而不释放任何项目符号。为什么在init中创建一个数组,然后在enemyShoot中创建一个新数组?

如果您不对变量做任何事情,为什么在enemyShoot 中有以下行?

bullet = [[Bullets alloc ] init];

在这里,您可以按照包含射击子弹(激光)的教程:http ://www.raywenderlich.com/3611/how-to-make-a-space-shooter-iphone-game

于 2013-08-14T07:55:01.260 回答