Game Development Reference
In-Depth Information
And these are the private attributes:
private var xChange:int : The difference between the mouseY value and the object's y
value
private var yChange:int : The difference between the mouseX value and the object's x
value
private var radians:Number : Holds the degree in radians for the current rotation of the
player
private var degrees:int : Holds the degrees for the current rotation of the player
private var drawingCanvas:Shape : Constructs the vector ship look and shield before
they are drawn in the shipBitmapData variable
And there are three public functions.
First, the constructor is identical to the BasicBlitArrayParticle version.
This is the second function:
public function update(mousePositionX:Number, mousePositionY:Number, delay:int,
timeBasedModifier:Number=1):void
This function takes in the current mouse x and y positions as well as a delay value. As mentioned
before, the delay is used to control the speed of the player ship. The timeBasedModifier will be
used to keep objects moving at a constant rate per second no matter at what frame rate the game
is running.
The new rotation and movement change values are calculated for the ship based on these three
passed in values. Here is the algorithm:
1.
We calculate the radian value for the new rotation of the ship using the difference
between the mouse x and y positions and the player's x and y positions. This uses the
Math.atan2 function.
2. Next we calculate the degrees ( 0 - 360 ) value of the newly calculated radian value:
radians = Math.atan2((mousePositionY)-y,(mousePositionX)-x);
degrees= (radians * (180 / Math.PI));
3. Then, we calculate the difference in the x and y values between the mouse and player:
yMove=(yChange/delay)*timeBasedModifier;
xMove=(xChange/delay)*timeBasedModifier;
4.
The xMove and yMove values are multiplied by the timeBasedModifier . This calculation
keeps the distance that objects move in a single second constant no matter what
frame rate the game SWF is playing at. This concept was be discussed further in the
”Creating the time-based step timer” section.
5. Next, we apply the difference divided by a delay so the ship doesn't just attach itself to
the mouse:
yMove=yChange/delay;
xMove=xChange/delay;
Search WWH ::




Custom Search