Objective-C:'GameLayer'可能无法响应'-addChild:z'

时间:2009-12-22 06:04:35

标签: iphone objective-c cocos2d-iphone

我是Objective-C和iPhone开发的新手。我正在使用Cocos2d-iphone库作为游戏引擎,试图开发一个非常简单的游戏。

我一直在学习一些教程,以便掌握一些事情并尝试将一些非常简单的“关卡”放在一起,其中一些球使用Chipmunk物理引擎在屏幕上反弹。下面是导致问题的文件的定义和实现。编译期间生成的警告是:

  

GameScene.m:69:警告:'GameLayer'可能无法响应'-addChild:z:'

GDB还提供以下内容:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** - [GameLayer addChild:z:]:无法识别的选择器发送到实例0xf6ff90'

如果我注释掉这一行,那么当调用GameLayer时程序将不再崩溃。 (用“<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>

// GameScene.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "chipmunk.h"

@interface GameScene : Scene
{
}
@end

@interface GameLayer : Layer
{
    cpSpace *space;
}

-(void) makeBall: (float) x y:(float)y;
-(void) setupGame;

@end

然后执行:

// GameScene.m
#import "MenuScene.h"
#import "GameScene.h"

void updateShape(void* ptr, void* unused){
    cpShape* shape = (cpShape*)ptr;
    Sprite* sprite = shape->data;
    if(sprite){
        cpBody* body = shape->body;
        [sprite setPosition:cpv(body->p.x, body->p.y)];
    }
}

@implementation GameScene
- (id) init {
    self = [super init];
    if (self != nil) {
        Sprite * bg = [Sprite spriteWithFile:@"background.png"];
        [bg setPosition:cpv(240, 160)];
        [self add:bg z:0];
        [self add:[GameLayer node] z:1];
    }
    return self;
}
@end

@implementation GameLayer
- (id) init {
    self = [super init];

    if(self != nil) {
        isTouchEnabled = YES;
    }

    //Make it shoot:
    isTouchEnabled = YES;

    //Initialize Chipmunk:
    cpInitChipmunk();

    // Create the chipmunk space
    space = cpSpaceNew();
    cpSpaceResizeStaticHash(space, 400.0f, 40);
    cpSpaceResizeActiveHash(space, 100, 600);

    space->gravity = cpv(0, -400);
    space->elasticIterations = space->iterations;

    // Update Chipmunk
    // Calls the "tick" function below. This function subsequently
    // makes a call to the update function which updates all of the 
    // sprites on the screen.
    [self schedule: @selector(tick:) interval: 1.0f/60.0f];

    // Setup the game (place the player and balls on the screen)
    [self makeBall:100 y:100];

    return self;
}

// Sets up the game, placing the balls on the stage. Also creates the
// flor boundries and player
-(void) setupGame {
}

// Creates a ball and adds it to the desired location on the screen
-(void) makeBall: (float) x y:(float)y {
    Sprite *ball = [[Sprite spriteWithFile:@"start_ball.png"] retain];
    ball.position = cpv(x,y);
    [self addChild:ball z:2]; << Apparently this line is causing the error?
}

-(void)tick: (ccTime)dt {
    cpSpaceStep(space, 1.0f/60.0f);
    //cpSpaceHashEach(space->activeShapes, &updateShape, nil);
}

@end

我已经评论了上面代码中我认为导致错误的行。 (&lt;&lt;&lt;&lt;显然这条线......)。

我确信这是愚蠢的,但任何帮助都会受到赞赏!

谢谢:)

2 个答案:

答案 0 :(得分:0)

编译时警告是因为@interface部分没有任何名为- addChild:z:的内容。

运行时错误是因为@implementation部分没有- addChild:z:

答案 1 :(得分:0)

我在使用Cocos2D 0.8时遇到了这个问题,并开发了一种解决方法,即在图层类中声明一个数组,该数组将包含您想要跟踪的子对象。

@interface GameLayer : Layer
{
    cpSpace *space;
    NSMutableArray* children;
}

然后您可以使用...

添加孩子
[[self children] addObject:myObject];

将数组的内容操作到你想做的任何事情都很容易。