Skip to main content
Participant
March 15, 2022
Answered

Passing variable to a graphic symbol instance

  • March 15, 2022
  • 1 reply
  • 284 views

Hi there,

New to Adobe Animate. Is there a way to pass variables to instances created via code?

 

I've created a symbol that moves left 12 frames and then right 12 frames back to the same spot.

When creating an instance using the Linkage property and the code:

var example = new lib.linkageText();
this.addChild(example);

is there a way I can pass a variable through to the instance?

For example, is there a way to pass a variable that is the speed for how fast it moves left then right? Something like:

var example = new lib.linkageText(100);
this.addChild(example);

 

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    Hi.

     

    To deal with constructor arguments you would have to deal with prototypes and other ES5 advanced concepts.

     

    In your case, an easier approach would be to set a property for your instance when you instantiate it:

    var example = new lib.linkageText();
    example.x = 200;
    example.y = 200;
    example.speed = 30; // custom property
    this.addChild(example);

     

    Then you can have a code that reads this property inside of the instance you just created.

     

    I hope it makes sense.

     

    Regards,

    JC

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    March 15, 2022

    Hi.

     

    To deal with constructor arguments you would have to deal with prototypes and other ES5 advanced concepts.

     

    In your case, an easier approach would be to set a property for your instance when you instantiate it:

    var example = new lib.linkageText();
    example.x = 200;
    example.y = 200;
    example.speed = 30; // custom property
    this.addChild(example);

     

    Then you can have a code that reads this property inside of the instance you just created.

     

    I hope it makes sense.

     

    Regards,

    JC

    Participant
    March 15, 2022

    Hi JC,

    This makes sense, but do I need to define the property somewhere in the symbol?

    I added your code above:

    var example = new lib.linkageText();
    example.x = 200;
    example.y = 200;
    example.speed = 30; // custom property
    this.addChild(example);

    Then in frame 1 of the symbol, in the Actions, I did:

    console.log(speed)

    and then tried

    console.log(this.speed)

    But when I look at the console, it has 'Uncaught ReferenceError: speed is not defined'.

    JoãoCésar17023019
    Community Expert
    Community Expert
    March 15, 2022

    Hi.

     

    Your code should've worked.

     

    Are you logging from within some function?