Skip to main content
jeffery wright
Inspiring
March 15, 2017
Answered

Animate CC HTML5 Canvas: Fire function on frame access?

  • March 15, 2017
  • 4 replies
  • 3722 views

I have a function that resets the position of a MC, that does not do what I expect:

function reset_mark() {

this.mark.x = 1200;

this.mark.y = -15;

}

Which I call from various frames via:

reset_mark();

I have also tried:

this.reset_mark();

MovieClip(root).reset_mark();

this.MovieClip(root).reset_mark();

But none seem to work... am I missing something painfully obvious? I wouldn't be surprised.

Thanks.

    This topic has been closed for replies.
    Correct answer ClayUUID

    Or you could skip the redirection and do this instead...

    this.reset_mark = function() { 

         this.mark.x = 1200;

         this.mark.y = -15;

    this.reset_mark();

    Scope refresher:

    Global

    bla = "foo";

    window.bla = "foo"; // better, because it makes intended global scope explicit

    Current TImeline

    this.bla = "foo";

    Current Frame

    var bla;

    var bla = "foo";

    Declaring a function using the function statement applies the same scope as using var, so it's confined to the scope of the current frame. Using a function expression instead and assigning it as a property of this allows it to be accessible to an entire timeline.

    javascript scope of function declarations - Stack Overflow

    4 replies

    jeffery wright
    Inspiring
    October 9, 2018

    Thanks everyone... I am used to declaring a function this way:

    function someFunction(){

    };

    So yes:

    this.someFunction=function(){

    }

    and calling it:

    this.someFunction();

    did the trick... it seems backwards to code it that way, I wonder why variables are not coded similarly?

    this.myVariable=var; for example.

    Thanks again for all your help!

    Legend
    October 9, 2018

    https://forums.adobe.com/people/jeffery+wright  wrote

    did the trick... it seems backwards to code it that way, I wonder why variables are not coded similarly?

    Variables are coded similarly. Almost identically, in fact.

    These declare a variable and a function as private to the local scope:

    var myVar = "foo";

    function myFunction() {

         alert("bar");

    };

    These assign a variable and a function as a public property and method, respectively, of an object:

    myObj.myVar = "foo";

    myObj.myFunction = function() {

         alert("bar");

    };

    The former uses a function declaration, while the latter uses a function expression.

    markr17328370
    Participant
    March 5, 2018

    If you are trying to reach a global scope, which I think you are, meaning you want to place this function on one key frame and fire it from a different key frame or layer.  Re write your code like this:

    --- write these things this way for local ---

    var foo = bar;

    var count = 0;

    function foobar(){

    // do stuff;

    }

    foobar();

    count++;

    --- write these things this way for global ---

    foo = bar;

    count = 0;

    foobar = function(){

    // do stuff;

    }

    foobar();

    count++;

    Legend
    March 5, 2018

    None of that code has anything to do with what the original poster was trying to accomplish a year ago.

    markr17328370
    Participant
    March 5, 2018

    ClayUUID  wrote

    None of that code has anything to do with what the original poster was trying to accomplish a year ago.

    I have also tried:

    this.reset_mark();

    MovieClip(root).reset_mark();

    this.MovieClip(root).reset_mark();

    To me it appears that they were not able to access the function by not utilizing the proper scope given the posters attempts of change.  Now if they were modifying the x or y values to achieve proper placement then I would agree with you.  But that was not what the poster was attempting to change.

    Colin Holgate
    Inspiring
    March 15, 2017

    You can solve the problem like this:

    this.reset_mark = reset_mark;

    this.reset_mark();

    function reset_mark() { 

    this.mark.x = 1200; 

    this.mark.y = -15; 

    ClayUUIDCorrect answer
    Legend
    March 15, 2017

    Or you could skip the redirection and do this instead...

    this.reset_mark = function() { 

         this.mark.x = 1200;

         this.mark.y = -15;

    this.reset_mark();

    Scope refresher:

    Global

    bla = "foo";

    window.bla = "foo"; // better, because it makes intended global scope explicit

    Current TImeline

    this.bla = "foo";

    Current Frame

    var bla;

    var bla = "foo";

    Declaring a function using the function statement applies the same scope as using var, so it's confined to the scope of the current frame. Using a function expression instead and assigning it as a property of this allows it to be accessible to an entire timeline.

    javascript scope of function declarations - Stack Overflow

    Joseph Labrecque
    Community Expert
    Community Expert
    March 15, 2017

    Hi. It may have something to do with scope - but would need to see how the file is arranged to know for sure.