Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:51:16+00:00 2026-06-15T14:51:16+00:00

Hi guys I really need some help, I have been stuck in this part

  • 0

Hi guys I really need some help, I have been stuck in this part of my game for over a week now and I can’t seem to get past this issue, So have look at my code below,

#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

When I run this code i get the following error, * Assertion failure in -[HelloWorldLayer addChild:z:tag:], /Users/……/libs/cocos2d/CCNode.m:335.

I when i add the child in the init method its fine but I don’t want to do that, I want to be able to call the try pop mole which will then call the pop mole based on if there is anymore sprites left in the array, I have a feeling I am missing something or doing something wrong.

UPDATE –

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:51:17+00:00Added an answer on June 15, 2026 at 2:51 pm

    You’re only creating the sprite once, but trying to add it several times. (Remember, these are pointers to objects, so you’re always referring to exactly the same sprite.) If you want 10 versions of the same sprite (at different positions, scales, speeds, whatever), you’ll need to create the sprite (via the creator method) 10 times. You can do a copy on the original one, or just create it on the fly:

    -(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--;
    }
    

    Of course, now you don’t need to pass mole1 into your method. You may (depending on your need) consider passing in a spriteFrameName or something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need some help with this. I'm 100% new to it. I need
hey guys having this really simple problem but cant seem to figure out have
Alright guys, I really hurt my brain over this one and I'm curious if
I'm really confused so I hope you guys can help me. I know how
This might be a really stupid question and make you guys laugh. I've deployed
Ok guys, I know this is pretty a rare case but I really would
This question might seem very specific but I am in need of some ideas
I need your help, i'm stacked on this project. When i run my applicaion
let's get some different points of view about this situation I have. I'm creating
I'm sorry if this comes through as a noob question, but I'm really stuck

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.