返回枚举 ivar 会导致 EXC_BAD_ACCESS
我正在编写一个游戏,其中 MainGameplay 类通过将 ivar 设置为枚举值来跟踪轮到谁:
typedef enum {
GAME_NOT_STARTED,
PLAYER_1_TO_MOVE,
PLAYER_2_TO_MOVE
} WhoseTurnIsIt;
然后 GameBoard 类检查移动尝试是否有效,并调用 turnEnded:(WhoseTurnIsIt)turn
或 MainGameplay.m 中的 reportTurnFailure:(WhoseTurnIsIt)turn
。
当我尝试在 MainGameplay 的接收方法中访问此返回值时,我会立即收到 EXC_BAD_ACCESS。看来我应该保留一些东西,但你不能保留枚举。在调试器中,值就在那里,所以我不明白哪些内容被错误访问。
这是在 GameBoard 中执行调用的代码:
-(void)buttonPressed:(id)sender {
CCArray *kids = [[[CCDirector sharedDirector] runningScene] children];
if (!mainScene) { // mainScene is an ivar on each the GameBoard's buttons.
for (CCScene *s in kids) {
// this looks crazy because the "main" scene is actually a controlling layer that has as a child the main gameplay layer:
if ([s isKindOfClass:[ControlLayer class]]) {
self->mainScene = (MainGameplay *)((ControlLayer *) s).gameLayer;
}
}
}
if (MOVE_NO_ERROR == [self checkMove:mainScene.turn]) {
[self setMove:mainScene.turn];
[mainScene turnEnded:mainScene.turn]; // This line and the next are the ones causing the EXC_BAD_ACCESS
} else [mainScene reportTurnFailure:mainScene.turn]; // This line too.
}
编辑 mainScene 中的函数被调用如下:
-(void) reportTurnFailure:(WhoseTurnIsIt)_turn {
NSLog(@"MainScene still valid"); // This line works fine
NSLog(@"Bzzzzzt. Player %@, try again", _turn); // This line crashes BUT _turn shows up with a proper value in the debugger.
}
I'm writing a game where a MainGameplay class keeps track of whose turn it is by setting an ivar to an enum value:
typedef enum {
GAME_NOT_STARTED,
PLAYER_1_TO_MOVE,
PLAYER_2_TO_MOVE
} WhoseTurnIsIt;
Then the GameBoard class checks whether a move attempt is valid, and calls either turnEnded:(WhoseTurnIsIt)turn
or reportTurnFailure:(WhoseTurnIsIt)turn
in MainGameplay.m.
I get EXC_BAD_ACCESS as soon as I try to access this returned value back in MainGameplay, in the receiving methods. Seems like I should retain something, but you can't retain an enum. In the debugger the values are there, so I don't understand what's being accessed improperly.
This is the code doing the calling in GameBoard:
-(void)buttonPressed:(id)sender {
CCArray *kids = [[[CCDirector sharedDirector] runningScene] children];
if (!mainScene) { // mainScene is an ivar on each the GameBoard's buttons.
for (CCScene *s in kids) {
// this looks crazy because the "main" scene is actually a controlling layer that has as a child the main gameplay layer:
if ([s isKindOfClass:[ControlLayer class]]) {
self->mainScene = (MainGameplay *)((ControlLayer *) s).gameLayer;
}
}
}
if (MOVE_NO_ERROR == [self checkMove:mainScene.turn]) {
[self setMove:mainScene.turn];
[mainScene turnEnded:mainScene.turn]; // This line and the next are the ones causing the EXC_BAD_ACCESS
} else [mainScene reportTurnFailure:mainScene.turn]; // This line too.
}
EDIT Functions in mainScene being called go like this:
-(void) reportTurnFailure:(WhoseTurnIsIt)_turn {
NSLog(@"MainScene still valid"); // This line works fine
NSLog(@"Bzzzzzt. Player %@, try again", _turn); // This line crashes BUT _turn shows up with a proper value in the debugger.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
_turn
不是对象,但%@
格式说明符表示参数是对象。请改用%i
。_turn
is not an object, but the%@
format specifier says that the argument is an object. Use%i
instead.