package
tools {
import
flash.display.DisplayObject;
import
flash.events.TimerEvent;
import
flash.utils.Timer;
public
class
Earthquake {
private
var
_timer:Timer;
private
var
_displayObject:DisplayObject;
private
var
_originalX:
int
;
private
var
_originalY:
int
;
private
var
_intensity:
int
;
private
var
_intensityOffset:
int
;
private
var
_seconds:
Number
;
public
static
const
LEFT_RIGHT_UP_DOWN:
int
=
0
;
public
static
const
LEFT_RIGHT:
int
=
1
;
public
static
const
UP_DOWN:
int
=
2
;
public
static
const
FRAME_RATE:
int
=
60
;
private
var
_direction:
int
;
private
var
_bShaking:
Boolean
=
false
;
private
var
_bDestroy:
Boolean
=
false
;
public
function
Earthquake(displayObject:DisplayObject, intensity:
Number
=
10
, seconds:
Number
=
1
, direction:
int
=
0
, autoDestroy:
Boolean
=
false
):
void
{
_displayObject = displayObject;
_originalX = displayObject.x;
_originalY = displayObject.y;
_intensity = intensity;
_intensityOffset = intensity /
2
;
_secOnds= seconds;
_direction = direction;
_bDestroy = autoDestroy;
}
public
function
go():
void
{
if
(_bShaking)
return
;
_bShaking =
true
;
var
msPerUpdate:
int
=
int
(
1000
/ FRAME_RATE);
var
totalUpdates:
int
=
int
(_seconds *
1000
/ msPerUpdate);
if
(!_timer)
{
_timer =
new
Timer(msPerUpdate, totalUpdates);
_timer.addEventListener(TimerEvent.TIMER, quake);
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, resetImage);
}
else
{
_displayObject.x = _originalX;
_displayObject.y = _originalY;
_timer.reset();
}
_timer.start();
}
private
function
quake( event:TimerEvent ):
void
{
var
newX:
int
= _originalX;
var
newY:
int
= _originalY;
switch
(_direction)
{
case
LEFT_RIGHT_UP_DOWN:
newX = _originalX + Math.random() * _intensity - _intensityOffset;
newY = _originalY + Math.random() * _intensity - _intensityOffset;
break
;
case
LEFT_RIGHT:
newX = _originalX + Math.random() * _intensity - _intensityOffset;
break
;
case
UP_DOWN:
newY = _originalY + Math.random() * _intensity - _intensityOffset;
break
;
}
_displayObject.x = newX;
_displayObject.y = newY;
}
private
function
resetImage( event:TimerEvent =
null
):
void
{
_displayObject.x = _originalX;
_displayObject.y = _originalY;
cleanup();
}
private
function
cleanup():
void
{
_bShaking =
false
;
_timer.reset();
if
(_bDestroy)
{
_timer.removeEventListener(TimerEvent.TIMER, quake);
_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, resetImage);
_timer =
null
;
_displayObject =
null
;
}
delete
[
this
];
}
}
class="line number141 index140 alt2">}