In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Simple Projectile + Prediction

-small test to prediction. seems to work fine by one assignment of the projectile's velocity. assigning the velocity as the target travels is undesired, although accurate; if the target maintains a constant speed then the projectile will always successfully intersect via one assignment.

single assignment:
projectile velocity = ((target position / projectile speed) + target velocity)

projectile speed modifies the projectile velocity which is based on the target's position and velocity. any speed you desire will cause the result of the above equation to compensate for such and correctly predict with any speed (as long as both projectile & target speeds are at a constant; eg. real world projectile prediction, where your ballistic does not have the physical means to change it's path mid-flight)

wish there was a 3d graphics library so I could write this simply on a 3d canvas

added hit test validation

purposely broken, just use the equation above
Get Adobe Flash player
by hemingway 19 Mar 2013
    Embed
/**
 * Copyright hemingway ( http://wonderfl.net/user/hemingway )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dtCq
 */

package
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    
    [SWF(frameRate=60, width=1000, height=465)]    
    public class FlashTest extends Sprite
    {
        private var _unit :Unit;
        private var _enemy :Enemy;
        
        public function FlashTest()
        {
            _unit = new Unit(233, 233);
            _enemy = new Enemy(433, 433);
            
            _unit.target = _enemy;
            
            addChild(_unit);            
            addChild(_enemy);
        }
    }
}

import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.text.*;

class Unit extends Sprite
{
    protected var _x :Number;
    protected var _y :Number;
    protected var _vX :Number;
    protected var _vY :Number;
    protected var _tar :Enemy;
    protected var _speed :Number;
    protected var _radius :Number;
    protected var _status :Boolean;
 
    public function Unit($x:Number, $y:Number, $speed:Number = 64)
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    
        _x = $x;
        _y = $y;
        _vX = 0;
        _vY = 0;
        _tar = null;
        _speed = $speed; //greater = slower
        _radius = 2;
        _status = true;
    }

    public function addedToStage($e:*) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

        _vX = (_tar._x/_speed) + _tar.vX;
        _vY = (_tar._y/_speed) + _tar.vY;
        
        init();
        
        addEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    public function init() :void
    { 
        graphics.clear();
        graphics.lineStyle(1);
        graphics.drawCircle(_x, _y, (_radius*2));
        if (_tar != null)
        {
            graphics.lineStyle(1, 0xCCCCCC);
            //graphics.moveTo(_x, _y);
            //graphics.lineTo(_tar._x+(_tar._vX*_speed), _tar._y+(_tar._vY*_speed));
        }
    }
    
    public function projectile() :void
    {
        if (_status)
        {
            //(((_tar.x - (_tar.radius*1.45)) - (_x + (_radius*1.45))) < _vX) ? (_x += ((_tar.x - (_tar.radius*1.45)) - (_x + (_radius*1.45))), rotation*=180) : _x += _vX; 
            //(((_tar.y - (_tar.radius*1.45)) - (_y + (_radius*1.45))) < _vY) ? (_y += ((_tar.y - (_tar.radius*1.45)) - (_y + (_radius*1.45))), rotation*=180) : _y += _vY;
        }else{
            visible = false;
        }
        
        x += _vX/2;
        y += _vY/2;
    }
    
    public function onFrame($e:*) :void
    {
        if (_tar != null)
        {
            projectile();
        }
        
        init();
    }

    public override function get x() :Number
    {return _x}
    public override function get y() :Number
    {return _x}
    public function get vX() :Number
    {return _vX}
    public function get vY() :Number
    {return _vY}
    public function get radius() :Number
    {return _radius}
    public function get target() :Enemy
    {return _tar}
    
    public override function set x($:Number) :void
    {_x = $; init()}
    public override function set y($:Number) :void
    {_y = $; init()}
    public function set vX($:Number) :void
    {_vX = $; init()}
    public function set vY($:Number) :void
    {_vY = $; init()}
    public function set radius($:Number) :void
    {_radius = $; init()}
    public function set target($:Enemy) :void
    {_tar = $}
}

class Enemy extends Sprite
{
    public var _x :Number;
    public var _y :Number;
    public var _vX :Number;
    public var _vY :Number;
    protected var _radius :Number;
    
    public function Enemy($x:Number, $y:Number)
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStage);
    
        _x = $x;
        _y = $y;
        _vX = 5;
        _vY = -10;
        _radius = 10;
    }
    
    public function addedToStage($e:*) :void
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
        
        init();
        
        addEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    public function init() :void
    {
        graphics.clear();
        graphics.lineStyle(1, 0xFF0000);
        graphics.drawCircle(_x, _y, (_radius*2));
    }
    
    public function onFrame($e:*) :void
    {
        _x += _vX;
        _y += _vY;
        
        init();
    }
    
    public override function get x() :Number
    {return _x}
    public override function get y() :Number
    {return _x}
    public function get vX() :Number
    {return _vX}
    public function get vY() :Number
    {return _vY}
    public function get radius() :Number
    {return _radius}
    
    public override function set x($:Number) :void
    {_x = $; init()}
    public override function set y($:Number) :void
    {_y = $; init()}
    public function set vX($:Number) :void
    {_vX = $; init()}
    public function set vY($:Number) :void
    {_vY = $; init()}
    public function set radius($:Number) :void
    {_radius = $; init()}
}