0

나는 내 레이어에 액세스하는 데 어려움을 겪고있다. 기본적으로 내 레이어 - 장면 계층은 다음과 같습니다.CCLayer 두통에 액세스!

Compiler.m - CCLayer - + (CCScene) 메서드를 보유하고 다른 모든 CCLayer를로드합니다.
Construct.m - CCLayer - box2d 엔진과 해당 구성 요소를 보유합니다.
Background.m - CCLayer - 배경을 보유합니다.
Hud.m - CCLayer - HUD를 유지합니다. 컴파일러 구현 클래스 내에서

, 나는 컴파일러 CCLayer에 장면과 관련된 모든 노드를 추가 :

@implementation Compiler 


+(CCScene *) scene{ 
    Compiler *compiler = [CompileLayer node]; 
    CCScene *scene = [CCScene node]; 
    [scene addChild: compiler]; 

    //Add A Background Layer. 
    Background *layerBackground = [Background node]; 
    layerBackground.position = CGPointMake(100,100); 
    [compiler addChild: layerBackground z: -1 tag:kBackground]; 


    //Add The Construct. 
    Construct *construct = [Construct node]; 
    [compiler addChild: construct z: 1]; 

    //Add A Foreground Layer Z: 2 
    //After background is working. 

    //Add the HUD 
    HudLayer *hud = [Hud node]; 
     [compiler addChild: hud z:3]; 
} 

모두가 잘 작동이, 내 층 컴파일러에 추가 컴파일러의 장면에서 액세스를 대표단은 예상대로

내 문제는 내 Construct 게임 영웅의 위치에 따라 이동할 수 있도록 Construct 레이어 내부에있는 배경 CCLayers - CCsprite * 배경에 액세스하려고하는 것입니다.

여러 가지 방법을 시도했지만, CCFLrite * 배경을 정의하기 위해 인스턴스 메서드가 아닌 클래스 메서드를 사용하기로 결정했습니다. 따라서 Construct Layer에서 액세스 할 수 있습니다.
@properties를 사용하여 액세스를 시도하고 클래스의 인스턴스 변수를 초기화했습니다.

이 작동
@implementation Background 
-(id) init 
{ 
    self = [super init]; 
    if (self != nil) 
    { 
     CCSprite *temp = [Background bk]; 
     [self addChild:temp z:0 tag:kBackGround]; 
    } 
    return self; 

} 

+(CCSprite *)bk{ 
    //load the image files 
    CCSprite *background = [CCSprite spriteWithFile:@"background.jpg"]; 

    //get the current screen dimensions 
    //CGSize size = [[CCDirector sharedDirector] winSize]; 
    background.anchorPoint = CGPointMake(0,0); 
    background.position = ccp(0,0); 
    return background; 
} 
@end 

, 그것은 배경 레이어에 이미지를로드 :

여기 내 배경 CCLayer입니다.

마지막으로 Construct Layer에서 배경 이미지에 액세스하려고합니다.

@interface Construct : CCLayer{ 
    CCSprite *tempBack; 
} 
@end 

@implementation Construct 

-(id) init{ 
    tempBack = [Background bk]; //The background equals an instance variable 
} 

-(void) tick:(ccTime)dt { 
    tempBack.position.x ++; // To do Something here. 
    tempBack.opacity = 0.5; // Some more stuff here. 

} 
@end 

이 나던 작품은, 난 전혀 적절 배경에 액세스하거나 나던 어떤 측면, tempBack에 '전무'포인터를받을 수 있습니다.

어떻게 백그라운드 CCLayers 클래스 변수 + (CCSprite) bk에 액세스하고 수정할 수 있습니까 ??

답변

1

tempBack iVar이 컴파일러 계층에서 자동으로 리 리되어 사용자가 보유하지 않아서 작동하지 않는 것 같습니다. 여기에 귀하의 초기화 방법은 같아야하는 방법은 다음과 같습니다 또한

-(id) init{ 
    tempBack = [[Background bk] retain]; //The background equals an instance variable 
} 

- 할당 해제에 release는 것을 잊지 마십시오. 그리고 bk 클래스 메서드에 의해 반환 된 다른 배경 스프라이트를 가지고 있기 때문에 작동하는지 확실하지 않습니다 (temptempBack 변수 주소를 인쇄 해 보면 알 수 있습니다). 통지 배경 레이어에서

, 스프라이트를 생성하고 레이어에 추가하기위한

업데이트.그런 다음 나중에 통지에 가입이 코드 조각을 추가

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateBackground:) name:@"PlayerChangedPosition" object:nil]; 

는 그런 구조 층의 update 방법 (또는 다른 예약 방법) 새로운 플레이어와 함께이 알림을 보내 좌표 : 이제

[[NSNotificationCenter defaultCenter] postNotificationName:@"PlayerChangedPosition" object:player]; 

배경의에 레이어를 구현하려면 updateBackground:(NSNotification *)aNotification 메소드를 구현하십시오.

- (void)updateBackground:(NSNotification *)aNotification { 
    // object message returns player object passed when posting notification 
    CGPoint newCoords = [[aNotification object] position]; 
    // adjust background position according to player position 
} 
+0

아니요. 어떻게 그 클래스 메소드 변수에 접근하여 그것을 다른 레이어에서 수정할 수 있습니까? 내 견과류 운전 : ( – Ospho

+0

플레이어의 위치와 함께 알림을 보내고 백그라운드 레이어에서 수신 대기하는 방법은 무엇입니까? – Eimantas

+0

Eimantas, 이것이 작동 할 수 있습니다. 어떻게해야합니까? 작은 코드 스 니펫을 게시 할 수 있습니까? – Ospho