Jump to content

Text losing properties after setText()


momenimum
 Share

Recommended Posts

After I create a Text with properties, those properties are lost after I setText().  Let me know if I am doing something incorrectly.  Thanks!

//This gets centered
this.label = new game.Text('SCORE 99999\nCongrats!',{align:'center'});

//No longer centered
this.label.setText('SCORE '+score+'\nCongrats!');

 

Link to comment
Share on other sites

I can't reproduce that one, are you doing anything else to the text other that calling setText?

This code seem to work as expected:

game.createScene('Main', {
    init: function() {
        this.text = new game.Text('This text is\ncentered', { align: 'center' });
        this.text.addTo(this.stage);
    },
    
    click: function() {
        this.text.setText('This text is still\ncentered');
    }
});

textcenter.gif.6ed09b9d0d36346f99b76f1204e819f3.gif

Link to comment
Share on other sites

Hi @enpu please take a look at this sample, the setText() makes the text blank.  I believe its related to maxWidth, because it works without maxWidth.

game.addAsset('font.fnt');

game.createScene('Main', {
    init: function() {
        this.text = new game.Text("Hello Panda Banda!\nHow are you today?",{maxWidth:300});
        this.text.addTo(this.stage);
        game.Timer.add(1500,function() {this.nextCall()}.bind(this));
    },
    
    nextCall: function() {
        this.text.setText("Line 1\nLine 2");
    }
});

 

Link to comment
Share on other sites

Thanks, this should be fixed now!

settext.gif.47c6c525e13968084f82e07b09bb5c4e.gif

Btw, you are creating unnecessary function in your Time line:

game.addAsset('font.fnt');

game.createScene('Main', {
    init: function() {
        this.text = new game.Text('Hello Panda Banda!\nHow are you today?', { maxWidth: 300 });
        this.text.addTo(this.stage);
        game.Timer.add(1500, this.nextCall.bind(this));
    },
    
    nextCall: function() {
        this.text.setText('Line 1\nLine 2');
    }
});

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...