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);
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:
or manually bump up the reference count after initializing it:
To see if that helps. This is why I like ARC 🙂