【问题标题】:Objective-C: 'GameLayer' may not respond to '-addChild:z'Objective-C:“GameLayer”可能不响应“-addChild:z”
【发布时间】:2009-12-22 06:04:35
【问题描述】:

总的来说,我是 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

我已经在上面的代码中注释了我认为导致错误的行。 (

我确定这很愚蠢,但我们将不胜感激!

谢谢:)

【问题讨论】:

    标签: iphone objective-c cocos2d-iphone


    【解决方案1】:

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

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

    【讨论】:

    • 检查 Cocos2D 0.8.1,上面的代码应该可以工作。 @interface不需要声明addChild:z:,因为GameLayer继承了Layer,Layer继承了CocosNode,而CocosNode实现(并声明)了addChild:z:
    【解决方案2】:

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

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

    然后您可以使用...添加子级

    [[self children] addObject:myObject];
    

    很容易将该数组的内容操作为您想做的任何事情。

    【讨论】:

      猜你喜欢
      • 2011-04-30
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2011-05-24
      • 2017-01-31
      • 2013-12-01
      相关资源
      最近更新 更多