# Rewriting AS code for some effects

**URL:** <https://forum.kirupa.com/t/rewriting-as-code-for-some-effects/157485>\
**Category:** flash\
**Created:** [August 2, 2005, 10:29am UTC](https://forum.kirupa.com/t/rewriting-as-code-for-some-effects/157485 "2005-08-02T10:29:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlk](https://avatars.discourse-cdn.com/v4/letter/m/e56c9b/32.png) [@mlk](https://forum.kirupa.com/u/mlk)\
**Post date:** [August 2, 2005, 10:29am UTC](https://forum.kirupa.com/t/rewriting-as-code-for-some-effects/157485/1 "2005-08-02T10:29:27Z")

</div>

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:

```auto
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
