PDA

View Full Version : decelerating spring effect?



joecastro
December 17th, 2003, 04:49 PM
does anybody know how to make a decelerating spring effect? what i want is to have a ball that goes to a target using the spring effect (ie it goes a bit further than the target and springs back a bit before a few times decreasing each time the distance - giving a spring effect) - but what i also want is that as it goes to the first position where it 'springs' back for the ball to decelerate!

I can do the spring - i can do the deceleration - but I dont know how to do the 2 together - :h:

// my spring effect -

onClipEvent (load) {
this._y = 200;
this._x = 100;
targetX = 300;
mover = 0;
loop = 0;
}
onClipEvent (enterFrame) {
if (_root.go == 1) {
mover = mover+(targetX-this._x)/2;
mover=mover *.6;
this._x = this._x+mover;
loop=loop+1;
_root.where=_x;
}
}


// my deceleration

onClipEvent (load) {
movedx = 0;
targetX = 300;
}


onClipEvent (enterFrame) {
distX = targetX-this._x;
totalDistance = Math.sqrt(distX*distX);
this._x += distX*speed;
if (totalDistance<1) {
this._x = targetX;
}
}


Any help much appreciated!

Clown Staples
December 22nd, 2003, 11:20 PM
not too difficult
you just have to set movetype ="spring" on the way out and
= "decel" on the way back

// my spring effect -

onClipEvent (load) {
this._y = 200;
this._x = 100;
targetX = 300;
mover = 0;
loop = 0;

movedx = 0;
targetX = 300;
}
onClipEvent (enterFrame) {
if ( movetype == "spring" ){

if (_root.go == 1) {
mover = mover+(targetX-this._x)/2;
mover=mover *.6;
this._x = this._x+mover;
loop=loop+1;
_root.where=_x;
}

}
else if (movetype=="decel"){
distX = targetX-this._x;
totalDistance = Math.sqrt(distX*distX);
this._x += distX*speed;
if (totalDistance<1) {
this._x = targetX;
}
}

}

Scootman
December 23rd, 2003, 01:22 AM
or maybe try the spring tutorial found... OH WAIT! RIGHT HERE ON KIRUPA.COM!!! how nifty is that? ;) (heres a link)

http://www.kirupa.com/developer/actionscript/spring.htm

joecastro
December 23rd, 2003, 08:20 AM
thanks everyone and merry christmas and happy new year - dont work too hard!