Why can't I use this sprite in Cocos2d/Objective c?

时光毁灭记忆、已成空白 提交于 2020-01-16 08:06:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!