PDA

View Full Version : Rewriting AS code for some effects



mlk
August 2nd, 2005, 06:29 AM
I want to use buttons which look like they have a spring attached.

I'm quite happy with the effect, the problem is, each buttons is generating its on onClipEvent to check its distance from the mouse. I'd like to know if there would be a simpler way of doing this (so I wouldn't have to duplicate the function to each button). If the mouse could figure out when it's in a button area and start the effect on that button only...

It would alleviate some cpu usage I guess =)

Here's the code I use for each button right now:


onClipEvent (load) {
_root.springpower = 0.6;
_root.springdistance = 40;
this.origix = this._x;
this.origiy = this._y
this.buttonVar = "Portfolio";
}
onClipEvent (enterFrame) {
this.distance = Math.sqrt((_parent._xmouse-origix)*(_parent._xmouse-origix)+(_parent._ymouse-origiy)*(_parent._ymouse-origiy));
if (distance<=_root.springdistance) {
this._x = this._x+(_parent._xmouse-this._x)/_root.springpower;
this._y = this._y+(_parent._ymouse-this._y)/_root.springpower;
} else {
this._x = this.origix;
this._y = this.origiy;
}
}

cheers

mlk

Dauntless
August 2nd, 2005, 12:13 PM
You could use prototypes: (oh, and it's a bad habit to put actionscript ON your buttons.

MovieClip.prototype.addSpring = function(itemName:String) {
this.origix = this._x;
this.origiy = this._y;
this.buttonVar = itemName;
this.onEnterFrame = function() {
this.distance = Math.sqrt((this._parent._xmouse - this.origix)*(this._parent._xmouse - this.origix) +( this._parent._ymouse - this.origiy) * (this._parent._ymouse - this.origiy));
if (this.distance<=_root.springdistance) {
this._x = this._x+(this._parent._xmouse-this._x)/_root.springpower;
this._y = this._y+(this._parent._ymouse-this._y)/_root.springpower;
} else {
this._x = this.origix;
this._y = this.origiy;
}
};
};
this.springpower = 0.6;
this.springdistance = 40;
//Then u use it like this (ON A FRAME)
myButton1.addSpring("home");
myButton2.addSpring("Portfolio");
myButton3.addSpring("Contact");


I hope this helps :)