1

这是一些我无法理解的问题...我想从上到下移动一个敌方精灵,但敌方精灵在不同的类别中,无法移动它。

这是通过 Enemyscene 在 GameScene 中添加精灵的代码 这是 Enemy.m 的代码

#import "Enemy.h"
#import "GameScene.h"

@implementation Enemy
@synthesize theGame,newSprite,lastTimeFired,firingSpeed,fireInterval,movementSpeed,launched,hp,maxHp;

-(id)initWithGame:(GameLayer *)game
{
    self=[super init];
    if(self !=nil)
    {
        CGSize size=[[CCDirector sharedDirector]winSize];
        self.theGame=game;
        self.lastTimeFired=0;
        NSLog(@"Enemy INIT ");

        int random=arc4random()%3+1;

        newSprite=[CCSprite spriteWithFile:[NSString stringWithFormat:@"enemy%d.png",random]];
        newSprite.position=ccp(100,200);
        [theGame addChild:newSprite];


        switch (random) 
        {
            case 1:
                movementSpeed=5;
                fireInterval=-1;
                break;
            case 2:
                movementSpeed=10;
                fireInterval=-1;
                break;
            case 3:
                movementSpeed=7;
                fireInterval=-1;
                break;
            case 4:
                movementSpeed=3;
                fireInterval=-1;
                break;
            default:
                movementSpeed=5;
                fireInterval=-1;
                break;
        }


    }
    return self;
}

我想把这个敌人精灵移动到 GameScene 层,这里是代码 GameScene.m

#import "GameScene.h"
#import "Hero.h"
#import "Enemy.h"
#define STARTING_LIVES 3;
@implementation GameScene
-(id) init
{
    self =[super init];
    if(self !=nil)
    {
        [self addChild:[GameLayer node]];
    }
    return self;
}

-(void) dealloc
{
        [super dealloc];
}   
@end


@implementation GameLayer
-(id)init
{
    if(self=[super init])
    {

       Hero *hero=[[Hero alloc]initWithGame:self];
        Enemy *e=[[Enemy alloc]initWithGame:self];



        lastTimeEnemyLaunched=0;
        enemyInterval=20;
        lives=3;
        [self schedule:@selector(doThis)];
        NSLog(@"After Schedule");
    }
    return self;
}



-(void)doThis

{
    Enemy *e;

    [e update];

}

@end

请帮助我,我是 cocos2d 游戏开发的新手......当我在模拟器中运行此代码时,它会出现错误

此代码给出 EXC_BAD_ACCESS

 -(void) update: (ccTime) dt
    {
        if( elapsed == - 1)
            elapsed = 0;
        else
            elapsed += dt;
        if( elapsed >= interval ) {
            impMethod(target, selector, elapsed);  //this code gives EXC_BAD_ACCESS
            elapsed = 0;
        }
    }

请帮助我....谢谢

4

0 回答 0