为什么我不能在Cocos2d / Objective c中使用这个sprite? [英] Why can't I use this sprite in Cocos2d/Objective c?
问题描述
在我的游戏中,在目标c,我有两个类,主要的ArcherClass和人类。
这是人类的.m的一部分。
In my game in objective c, I have two classes, The main ArcherClass and the person class. Here is part of the .m of the person class.
- (id)init{
self = [super init];
if (self) {
_bow = [CCSprite spriteWithFile:@"bpw.png"];
}
return self;
}
这里是ArcherClass.m的一部分
Here is part of main ArcherClass.m
Player *object = nil;
CCSprite *bow;
bow = [object bow];
//bow = [CCSprite spriteWithFile:@"bow.png"];
[self addChild:bow z:1];
bow.position = ccp(150,150);
以下代码由于我注释掉的部分而产生sigbart错误。如果我删除注释,代码工作正常。
The following code gets a sigbart error, because of the part I commented out. If I delete the comment, the code works fine.
为什么我不能使用在Person类中声明的图像文件?我已经宣布精灵具有那个图像价值或任何东西,但由于某种原因,我必须重新声明我的主类中的图像?这是为什么。
Why can I not use the image file declared in the Person class? I already declared that sprite as to having that image value or whatever, but for some reason I have to re-declare the image in my main class? Why is that.
推荐答案
假设你的person类和Player类是相同的东西, >
Assuming that your "person" class and "Player" classes referred to are the same thing, this code:
Player *object = nil;
CCSprite *bow;
bow = [object bow];
将 bow
设置为 nil
。您尚未初始化 object
,因此任何调用的方法都会返回 nil
。
Is going to set bow
to nil
. You haven't initialised object
so any methods called on it will return nil
.
在您的代码中,您需要以下代码:
Somewhere in your code you need the following:
Player *object = [[Player alloc] init];
很难说没有更多上下文的地方,但如果你发送消息给对象,有一个有效的实例。你不说如何射手类和玩家类是相关的。
It's difficult to say where without more context, but if you are sending messages to object, you need to have a valid instance of it. You don't say how the archer class and player classes are related.
这篇关于为什么我不能在Cocos2d / Objective c中使用这个sprite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!