In my continued quest to understand the use of vectors I have put together this little game that has some of the rocket thrust fun of the trendy iPhone game Jetpack. I wasn’t planning on doing it as such, but after watching the animation generated, I realized it had a similar feel. All I was planning to work on was playing around with adding a gravity vector to the movement, and before I knew it, the ball became a ship and then a jetpack that even required a thruster animation.
Check out the Demo.
Jetpack Vectors
[iframe src=”http://www.theartboy.net/demos/loadFlash/loadFlash.php?path=../jetpack/ball.swf&width=550&height=400″ width=”550″ height=”400″]
[as3]
package {
import flash.display.MovieClip;
import flash.events.Event;
import com.foed.Vector2D;
import flash.events.MouseEvent;
public class BallMain extends MovieClip {
private var ball:GreenBall;
private var g:Vector2D;
private var c:Vector2D;
private var v:Vector2D;
private var b:Vector2D;
private var h:Vector2D;
public function BallMain() {
// constructor code
init();
}
private function init():void{
g=new Vector2D(0,0.3);
c=new Vector2D(0,5);
h=new Vector2D(.3,0);
v=new Vector2D(5,0);
ball=new GreenBall();
addChild(ball);
ball.x=200;
ball.y=stage.stageHeight-200;
ball.velocity=v;
addEventListener(Event.ENTER_FRAME,loop);
stage.addEventListener(MouseEvent.MOUSE_DOWN,cd);
stage.addEventListener(MouseEvent.MOUSE_UP,cu);
}
private function loop(e:Event):void{
v=v.add(g);
ball.velocity=v;
checkBounds();
ball.update();
}
private function checkBounds():void{
var ib:Number;
b=new Vector2D(0,0);
if(ball.y>stage.stageHeight-100){
b.length=5;
ib=Vector2D.angleBetween(v,b);
b.angle=(360-ib*180/Math.PI)*Math.PI/180;
v=b;
}else if(ball.y<0+ball.height){
//b=new Vector2D();
b.length=5;
ib=Vector2D.angleBetween(v,b);
b.angle=(360+ib*180/Math.PI)*Math.PI/180;
v=b;
}
}
private function cd(e:MouseEvent):void{
v=v.subtract(c);
v.length=v.length+5;
ball.burnOn();
}
private function cu(e:MouseEvent):void{
ball.burnOff();
}
}
}
[/as3]
The code is no where near optimized but it is a start. I am still working out the kinks in the math and use of the Vector2D class.