Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Animate CC HTML5 Canvas: Fire function on frame access?

Engaged ,
Mar 15, 2017 Mar 15, 2017

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.

3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 15, 2017 Mar 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 expres

...
Translate
Community Expert ,
Mar 15, 2017 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2017 Mar 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; 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2017 Mar 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2018 Mar 05, 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++;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 05, 2018 Mar 05, 2018

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2018 Mar 05, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 05, 2018 Mar 05, 2018

Alternatively, they may have just been having issue with the this. portions which may have been resolved by doing this:

  1. var that = this;
  2. function reset_mark() { 
  3. that.mark.x = 1200
  4. that.mark.y = -15
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 09, 2018 Oct 09, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 09, 2018 Oct 09, 2018
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines