如何从数组添加CCsprite到图层

时间:2012-12-09 07:30:24

标签: objective-c xcode cocos2d-iphone sprite ccsprite

大家好我真的需要一些帮助,我已经在游戏的这一部分停留了一个多星期了,我似乎无法解决这个问题,所以请看下面的代码,

  

#import“HelloWorldLayer.h”
      #import“AppDelegate.h”

@implementation HelloWorldLayer

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super init]) ) {
    moles = [[NSMutableArray alloc] init];
    winSize = [[CCDirector sharedDirector]winSize];
    CCSprite *mole1 = [CCSprite spriteWithFile:@"lightsabericonblue.png"];


   [self starCreateCurrentLevel:mole1];


}
return self;
}


-(void)starCreateCurrentLevel:(CCSprite *)mole1{
starCountCurrentLevel = 10;
for (int i = 0; i < starCountCurrentLevel;) {

    [moles addObject:mole1];        
    starCountCurrentLevel--;

}

[self schedule:@selector(tryPopMoles:) interval:1];

}


- (void)tryPopMoles:(ccTime)dt {
if(moles.count !=   0){
    for (CCSprite *mole in moles) {
        if (arc4random() % moles.count == 0) {
                    if (mole.numberOfRunningActions == 0) {
                        [self popMole:mole];
                    }
        }
    }


}else if(moles.count == 0){
    NSLog(@"No More Moles To Spawn");
    [self unschedule:@selector(tryPopMoles:)];
}

}

- (void) popMole:(CCSprite *)mole {
mole.position = ccp(150, 150);
[self addChild:mole];  
}



- (void) dealloc
{
[moles release];
moles = nil;
[super dealloc];
}

@end

当我运行此代码时,我收到以下错误, * 断言失败 - [HelloWorldLayer addChild:z:tag:],/ Users /...... / libs /cocos2d / CCNode.m:335

当我在init方法中添加孩子时它很好但我不想这样做,我希望能够调用try pop mole,然后根据是否有精灵调用pop mole留在阵列中,我有一种感觉,我错过了什么或做错了什么。

更新 -

#import "HelloWorldLayer.h"
#import "AppDelegate.h"

#pragma mark - HelloWorldLayer
CGSize winSize;
int enemyX;
int enemyY;
int randomAngleY;
int randomAngleX;
int winSizeX;
int winSizeY;
int minDuration = 1;
int maxDuration = 4.0;
int rangeDuration;
int actualDuration;
int starCountCurrentLevel;
int test = 0;

@implementation HelloWorldLayer

+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}


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

moles = [[NSMutableArray alloc] init];
winSize = [[CCDirector sharedDirector]winSize];
[self starCreateCurrentLevel];

}
return self;
}

-(void)starCreateCurrentLevel{
starCountCurrentLevel = 10;
for (int i = 0; i < starCountCurrentLevel;) {
[moles addObject:[CCSprite spriteWithFile:@"lightsabericonblue.png"]];
starCountCurrentLevel--;
}
[self schedule:@selector(tryPopMoles:) interval:3];

}

- (void)tryPopMoles:(ccTime)dt {
 NSMutableArray *tempArray = [NSMutableArray arrayWithArray:moles];
for (int randomIndex = tempArray.count -1; randomIndex < tempArray.count; randomIndex--) {
        CCSprite * sprite = (CCSprite *)[tempArray objectAtIndex:randomIndex];
        //CCSprite *sprite = [tempArray objectAtIndex:randomIndex];
        [self popMole:sprite];
        NSLog(@"Enemy Added");
        [tempArray removeObject:sprite];
    }
 if([tempArray count] == 0){
    NSLog(@"No More Moles To Spawn");
    [self unschedule:@selector(tryPopMoles:)];
}


}

- (void) popMole:(CCSprite *)sprite {

winSizeX = winSize.width - sprite.contentSize.width + 25;
winSizeY = winSize.height - 25 - sprite.contentSize.height + 17;
randomAngleX = arc4random() % winSizeX;
randomAngleY = arc4random() % winSizeY;
enemyX = arc4random() % winSizeX ;
enemyY = arc4random() % winSizeY;
rangeDuration = maxDuration - minDuration;
actualDuration = (arc4random() % rangeDuration) + minDuration;
sprite.position = ccp(enemyX, enemyY);
[self addChild:sprite];

}


- (void) dealloc
{
[moles release];
moles = nil;
[super dealloc];
}

@end

1 个答案:

答案 0 :(得分:1)

您只创建一次精灵,但尝试多次添加精灵。 (请记住,这些是指向对象的指针,所以你总是指的是完全相同的精灵。)如果你想要同一个精灵的10个版本(在不同的位置,比例,速度等),你需要创建精灵(通过创作者方法)10次。您可以对原始版本进行复制,也可以即时创建:

-(void)starCreateCurrentLevel:(CCSprite *)mole1{
starCountCurrentLevel = 10;
for (int i = 0; i < starCountCurrentLevel;) {

    //[moles addObject:mole1];  
    // ^^ You're adding the same sprite (meaning same pointer reference) again and again.  Try this instead:
    [moles addObject:[CCSprite spriteWithFile:@"lightsabericonblue.png"]];      

    starCountCurrentLevel--;
}

当然,现在你不需要将mole1传递给你的方法。您可以(根据您的需要)考虑传入spriteFrameName或其他内容。