问题
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;
}
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);
The following code gets a sigbart error, because of the part I commented out. If I delete the comment, the code works fine.
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.
回答1:
Assuming that your "person" class and "Player" classes referred to are the same thing, this code:
Player *object = nil;
CCSprite *bow;
bow = [object bow];
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.
来源:https://stackoverflow.com/questions/8214684/why-cant-i-use-this-sprite-in-cocos2d-objective-c