Right, I'm trying to implement this game,
That game uses really simple physics; basically just subtracting a value from a velocity
property when you hold the mouse, otherwise adding to it.
The y
of the helicopter is increased by the velocity
(will being to rise once velocity
is below 0). This will also cause the gradual speeding up / slowing down effect that you see.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class Game extends MovieClip
{
// Properties.
private var _updating:Array = [];
private var _mouseDown:Boolean = false;
// Objects.
private var _helicopter:Helicopter;
/**
* Constructor.
*/
public function Game()
{
// Listeners.
stage.addEventListener(Event.ENTER_FRAME, _update);
stage.addEventListener(MouseEvent.MOUSE_DOWN, _mouseAction);
stage.addEventListener(MouseEvent.MOUSE_UP, _mouseAction);
// Helicopter.
_helicopter = new Helicopter();
stage.addChild(_helicopter);
}
/**
* Mouse action manager.
* @param e MouseEvent instance.
*/
private function _mouseAction(e:MouseEvent):void
{
switch(e.type)
{
default: _mouseDown = false; break;
case MouseEvent.MOUSE_DOWN: _mouseDown = true; break;
}
}
/**
* Update the game.
* @param e Event instance.
*/
private function _update(e:Event):void
{
_helicopter.update(_mouseDown);
}
}
}
package
{
import flash.display.Sprite;
public class Helicopter extends Sprite
{
// Properties.
private var _velocity:Number = 0;
/**
* Constructor.
*/
public function Helicopter()
{
// Temp graphics.
graphics.beginFill(0x00FF00);
graphics.drawRect(0, 0, 60, 35);
graphics.endFill();
}
/**
* Update the helicopter.
* @param mouseDown Boolean representing whether the mouse is held down or not.
*/
public function update(mouseDown:Boolean):void
{
if(mouseDown) _velocity -= 1;
else _velocity += 1;
y += _velocity;
}
}
}
velocity
. My code is more of a rough example. Do you need complete code? - Marty 2012-04-04 23:02
I remember playing that game so I think I understand the kind of motion it has implemented. Here is a brief idea of how to implement it yourself.
What you need is a simple formula in physics , This simple formula is as follows.
v = u+at;
Right. Thats your formula where v = final velocity u = initial velocity a = acceleration t = time.
Here time is your every step. The distance travelled at every step can be calculated using
s = u*t+ (0.5 * a*t*t);
s = distance travelled at a time step u = initial velocity t = time. a = acceleration ( which in your case will be either the gravity - which will come into effect when you leave the mouse. )
Each enter frame you see if the mouse is down, if the mouse is down for more than say 20 frames, after the 20th frame the acceleration starts going up by 1 unit ( you define the unit! ).
The same logic applies when you stop holding the mouse. After 20 frames ( or whatever time decay you define! ) the acceleration starts going down by 1 unit.