Objective c Cocos2d初学者objc\u msgSend EXC\u错误访问
我正在用Objective C中的Cocos2d为iOS开发我的第一个应用程序。我对Objective C还不熟悉,但我尝试用谷歌搜索,但我找不到解决这个一般问题的方法Objective c Cocos2d初学者objc\u msgSend EXC\u错误访问,objective-c,ios,cocos2d-iphone,ios6,Objective C,Ios,Cocos2d Iphone,Ios6,我正在用Objective C中的Cocos2d为iOS开发我的第一个应用程序。我对Objective C还不熟悉,但我尝试用谷歌搜索,但我找不到解决这个一般问题的方法 -(void)accelerate{ moveSpeed = 720.0 / 3.0; [self stopAllActions]; _moving = FALSE; CCAnimation *walkAnim = [CCAnimation animationWithFrames:_wa
-(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 accelerate的方式:
-(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在我的游戏场景中:
//implementation
@synthesize Player1 = _Player1;
//header
@property (nonatomic,retain) TPlayer *Player1;
谢谢你的耐心和帮助。我不确定我应该把代码的哪一部分放在这里,所以请告诉我是什么,我会添加它
西蒙
编辑1:
Player1在游戏场景的init函数中分配。TPlayer是CCSprite的子类:
_Player1 = [[TPlayer alloc] initWithSpriteFrameName:@"walk2"];
EXC_坏访问发生在这条线上:
CGPoint moveDifference = ccpSub(moveLocation, self.position);
- 属性应以小写字母开头(并以大写字母开头)
- 您将显示在何处@synthesis属性,而不是实际分配实例的位置
_player1 = [[Player alloc] init];
如果看不到回溯和
ccpSub()
的定义,很难说得更多。最好的猜测是,self.position
返回了一个无意义的值,使得ccpSub()
出轨。不太可能的情况是,self
被过度释放,但仍然足够可行,允许方法分派在调用[self position]
时崩溃。您现在就找到了崩溃的解决方案……但您的moveTo函数不正确
-(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]];
}
self.position可能会像前面提到的那样由于内存问题而崩溃。赛尔夫可能会被解雇。您正在运行哪个版本的Xcode?在最新版本中,不需要@synthesis,因为属性会自动为您合成。您也可以考虑将项目转换为ARC。我已经完成了我的CoCo2D项目,我很高兴我做到了 你可以试试:
_Player1 = [[[TPlayer alloc] initWithSpriteFrameName:@"walk2"] autorelease];
或在初始化引用计数后手动增加引用计数:
[self.Player1 retain];
看看这是否有帮助。这就是我喜欢ARC的原因:)在哪一行出现EXC\u BAD\u访问?您在哪里分配播放器1谢谢。。。然而,我认为这与不清理内存有关。我意识到我只能在playerMoveTo中声明一个变量。当我宣布第二个失败时…你们有什么小提示给我,我应该在哪里寻找问题。。?