A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: This screen shaking code works in AS2 and it looks good ! How do translate this toAS3

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    22

    This screen shaking code works in AS2 and it looks good ! How do translate this toAS3

    Say you have a stage with a rectangular background that is the size of the stage [This is just to make it obvious that the screen shaking work], frame rate of document is 60fps.
    And a button, the button name is "btn_mc".

    On frame 1 of the main timeline, I enter this:

    btn_mc.onEnterFrame = function():Void
    {
    function shake()
    {
    shakex = random(20) - 10;
    shakey = random(20) - 10;
    shakex *= 0.7;
    shakey *= 0.7;
    if (shakex < 1 && shakey < 1)
    {
    _root._x = 0;
    _root._y = 0;
    }
    }
    setInterval(shake,100);
    _root._x -= shakex;
    _root._y -= shakey;
    };

    Test movie, wow it looks so good !
    I tried porting it to AS3 but the screen didn't shake, anyone here that can help ?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Mess around with this
    PHP Code:
    import flash.events.Event;

    var 
    shakex:Number;
    var 
    shakey:Number;

    btn_mc.addEventListener(Event.ENTER_FRAME,shake);

    function 
    shake(e:Event):void
    {
        
    shakex Math.floor(Math.random() * 20) - 10;
        
    shakey Math.floor(Math.random() * 20) - 10;
        
    shakex *=  0.7;
        
    shakey *=  0.7;
        if (
    shakex && shakey 1)
        {
            
    btn_mc.0;
            
    btn_mc.0;
        }
        
    btn_mc.-=  shakex;
        
    btn_mc.-=  shakey;


  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    22
    Thank you fruitbeard, your code is SO GOOD !
    Even better than the original code !

    I have broke it into two versions in case someday someone came here to study it:

    Code:
    ActionScript 2:
    var shakex:Number;
    var shakey:Number;
    
    _root.onEnterFrame = function():Void
    {
    	shake(this);
    };
    
    function shake(arg_object_to_shake):Void
    {
        shakex = Math.floor(Math.random() * 20) - 10;
        shakey = Math.floor(Math.random() * 20) - 10;
        shakex *=  0.7;
        shakey *=  0.7;
        if (shakex < 1 && shakey < 1)
        {
    		arg_object_to_shake._x = 0;
    		arg_object_to_shake._y = 0;
        }
    	arg_object_to_shake._x -=  shakex;
    	arg_object_to_shake._y -=  shakey;
    }
    
    =============================================================
    
    ActionScript 3:
    import flash.events.Event;
    
    var shakex:Number;
    var shakey:Number;
    
    root.addEventListener(Event.ENTER_FRAME,shake);
    
    function shake(event:Event):void
    {
        shakex = Math.floor(Math.random() * 20) - 10;
        shakey = Math.floor(Math.random() * 20) - 10;
        shakex *=  0.7;
        shakey *=  0.7;
        if (shakex < 1 && shakey < 1)
        {
            event.currentTarget.x = 0;
            event.currentTarget.y = 0;
        }
        event.currentTarget.x -=  shakex;
        event.currentTarget.y -=  shakey;
    }

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