-
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 ?
-
.
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 < 1 && shakey < 1)
{
btn_mc.x = 0;
btn_mc.y = 0;
}
btn_mc.x -= shakex;
btn_mc.y -= shakey;
}
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|