0
votes

I'm currently having issues with the following problem. I'm trying to access the layer inside the scene and thereby the elements that I set in that layer. In this case I want access to the conv_label in the Layer to set the text.

I'm doing this via a ConversationClass which extends cc.Class. When I try to access the layer via the variable it doesn't work or with getChildByName or Tag it doesn't work (value is always null).

This is a method inside the ConversationClass, I can console log the currentscene without any problem, but any variable I set doesn't appear in the current scene. in this case the name was "conv_layer", I can access the children by just using array calls, but that's not really a good way it seems and quite confusing.

This I tried:

currentscene.children[0].children[3] will give me the right element.
currentscene.conv_layer.getChildByName("text") says conv_layer does not exist
currentscene.children[0].getChildByName("text") returns null

Does anyone know how to solve this issue or can tell me where my thinking went wrong?

Not sure of it matters, but I call the scene (for now) in the following way.

cc.LoaderScene.preload(conversation_load, function() {
    cc.director.runScene(new ConversationScene());
    this.startGame();
}, this);

This is where I want access

startConversation: function(conversation) {
    this._conversationObject = conversation;
    this._currentScene = cc.director.getRunningScene();
    console.log(this._currentScene); // Shows current scene object (doesn't have conv_layer property)
    if(scene !== null)
        this._currentConversationLayer = scene.conv_layer; // Returns null
},

This is my scene:

var ConversationScene = cc.Scene.extend({
    conv_layer: null,
    onEnter: function() {
        this._super();
        this.conv_layer = new ConversationLayer();
        this.conv_layer.setName('conversation');
        this.conv_layer.setTag(1);
        this.addChild(this.conv_layer);
    }
});

and this is my layer:

var ConversationLayer = cc.Layer.extend({
    ctor: function() {
        this._super();
            this.init();
        },
        init: function() {
            this._super();

            var winSize = cc.director.getWinSize();
            GD.current_conversation = conversation1;

            this.background = new cc.Sprite();
            this.background.anchorX = 0.5;
            this.background.anchorY = 0.5;
            this.background.setPositionX(winSize.width / 2);
            this.background.setPositionY(winSize.height / 2);
            this.addChild(this.background);

            this.girl = new cc.Sprite();
            this.girl.anchorX = 0;
            this.girl.anchorY = 0;
            this.addChild(this.girl);

            this.text_background = new cc.Sprite(resources.conversation_interactive_assets, cc.rect(0,0,1920/GD.options.scale,320/GD.options.scale));
            this.text_background.anchorX = 0.5;
            this.text_background.anchorY = 0;
            this.text_background.setPositionX(winSize.width / 2);
            this.text_background.setPositionY(0);
            this.addChild(this.text_background);

            // Left
            this.conv_label = new cc.LabelBMFont("", resources.font);
            this.conv_label.anchorX = 0;
            this.conv_label.anchorY = 0;
            this.conv_label.setPositionX((winSize.width - this.text_background.width) / 2 + 20);
            this.conv_label.setPositionY(this.text_background.height - 30);
            this.conv_label.color = cc.color.BLACK;
            this.conv_label.setName('text');
            this.addChild(this.conv_label);
        }
});
1

1 Answers

0
votes

The issue was the loading order of everything.

It seems scenes are loaded in async, so the next function would be called but no layer would exist at that point.

Solved it by doing creation inside the class itself and calling onSceneEnter.