|
|
|
#1 |
|
Junior Member
Join Date: Feb 2006
Posts: 19
|
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;
}
}
}
Code:
onClipEvent(enterFrame)
{
shake(3, 1)
}
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 :/ |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Jun 2005
Location: Mid-Eastern U.S.
Posts: 161
|
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;
}
}
}
}
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)
}
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Feb 2006
Posts: 19
|
Aahh! Yea that works perfectly thanks, knew it'd be something I was missing.
Appreciate the quick reply
|
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Aug 2001
Location: uk
Posts: 11,221
|
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);
}
__________________
Antonetti in '09 |
|
|
|
![]() |
|
||||||
| Thread Tools | |
| Display Modes | |
|
|