-
Dance Monkey Dance!
[F8] Shake a movieclip
Hey peeps, was wondering if you could help me out. Im trying to shake a MovieClip. What I mean by that is you know when an explosion goes off and the entire screen shakes for a brief moment. I thought it would be simple but for some reason I cant seem to think of how to do it properly. I have tried using springing which I couldnt get to work properly. Then I tried this, which looks similar to what I want but this just ends up being the same explosion over and over and in the same direction. I have a movieclip the size of the stage, with it registration point at the top left corner which is then located on the main stage at (0,0).
Code:
var shakeSize:Number = 10;
var shake:Number = true;
onEnterFrame = function() {
if(this.shake) {
this.size *= -1;
_root.cont._x += this.size;
_root.cont._y += this.size;
}
}
That shakes on the same diagonal axis continually, i get why it does that. What I want is a more random shake. Now I think using springing will achieve this effect but I cant figure out what the _root.cont is springing to and how to make it spring in all different directions until it slows down. Which then I will set shake to false.
I hope this makes sense.
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
Senior Member
Maybe :
PHP Code:
var origX = this._x;
var origY = this._y;
onEnterFrame = function () {
this._x = origX + Math.round(Math.random()*10) -5;
this._y = origY + Math.round(Math.random()*10) -5;
}
Complete example :
http://fil.razorback.free.fr/tmp/shakeMC.fla
Last edited by fil_razorback; 11-05-2006 at 08:16 AM.
-
file not found
quick kind of off-topic question about the variables (in the original code)
Code:
var shake:Number = true;
is true a number?
is that action "legal"?
I'm asking because I honestly don't know.
-
Script kiddie
I think 'true' as a value of 1, doesn't it? Maybe that's the same as saying var shake:Number = 1; ? But yes, I'm also curious.
-
Senior Member
I think it'd be cleaner to write
var shake:Boolean = true;
but knowing that trace(Number(true)) outputs 1...I suppose it's ok
-
I suggest taking advantage of the Math.random() function.
Also, I think true = 0 and false = -1 or that may just be in Visual Basic.
-
Script kiddie
I believe that in Flash, true is 1 and false is 0.
-
self-portrait
(still of topic) yes, true evaluates to 1 and false to 0. Although it is not a very clean/ good way of coding. For instance, trace( 5 *true +2 *false ) will output "5".
-
file not found
'k thanks guys.
btw, if you trace(0+true) it equals 1, kind of the ultimate proof there...
anyway, back to the topic at hand.
-
Dance Monkey Dance!
LOL first of all I quickly typed the above code up as an example and I normally do var shake:Boolean = true; just accidently did Number :S but its useful if its a variable that you need to change to a number down the track rather then declare it as Object. But honestly it was a mistake. And I didnt know you could do it
fil_razorback : Thanks for your help, it works a treat and isnt to intensive. I think the use of springs in my game with everything else running would have bogged the game down a bit. Thanks again. Really appretiate it.
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
Originally Posted by VENGEANCE MX
I believe that in Flash, true is 1 and false is 0.
Yup, but not just flash, basically every programming language ever. Boolean is the basic way PC's work. 1's and 0's...The hard drive gets north and south poles magnetically set on it, north is a 1, and south is 0, true false, yes no etc...
-
Zombie Coder
That's effin' sweet fil_razorback. I'm using that in my game now.
add:
violence=violence < 0 ?0: violence-=0.1;
at the end of the function to get it to gradually filter off.
-
M.D.
how about: violence *= .9
-
Dance Monkey Dance!
So like this ?
PHP Code:
var shakeSize:Number = 10;
var origX:Number = this._x;
var origY:Number = this._y;
onEnterFrame = function () {
this._x = origX + Math.round(Math.random()*shakeSize) -5;
this._y = origY + Math.round(Math.random()*shakeSize) -5;
this.shakeSize *= 0.9;
}
Doesn't really seem to look right? Would it have to shake for a certain amount of time before it reached zero? Or would it start to go the other way if it was shaking too long?
Will at some point it = 0?
Thanks guys
Last edited by Doush.; 12-07-2006 at 12:10 AM.
"I layed down in my bed last night looked up at the stars, and thought to myself... Where the F*#K is my roof"
-
Senior Member
I use this in my latest game. I modified the code for x and y instead of scaling. "The scaling made people sea sick"
elasticX
Code:
r=_root
//you can change the stagex "y to any # you want.
r._x=stagex
r._y=stagey
xSx=0;
ySy=0;
onEnterFrame = function () {
if (_x != stagex|| _y != stagey) {
elasticX(r, stagex, stagey, 0.8, .2);
}
};
elasticX = function (mc, targetx, targety, accel, convert) {
xSx = xSx*accel+(targetx-mc._x)*convert;
ySy = ySy*accel+(targety-mc._y)*convert;
mc._x += xSx;
mc._y += ySy;
};
//how to use you (don't need to copy this)
//this will shake Left and right
elasticX(r, 100, stagey, 0.8, .2);
//this will shake up and down
elasticX(r, stagex, 100, 0.8, .2);
just call the code once in the death of the baddy. It gives a really nice effect. you can change the accel and convert to get less or more of the elastic feeling.
Get the FLA @ my site
zervell/examples
Last edited by zervell; 12-06-2006 at 09:16 AM.
CEO OF
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|