2009-12-22 6 views
0

일반적으로 Objective-C 및 iPhone 개발에 익숙하지 않습니다. 아주 간단한 게임을 개발하기 위해 게임 엔진에 Cocos2d-iphone 라이브러리를 사용하고 있습니다.Objective-C : 'GameLayer'가 '-addChild : z'에 응답하지 않을 수 있습니다.

나는 물건을 걸기 위해 약간의 튜토리얼을 따라 왔고 Chipmunk 물리 엔진을 사용하여 화면 주위로 튀는 아주 단순한 "레벨"을 조합하려고 시도하고 있습니다. 아래는 문제를 일으키는 파일의 정의와 구현입니다. 컴파일하는 동안 생성 된 경고는 다음과 같습니다

GameScene.m : 69 : 경고 : 'Z : -addChild' 'GameLayer 것은'에 응답하지 않을 수

GDB는 다음을 제공합니다 :

- 0 '인식 선택기 인스턴스 0xf6ff90 전송 [: Z : GameLayer하는 AddChild :] ***'

의한 캐치되지 않는 예외 'NSInvalidArgumentException'이유 응용 프로그램 종료

이 줄을 주석으로 처리하면 GameLayer가 호출 될 때 프로그램이 더 이상 중단되지 않습니다. (. "...? < < 분명히 오류"로 문제의 라인을 댓글이 GameScene.m 하단의 - 내 게시물에 코드의 두 번째 비트.)

// 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 

나는 위의 코드에서 오류의 원인이라고 생각하는 줄을 주석 처리했습니다. (< < 분명히이 행 ...).

나는 그것이 바보 같은 것이라고 확신하지만 어떤 도움을 주시면 감사하겠습니다! @interface 부분은 - addChild:z:라는 아무것도하지 않기 때문에

감사합니다 :)

답변

0

컴파일시 경고입니다.

@implementation 부분에 - addChild:z:이 없기 때문에 런타임 오류가 발생했습니다.

+0

실제로, 바보 스럽습니다. 어쨌든, 지금 고쳤습니다. 감사합니다 :) – Louis

+0

Cocos2D 0.8.1 확인, 위의 코드가 작동합니다. @interface는 addChild : z :를 선언 할 필요가 없습니다. GameLayer는 CocosNode를 상속하는 Layer를 상속하고 CocosNode는 addChild를 구현 (선언)합니다. z : – nash

0

나는이 문제를 Cocos2D 0.8에서 가지고 있었고 추적 할 자식 개체를 포함 할 레이어 클래스에 배열을 선언하는 것이 었습니다. 그런 다음 사용하여 자녀를 추가 할 수 있습니다

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

... 그것은 당신이 수행 할 수 있습니다 무엇에 해당 배열의 내용을 조작 할 수있을만큼 쉽게

[[self children] addObject:myObject]; 

.

관련 문제