我正在用 Objective C 中的 Cocos2d 开发我的第一个 iOS 应用程序。我是 Objective c 的新手,但我试图用谷歌搜索,但我找不到这个普遍问题的解决方案。
-(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);
}
这就是我从我的游戏场景中调用 Player1 加速的方式:
-(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 在我的 gameScene 中:
//implementation
@synthesize Player1 = _Player1;
//header
@property (nonatomic,retain) TPlayer *Player1;
感谢您的耐心和帮助。我不确定我应该把代码的哪一部分放在这里,所以请告诉我什么,我会添加它。
西蒙
编辑 1: Player1 分配在游戏场景的初始化函数中。TPlayer 是 CCSprite 的子类:
_Player1 = [[TPlayer alloc] initWithSpriteFrameName:@"walk2"];
EXC_BAD_ACCESS 发生在这一行:
CGPoint moveDifference = ccpSub(moveLocation, self.position);