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 8256833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:59:42+00:00 2026-06-08T01:59:42+00:00

I am currently running this Block of code in my Level1.m file, this is

  • 0

I am currently running this Block of code in my Level1.m file, this is the scene for the first level of my game.

#import "BankerL1.h"
#import "GameOverScene.h"
#import "BankerGameWin1.h"

// HelloWorldLayer implementation
@implementation BankerL1Layer
@synthesize label = _label;
@synthesize Banker = _Banker;
@synthesize WalkAction = _WalkAction;
@synthesize MoveAction = _MoveAction;

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

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

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

// return the scene
return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {

    CGSize winSize = [[CCDirector sharedDirector] winSize];

    CCSprite *background = [CCSprite spriteWithFile:@"Ninja Menu Background.png"];
    background.position = ccp(winSize.width/2, winSize.height/2);
    [self addChild:background z:-1];

    CCLabelTTF *Levelcounter = [CCLabelTTF labelWithString:@"Level 1" fontName:@"Marker         Felt" fontSize:40];
    Levelcounter.position =  ccp(winSize.width * 0.80,winSize.height * 0.92);
    [self addChild: Levelcounter];

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:     @"GreenorcSpriteSheet_default.plist"];

    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode     batchNodeWithFile:@"GreenorcSpriteSheet_default.png"];
    [self addChild:spriteSheet];

    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <=6; ++i) {
        [walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache]     spriteFrameByName: [NSString stringWithFormat:@"Greenorc%d.png", i]]];

    }
    CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
    self.Banker = [CCSprite spriteWithSpriteFrameName:@"Greenorc1.png"];
    _Banker.position = ccp(winSize.width/2, (winSize.height/2)-190);
    self.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate     actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    //[_Banker runAction:_WalkAction];
    [spriteSheet addChild:_Banker];

    self.isTouchEnabled = YES;

    _targets = [[NSMutableArray alloc] init];

}
return self;
}

- (void) onEnter
{
// First, call super if you override this. ALWAYS.
[super onEnter];

[self schedule:@selector(gameLogic:) interval:1.5];
[self scheduleUpdate]; // use this instead of schedule: if it's for an update method.

}

//Implements the Callback function above
-(void)gameLogic:(ccTime)dt {
[self addTarget];

}

-(void)spriteMoveFinished:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if (sprite.tag == 1) { // target
    [_targets removeObject:sprite];

    GameOverScene *gameOverScene = [GameOverScene node];
    [gameOverScene.layer.label setString:@"You Lose :["];
    [[CCDirector sharedDirector] replaceScene:gameOverScene];

} else if (sprite.tag == 2) { // projectile
    [_targets removeObject:sprite];
}
}

//Adds the "targets" or in this case enemies, to the scene and spawns/moves them
-(void)addTarget {

CCSprite *target = [CCSprite spriteWithFile:@"seeker.png"
rect:CGRectMake(0, 0, 40, 40)];

target.tag = 1;
[_targets addObject:target];

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = target.contentSize.height/2;
int maxX = winSize.height - target.contentSize.height/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;//Randomizes the place it will spawn on X-    Axis

// Create the target slightly off-screen along the top edge,
// and along a random position along the Y axis as calculated above
target.position = ccp(actualX, winSize.height + (target.contentSize.height/2));
[self addChild:target];

// Determine speed of the target
int minDuration = 2.0;
int maxDuration = 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;//Speed is randomized     between 2 and 5

// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration
                                    position:ccp(actualX, -target.contentSize.height/2)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
                                         selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

}

//WHEN THE THINGS COLLIDE, THEY DISSAPEAR
- (void)update:(ccTime)dt {
CGSize winSize = [[CCDirector sharedDirector] winSize];

NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
    CGRect targetRect = CGRectMake(
                                       target.position.x - (target.contentSize.width/2),
                                       target.position.y - (target.contentSize.height/2),
                                       target.contentSize.width,
                                       target.contentSize.height);
    BOOL playerHit = FALSE;
    for (CCSprite *player in _targets) {
        CGRect playerRect = CGRectMake(
                                       player.position.x - (player.contentSize.width/2),
                                       player.position.y - (player.contentSize.height/2),
                                       player.contentSize.width,
                                       player.contentSize.height);

        if (CGRectIntersectsRect(playerRect, targetRect)) {
            //[targetsToDelete addObject:target];
            playerHit = TRUE;
                [targetsToDelete addObject:target];

            break;
        }
    }

    for (CCSprite *target in targetsToDelete) {
        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];

        _targetsDestroyed++;
        [_label setString:[NSString stringWithFormat:@""]];
        if (_targetsDestroyed > 30) {
            GameWinScene *gameWinScene = [GameWinScene node];
            _targetsDestroyed = 0;
            [[CCDirector sharedDirector] replaceScene:gameWinScene];
        } else{
            NSString *killcounttext = [NSString stringWithFormat:@"Catches: %i",     _targetsDestroyed];
            self.label = [CCLabelTTF labelWithString:killcounttext fontName:@"Zapfino"     fontSize:20];
            _label.color = ccc3(225,225,225);
            _label.position = ccp(winSize.width * 0.20,winSize.height * 0.92);
            [self addChild:_label];
        }
    }

    if (targetsToDelete.count > 0) {
        [targetsToDelete addObject:target];
    }
    [targetsToDelete release];
}

for (CCSprite *target in targetsToDelete) {
    [_targets removeObject:target];
    [self removeChild:target cleanup:YES];
    //[[SimpleAudioEngine sharedEngine] playEffect:@"ProjectileHit.wav"];
}
[targetsToDelete release];
}

-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0
                                          swallowsTouches:YES];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];

float bankerVelocity = 320.0/2.0;

CGPoint moveDifference = ccpSub(touchLocation, _Banker.position);

float distanceToMove = ccpLength(moveDifference);

float moveDuration = distanceToMove / bankerVelocity;

    if (moveDifference.x < 0) {
    _Banker.flipX = NO;
    } else {
    _Banker.flipX = YES;
    }

[_Banker stopAction:_MoveAction];

if (!_Moving) {
    [_Banker runAction:_WalkAction];
}
self.MoveAction = [CCSequence actions:
                   [CCMoveTo actionWithDuration:moveDuration position:touchLocation],
                   [CCCallFunc actionWithTarget:self selector:@selector(bearMoveEnded)],
                   nil];

[_Banker runAction:_MoveAction];
_Moving = TRUE;

}

-(void)bearMoveEnded {
[_Banker stopAction:_WalkAction];
_Moving = FALSE;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
self.Banker = nil;
self.WalkAction = nil;

// don't forget to call "super dealloc"
[super dealloc];
}
@end

The scene starts and everything works fine except as soon as a target is added (this is what I think is happeneing) the game freezes and crashes.
I don’t know if the target being added is what is causing the crash, but it seems that whenever it is time for the target to come on, it crashes.

There is nothing in the debugger that says the game crashed, Xcode things the game is still running even though it is frozen. Please help :/ thanks

  • 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-08T01:59:46+00:00Added an answer on June 8, 2026 at 1:59 am

    retain the _targets array in the init method like this: _targets = [[[NSMutableArray alloc] init] retain];

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

Sidebar

Related Questions

Okay so, I'm currently running this code to move a bunch of data from
I have this running software that is currently being used by about 400 people.
I'm currently using Entity Framework and am running into this issue: The relationship between
Currently I'm running a MySQL query like this $query = SELECT t1.id AS 'post_id',
see this fiddle . This upon running gives an error in console. I'm currently
I'm running code that sometimes yields this: UInt32 current; int left, right; ... //sometimes
So here's the problem, I'm reading a level file for my game, works fine
I am currently running the latest version of Code-blocks in Ubuntu 11.04. I have
Ok, so I'm currently running this script to remove all the excess spaces, linebreaks,
The Short and Sweet Running this code in Ruby 1.9: FOO = global constant

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.