A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [F8] Shake a movieclip

  1. #1
    Dance Monkey Dance! Doush.'s Avatar
    Join Date
    Jun 2004
    Posts
    254

    [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"

  2. #2
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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.

  3. #3
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    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.

  4. #4
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    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.
    http://www.birchlabs.co.uk/
    You know you want to.

  5. #5
    Senior Member fil_razorback's Avatar
    Join Date
    Jul 2005
    Location
    Paris, France
    Posts
    607
    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

  6. #6
    Member
    Join Date
    Nov 2005
    Posts
    32
    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.

  7. #7
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    I believe that in Flash, true is 1 and false is 0.
    http://www.birchlabs.co.uk/
    You know you want to.

  8. #8
    self-portrait Kianis's Avatar
    Join Date
    Feb 2004
    Location
    Stockholm, Sweden
    Posts
    425
    (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".
    // Mazapán, my portfolio

  9. #9
    file not found Captain_404's Avatar
    Join Date
    Apr 2006
    Posts
    457
    '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.

  10. #10
    Dance Monkey Dance! Doush.'s Avatar
    Join Date
    Jun 2004
    Posts
    254
    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"

  11. #11
    Member
    Join Date
    Nov 2006
    Posts
    93
    Quote 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...

  12. #12
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    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.

  13. #13
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    how about: violence *= .9
    lather yourself up with soap - soap arcade

  14. #14
    Dance Monkey Dance! Doush.'s Avatar
    Join Date
    Jun 2004
    Posts
    254
    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"

  15. #15
    Senior Member zervell's Avatar
    Join Date
    May 2004
    Posts
    259
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center