Why does my program crash when given negative values?

Posted by Wayfarer on Stack Overflow See other posts from Stack Overflow or by Wayfarer
Published on 2010-03-20T20:02:29Z Indexed on 2010/03/20 20:31 UTC
Read the original article Hit count: 299

Alright, I am very confused, so I hope you friends can help me out. I'm working on a project using Cocos2D, the most recent version (.99 RC 1). I make some player objects and some buttons to change the object's life. But the weird thing is, the code crashes when I try to change their life by -5. Or any negative value for that matter, besides -1.

    NSMutableArray *lifeButtons = [[NSMutableArray alloc] init];
    CCTexture2D *buttonTexture = [[CCTextureCache sharedTextureCache] addImage:@"Button.png"];
    LifeChangeButtons *button = nil;

    //top left
    button = [LifeChangeButtons lifeButton:buttonTexture ];
    button.position = CGPointMake(50 , size.height - 30);
    [button buttonText:-5];
    [lifeButtons addObject:button];
    //top right
    button = [LifeChangeButtons lifeButton:buttonTexture ];
    button.position = CGPointMake(size.width - 50 , size.height - 30);
    [button buttonText:1];
    [lifeButtons addObject:button];
    //bottom left
    button = [LifeChangeButtons lifeButton:buttonTexture ];
    button.position = CGPointMake(50 , 30);
    [button buttonText:5];
    [lifeButtons addObject:button];
    //bottom right
    button = [LifeChangeButtons lifeButton:buttonTexture ];
    button.position = CGPointMake(size.width - 50 , 30);
    [button buttonText:-1];
    [lifeButtons addObject:button];

    for (LifeChangeButtons *theButton in lifeButtons) {
        [self addChild:theButton];
    }

This is the code that makes the buttons. It simply makes 4 buttons, puts them in each corner of the screen (size is the screen) and adds their life change ability, 1,-1,5, or -5. It adds them to the array and then goes through the array at the end and adds all of them to the screen. This works fine.

Here is my code for the button class: (header file)

//
//  LifeChangeButtons.h
//  Coco2dTest2
//
//  Created by Ethan Mick on 3/14/10.
//  Copyright 2010 Wayfarer. All rights reserved.
//

#import "cocos2d.h"


@interface LifeChangeButtons : CCSprite <CCTargetedTouchDelegate> {
    NSNumber *lifeChange;

}

@property (nonatomic, readonly) CGRect rect;
@property (nonatomic, retain) NSNumber *lifeChange;

+ (id)lifeButton:(CCTexture2D *)texture;
- (void)buttonText:(int)number;


@end

Implementation file:

//
//  LifeChangeButtons.m
//  Coco2dTest2
//
//  Created by Ethan Mick on 3/14/10.
//  Copyright 2010 Wayfarer. All rights reserved.
//

#import "LifeChangeButtons.h"
#import "cocos2d.h"
#import "CustomCCNode.h"

@implementation LifeChangeButtons

@synthesize lifeChange;


//Create the button

+(id)lifeButton:(CCTexture2D *)texture {
    return [[[self alloc] initWithTexture:texture] autorelease];
}

- (id)initWithTexture:(CCTexture2D *)atexture
{
    if ((self = [super initWithTexture:atexture])) {
        //NSLog(@"wtf");
    }
    return self;
}

//Set the text on the button

- (void)buttonText:(int)number {
    lifeChange = [NSNumber numberWithInt:number];
    NSString *text = [[NSString alloc] initWithFormat:@"%d", number];
    CCLabel *label = [CCLabel labelWithString:text fontName:@"Times New Roman" fontSize:20];
    label.position = CGPointMake(35, 20);
    [self addChild:label];

}

- (CGRect)rect
{
    CGSize s = [self.texture contentSize];
    return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}

- (BOOL)containsTouchLocation:(UITouch *)touch
{
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}

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


- (void)onExit
{
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super onExit];
}   

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

    if ( ![self containsTouchLocation:touch] ) return NO;


    NSLog(@"Button touch event was called returning yes. ");
    //this is where we change the life to each selected player
    NSLog(@"Test1");
    NSMutableArray *tempArray = [[[UIApplication sharedApplication] delegate] selectedPlayerObjects];
    NSLog(@"Test2");
    for (CustomCCNode *aPlayer in tempArray) {
        NSLog(@"we change the life by %d.", [lifeChange intValue]);
        [aPlayer changeLife:[lifeChange intValue]];
    }

    NSLog(@"Test3");
    return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
    NSLog(@"You moved in a button!");
}


- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSLog(@"You touched up in a button");
}


@end

Now, This function:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

Is where all the shit goes down. It works for all of the buttons except the -5 one. And then, it gets to:

NSLog(@"we change the life by %d.", [lifeChange integerValue]);

And it crashes at that statement. It only crashes when given anything less than -1. -1 works, but nothing smaller does. Here is the code in the CustomCCNode Class, "changeLife" that is being called.

- (void)changeLife:(int)lifeChange {
    NSLog(@"change life in Custom Class was called");
    NSLog(@"wtf is lifechange: %d", lifeChange);
    life += lifeChange;
    lifeString = [[NSString alloc] initWithFormat:@"%d",life];
    [text setString:lifeString];
}

Straight forward, but when the NSnumber is -5, it doesn't even get called, it crashes at the NSlog statement. So... what's up with that?

© Stack Overflow or respective owner

Related posts about cocos2d

Related posts about cocos2d-iphone