-
function problem
This is becoming rather frustrating as its probably a simple problem I just can't see it and I need an extra pair of eyes.
This function will make a movie clip rotate backwards and forwards once the counter variable hits a certain number (the variable 'delay'), and rotate at a certain speed ('speed') - which will either be 1 or 2)).
Problem is I can't seem to get the individual movieclip to respond without writing the function on the movieclip itself, the function is currently on the main timeline.
Code I'm Using:
(On the Main Timeline)
Code:
function shake(delay, speed)
{
if(_root.counter > delay)
{
if(anticlock == 0)
{ this._rotation += speed; }
else
{ this._rotation -= speed; }
if(this._rotation == 4)
{
anticlock = 1;
}
else if(this._rotation == -4)
{
anticlock = 0;
}
}
}
then on the movieclip i'm simply putting:
Code:
onClipEvent(enterFrame)
{
shake(3, 1)
}
...which doesn't work. If I put the function on top of the movieclip and then call the function then it does work.
I've tried using
_root.shake(3, 1);
and that works but it shakes the entire movie not the clip.
this.shake(x, x)
doesn't work either... and this._root.shake(x, x) .. shakes the entire movie as well.
... what am I doing wrong :/
-
You need to use _root.shake(3,1) , not shake(3,1) .
The reason that it shakes the entire movie is because the function is defined on the main timeline. Therefore, "this" inside the function referrs to the main timeline (A.K.A. the whole movie), regardless of what movieclip calls the function.
I suggest changing your code to this:
Code:
function shake(delay, speed, targetMC)
{
with(targetMC){
if(_root.counter > delay)
{
if(anticlock == 0)
{ _rotation += speed; }
else
{ _rotation -= speed; }
if(_rotation == 4)
{
anticlock = 1;
}
else if(_rotation == -4)
{
anticlock = 0;
}
}
}
}
I'm pretty sure this should work.
What this does is gives the function the movieClip that needs to be shaken in a third parameter, targetMC. You also need to either replace all "this"'s in the function with "targetMC", or just delete them and put the whole thing inside a with() statement like I did.
Therefore, since a third parameter was added, the code on the movieClip is:
Code:
onClipEvent(enterFrame)
{
shake(3, 1, this)
}
This will send the movieClip as the third parameter, since "this" referrs to the movieClip when used in the movieClip.
-
Aahh! Yea that works perfectly thanks, knew it'd be something I was missing.
Appreciate the quick reply :D
-
Some extra info...
Adding the 3rd parameter to the function is fine, but you don't have to do it - flash provides 2 methods that allow you to use a function as a method of some other object (giving control over what "this" will be reference to) - someFunction.call and someFunction.apply
For example,
Code:
onClipEvent(enterFrame) {
_root.shake.call(this, 3, 1);
}
would call the function shake (defined on _root) but within that function "this" would be a reference to the clip that has the onEnterFrame event - the 1st argument to the call method sets what "this" will be when used in the function.