Adzi
Adzi

Reputation: 47

Helicopter trajectory formula for Actionscript three?

Right, I'm trying to implement this game, http://www.helicoptergame.net/ , but I'm struggling with the drop and the increase of the helicopter once the mouse is clicked.

I've set the helicopter as a symbol with instance name 'helicopter'.

Here is what I mean;

enter image description here

Surely there's a formula for the height increase, and decrease (unfortunately I'm not smart enough to figure it out)

It would be great if you could help me!

Thanks!

Upvotes: 0

Views: 134

Answers (2)

Ganaraj
Ganaraj

Reputation: 26841

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.

Refer : http://en.wikipedia.org/wiki/Equations_of_motion

Upvotes: 0

Marty
Marty

Reputation: 39456

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.


Sample Code:

Game.as (this would be your document class).

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);
        }
        
    }
}

Helicopter.as

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;
        }
        
    }
}

Upvotes: 1

Related Questions