访问CCLayer头疼!

发布于 2024-10-28 07:56:38 字数 2291 浏览 2 评论 0原文

我在访问我的图层时遇到了一些麻烦,这让我发疯。基本上,我的图层场景层次结构如下:

Compiler.m - CCLayer - 保存 +(CCScene) 方法并加载所有其他 CCLayers。
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 层内的背景 CCLayers - CCsprite *background,以便我可以根据 Construct 游戏英雄的位置移动它。

我尝试了许多不同的方法,但我目前决定使用类方法而不是实例方法来定义 CCSprite *background,以便我可以在构造层中访问它。
我还尝试使用 @properties 进行访问并初始化该类的实例变量。

这是我的背景 CCLayer:

@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

这有效,它将图像加载到背景层。

最后,我尝试从构造层访问背景图像。

@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

这不起作用,我在某些方面收到“nil”指针,tempBack 无法正确访问后台,或者根本无法访问后台。

如何访问和修改背景CCLayers类变量+(CCSprite) bk?

I'm having abit of trouble accessing my layers and its driving me insane. Basically, my layer-scene hierachy is as follows:

Compiler.m - CCLayer - holds the +(CCScene) method and loads all the other CCLayers.
Construct.m - CCLayer - holds the box2d engine and its components
Background.m - CCLayer - holds the Background.
Hud.m - CCLayer - holds the HUD.

within the Compiler implementation class, I add the Scene and all the relevant nodes to the Compiler 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];
}  

This all works fine, my layers are added to the compiler and the compiler's scene is accessed by the delegate as predicted.

My problem is that im trying to access my Background CCLayers - CCsprite *background, inside the Construct layer so that I can move it according to the position of my Construct game hero.

I've tried many different methods, but I've currently decided to use a class method rather than an instance method to define CCSprite *background so that I can access it in my Construct Layer.
I've also tried accessing with @properties and initialising an instance variable of the class.

Here is my Background CCLayer:

@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

This works, it loads the image to the background layer.

Then finally, I try to access the background image from the 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

This doesnt work, I receive a 'nil' pointer in some respects, tempBack doesnt access the background properly, or at all.

How can I access and modify the Background CCLayers Class Variable +(CCSprite) bk??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-11-04 07:56:38

它可能不起作用,因为您的 tempBack iVar 是在编译器层自动释放的,并且您不保留它。您的 init 方法应如下所示:

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

另外 - 不要忘记在 dealloc 中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
}

It probably doesn't work because your tempBack iVar is autoreleased in compiler layer and you don't retain it. Here's how your init method should look like:

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

Also - don't forget to release it in dealloc. And I am not sure if it will work either since you'll have different background sprite returned by bk class method (try printing temp and tempBack variable addresses and you'll see).

Update for notifications

In background layer, create the sprite and add it to the layer. Then afterwards add this snippet of code to subscribe to notifications:

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

Then in construct layer's update method (or any other scheduled method) send this notification with new player coordinates:

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

Now in background's layer, implement the updateBackground:(NSNotification *)aNotification method:

- (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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文