Skip to main content
Participant
Mar 15, 2022 9:24 AM UTC
Answered

Passing variable to a graphic symbol instance

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 is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Mar 15, 2022 9:51 AM UTC

    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

    5 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Mar 15, 2022 9:51 AM UTC

    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

    Translate
    Participant
    Mar 15, 2022 8:36 PM UTC

    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'.

    Translate
    JoãoCésar17023019
    Community Expert
    Community Expert
    Mar 15, 2022 10:04 PM UTC

    Hi.

     

    Your code should've worked.

     

    Are you logging from within some function?

    Translate