Some help accessing a dynamic text box from an external class js/html5/adobe animate cc

Hi team,
Can you please help me out with accessing a dynamic text field on the stage of an animate cc project - exporting to HTML5 Canvas?
I can get this code to work

class Game{
	constructor(stage, root){
		this.stage = stage;
		this.root = root;
		
		this.init();
   
	}
	
	init(){
        this.mainText = this.root.mainText;
		const game = this;
		createjs.Ticker.addEventListener("tick", function(){ game.update(); })
		
		this.newGame();
	}
	
newGame(){
		var str = "hello world";
    
    
		this.mainText.text = str;
	}
}

createjs.Ticker.addEventListener(“tick”, function(){ game.update(); }) this function is throwing an error saying that the game.update() isn’t a function - it’s not defined. But if I try to remove it the whole thing doesnt work. The text box is a dynamic text box in the main project, and this code works at the moment - despite the error- When I say it works I mean that there is “hello world” in the text box.
How can I get rid of the error and why won’t this code let me create the str variable at the beginning of the code. Ideally I want to have a function check the state of the story and then assign the correct text to the mainText box somewhere else in the code.

thanks for any help, pointers or advice
sub

Well, thats true.
game is a member of the Game class, for which there are three functions defined: the constructor, init, and newGame. None of those three functions are called “update”. Did you perhaps mean to say game.newGame() ?

ok thanks for that- it makes sense

class Game{
	constructor(stage, root){
		this.stage = stage;
		this.root = root;
		
		this.init();
   
	}
	
	init(){
        this.mainText = this.root.mainText;
		const game = this;
		createjs.Ticker.addEventListener("tick", function(){ game.update(); })
		
		
	}
	
update(){
		var str = "hello world";
    
    
		this.mainText.text = str;
	}
}

So this is better! But I still cant create that variable anywhere else- if I try to put this.str (or let or var) in the constructor it won’t work.

I’m not sure what you’re trying to do specifically. If you’re making a string that will be static to all instances of the class, you could declare it as such;

class Game{
   static str = "Hello World";

If it’s an instance variable, you can declare it in the constructor:

	constructor(stage, root){
		this.stage = stage;
		this.root = root;
        this.str = "Hello World";
class Game{
	constructor(stage, root){
		this.stage = stage;
		this.root = root;
		this.Brr = "Help";
		this.init();
   
	}
	
	init(){
        this.mainText = this.root.mainText;
		const game = this;
		createjs.Ticker.addEventListener("tick", function(){ game.update(); })
        this.setState = 0;
		
		
	}
	
update(){
		if (this.setState == 0 ){
            this.str = "yes";
        }
    this.setText();
	}
    setText(){
        this.mainText.text = this.str;
        console.log(Brr);
    }
}

so this works but throws an error saying that it cant find variable Brr in line 27 at the console

So here you define Brr.

Here you define str (though be careful, str may not exist, because you’ve put this definition in an if statement):

Here you use str, and it works:

Here you try to use Brr, and it doesnt.

What’s the difference between the last 2 codeblocks?

1 Like

I see it!!! this.Brr. :slight_smile:

2 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.