To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash Newbies

Reply
 
Thread Tools Display Modes
Old 03-13-2006, 08:15 PM   #1
Ryu85
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;
	}

}
}
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 :/
Ryu85 is offline   Reply With Quote
Old 03-13-2006, 09:20 PM   #2
Bernz
Senior Member
 
Bernz's Avatar
 
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;
			}
		}
	}
}
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.
Bernz is offline   Reply With Quote
Old 03-13-2006, 10:23 PM   #3
Ryu85
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
Ryu85 is offline   Reply With Quote
Old 03-14-2006, 04:50 AM   #4
catbert303
Senior Member
 
catbert303's Avatar
 
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);
}
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.
__________________
Antonetti in '09
catbert303 is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash Newbies

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:55 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.