0
votes

I'm developing my first app for iOS with Cocos2d in Objective C. I'm new to objective c but I tried to google around but I cannot find solution for this general problem.

-(void)accelerate{
     moveSpeed = 720.0 / 3.0;

     [self stopAllActions];
     _moving = FALSE;
     CCAnimation *walkAnim = [CCAnimation animationWithFrames:_walkAnimFrames delay:0.066f];
     self.walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
     CGPoint loc = ccp(500, 200);
     [self playerMoveTo:loc];
 }

-(void)playerMoveTo:(CGPoint)moveLocation{
     CGPoint moveDifference = ccpSub(moveLocation, self.position); //here is EXC_BAD_ACCESS
     float distanceToMove = ccpLength(moveDifference);
 }

and this is the way i call Player1 accelerate from my Game Scene:

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

Player1 is in my gameScene :

//implementation
@synthesize Player1 = _Player1;

//header
@property (nonatomic,retain) TPlayer *Player1;

Thank you for your patience and help. I wasn't sure what part of code i should put here so please tell me what and I will add it.

Simon

EDIT 1: Player1 is allocated in init function of game scene. TPlayer is subclass of CCSprite :

_Player1 = [[TPlayer alloc] initWithSpriteFrameName:@"walk2"];

And EXC_BAD_ACCESS happens on this line:

CGPoint moveDifference = ccpSub(moveLocation, self.position);
3
On what line does the EXC_BAD_ACCESS occur? - Ben Trengrove
Also where do you alloc Player1 - Ben Trengrove

3 Answers

0
votes

self.position might be crashing as was mentioned because of memory issues. Self might be getting deallocated on you. What version of Xcode are you running? In the latest version the @synthesize is unnecessary as properties are automatically synthesized for you. You might also consider converting your project to ARC. I have done it with my Cocos2D project and I'm happy I did.

You could try:

_Player1 = [[[TPlayer alloc] initWithSpriteFrameName:@"walk2"] autorelease];

or manually bump up the reference count after initializing it:

[self.Player1 retain];

To see if that helps. This is why I like ARC :)

0
votes
  • properties should start with lower case letters (and be camelCased)

  • you show where you @synthesize the property, not where you actually allocate an instance.

Something like:

_player1 = [[Player alloc] init];

Hard to say much more without seeing the backtrace and the definition of ccpSub(). Best guess is that self.position is returning a nonsense value making ccpSub() go off the rails. Less likely is that self is over-released, but still viable enough to allow a method dispatch that subsequently crashes on the call to [self position].

0
votes

You got solution for crash now right...But ur moveTo function is not correct.

-(void)playerMoveTo:(CGPoint)moveLocation{
     CGPoint moveDifference = ccpSub(moveLocation, self.position); //here is EXC_BAD_ACCESS
     float distanceToMove = ccpLength(moveDifference);

     [self runAction:[CCMoveTo actionWithDuration:1 position:moveLocation]];

 }