Copy link to clipboard
Copied
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.
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
...Copy link to clipboard
Copied
Hi. It may have something to do with scope - but would need to see how the file is arranged to know for sure.
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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++;
Copy link to clipboard
Copied
None of that code has anything to do with what the original poster was trying to accomplish a year ago.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Alternatively, they may have just been having issue with the this. portions which may have been resolved by doing this:
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now