Groups | Blog | Home
all groups > flash actionscript > february 2006 >

flash actionscript : Shake/Vibrate Actionscript


brooklynconcept.com
2/7/2006 9:05:13 PM
I would like to place some actionscript code on an instance of a movieclip that
makes it shake or vibrate when rolledOver.

Does anyone have anything out there for this. Would be great if it had a timer
function too, so the shake time can be limited to a value specified.

THANKYOU!!!
abeall
2/7/2006 9:14:24 PM
you could try this very shakey function

MovieClip.prototype.shake = function(xamount,yamount){
this.shake = 0;
this.x = this._x;
this.y = this._y;
this.onEnterFrame = function(){
var d = -1;
++this.shake%2 ? d=-1 : d=1 ;
this._x = this.x+((xamount*d)/this.shake);
this._y = this.y+((yamount*d)/this.shake);
}
}


myMovieClip.shake(5,0);
NSurveyor
2/7/2006 9:47:27 PM
Perhaps something like this...

MovieClip.prototype.shakeIt = shakeIt;
TextField.prototype.shakeIt = shakeIt;

MYCLIP.onRollOver = function() {
this.shakeIt(10, 2);//shake once every 10 milliseconds, and move 2 px in an
up-right-left-down motion
this.stopShake(1000);//stop the shake after 1000 milliseconds (1 second)
};
MYCLIP.onRollOut = function() {
this.stopShake();//stop the shake immediately
};
function shakeIt(msps, size) {
var shakeF = function (obj, s) {
obj.cs = (obj.cs%5) ? obj.cs+1 : 1;
if (obj.cs == 1) {
obj._y += s;
obj._x += s;
} else if (obj.cs == 2) {
obj._x -= 2*s;
} else if (obj.cs == 3) {
obj._x += s;
obj._y += s;
} else if (obj.cs == 4) {
obj._y -= 2*s;
}
updateAfterEvent();
};
this.stopShake = function(ms) {
var stopF = function (mc) {
clearInterval(mc.stopID);
delete mc.stopID;
clearInterval(mc.shakeID);
delete mc.shakeID;
};
if (!ms) {
stopF(this);
} else {
clearInterval(this.stopID);
this.stopID = setInterval(stopF, ms, this);
}
};
this.stopShake();
this.shakeID = setInterval(shakeF, msps, this, size);
}
brooklynconcept.com
2/7/2006 10:14:39 PM
ah thankyou both!!!. I actually used your script NSurveyor and it works really
well for me. I tried to make it so that it shakes more vigorously at first and
then eases out of the shake, to mimic a mini explotion. Mine kind of work but
I'd like to see what you come up with in terms of building it in to your code.

Also, Id like to understand how your code works abeall if you have time can
you explain this? if not, thanks for your help!

thanks!!!

onClipEvent(load){
NUM1=4;
NUM2=8;

MovieClip.prototype.shakeIt = shakeIt;
TextField.prototype.shakeIt = shakeIt;

function shakeIt(spm) {
var curshake = function (obj) {
obj.currentshake = (obj.currentshake<4) ? obj.currentshake+1 : 1;
var currentshake = obj.currentshake;
if (currentshake == 1) {
obj._y += NUM1;
obj._x += NUM1;
} else if (currentshake == 2) {
obj._x -= NUM2;
} else if (currentshake == 3) {
obj._x += NUM1;
obj._y += NUM1;
} else if (currentshake == 4) {
obj._y -= NUM2;
}
};
clearInterval(this.shakeID);
this.shakeID = setInterval(curshake, spm, this);
this.stopShake = function(){
clearInterval(this.shakeID);
}
}
_root.preLoader.shakeIt(1);
}
onClipEvent(enterFrame){
if(_parent.smoke._currentframe>=_parent.smoke._totalframes){
_root.preLoader.stopShake();
}
NUM1-=0.5
NUM2-=1
}
abeall
2/7/2006 10:24:43 PM
[quoted text, click to view]

My code is very basic:

//create movieclip method
MovieClip.prototype.shake = function(xamount,yamount){
//how much it has shaken so far, starts at 0 of course
this.shaken = 0;
//define starting point
this.x = this._x;
this.y = this._y;
//onEnterFrame start shaking!
this.onEnterFrame = function(){
//define variable: dirrection(-1 for left, 1 for right)
var d;
//add 1 to shaken, and if even, shake left, if odd, shake right
++this.shaken%2 ? d=-1 : d=1 ;
//shake: starting location + (amount to shake / how long its been shaking
already)
//dividing by shaken time makes it fade, you could multiply that to make it
fade faster
this._x = this.x+((xamount*d)/this.shaken);
this._y = this.y+((yamount*d)/this.shaken);
}
}
//then you just call it on your MC, define how much to shake the x, how much
to shake the y
myMovieClip.shake(25,0);

//or

on(rollOver, release){
shake(25,0);
}

And I have no idea what NS's script does :p
AddThis Social Bookmark Button