Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Elasticity game, Actionscript 3, Flash and Game development.

Today I am porting an old tutorial, Controlling a ball like in Flash Elasticity game tutorial, into AS3.

I am doing this because it was a great game in my opinion, and one of the “non Box2D powered” games that can live a second life now that Box2D has been released.

About all the theory and the objects used, refer to the original post, this is just a starting point.

Anyway, let’s see the code… there are five classes in this prototype:

main class

This is the main class, the one calling all subclasses

package {
	import flash.display.Sprite;
	import flash.ui.Mouse
	public class main extends Sprite {
		public var circle:circle_mc = new circle_mc();
		public var crosshair:crosshair_mc = new crosshair_mc();
		public var newmouse:newmouse_mc = new newmouse_mc();
		public var ball:ball_mc = new ball_mc();
		public function main():void {
			Mouse.hide();
			addChild(circle);
			addChild(crosshair);
			addChild(newmouse);
			addChild(ball);
		}
	}
}

circle_mc class

This is the class relative to the big circle you can move the crosshair in:

package {
	import flash.display.Sprite;
	public class circle_mc extends Sprite {
		public function circle_mc():void {
			x = 250;
			y = 200;
		}
	}
}

I just place it in the middle of the stage.

newmouse_mc class

This is my mouse replacement

package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class newmouse_mc extends Sprite {
		public function newmouse_mc():void {
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
		}
		public function on_enter_frame(e:Event):void {
			x=stage.mouseX;
			y=stage.mouseY;
		}
	}
}

And I just follow the mouse…

crosshair_mc class

The crosshair follows the mouse but can’t go outside the big circle

package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class crosshair_mc extends Sprite {
		public function crosshair_mc():void {
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
		}
		public function on_enter_frame(e:Event):void {
			var par:main=this.parent as main;
			var circle_limit:Number = par.circle.width/2-width/2
			x=stage.mouseX;
			y=stage.mouseY;
			var dist_x:Number=x-par.circle.x;
			var dist_y:Number=y-par.circle.y;
			var distance:Number=dist_x*dist_x+dist_y*dist_y;
			if (distance>circle_limit*circle_limit) {
				var angle:Number=Math.atan2(dist_y,dist_x);
				x=250+circle_limit*Math.cos(angle);
				y=200+circle_limit*Math.sin(angle);
			}
		}
	}
}

ball_mc class

Finally, the ball follows the crosshair with an elastic effect

package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class ball_mc extends Sprite {
		public var friction:Number=0.9;
		public var speed_scale:Number=0.1;
		public var xspeed:Number=0;
		public var yspeed:Number=0;
		public function ball_mc():void {
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
			addEventListener(Event.ADDED_TO_STAGE,on_added_stage);
		}
		public function on_enter_frame(e:Event):void {
			var par:main=this.parent as main;
			var dist_x:Number = (par.crosshair.x-x)*speed_scale;
			var dist_y:Number = (par.crosshair.y-y)*speed_scale;
			xspeed+=dist_x;
			yspeed+=dist_y;
			xspeed*=friction;
			yspeed*=friction;
			x+=xspeed;
			y+=yspeed;
		}
		public function on_added_stage(e:Event):void {
			var par:main=this.parent as main;
			x=par.circle.x;
			y=par.circle.y;
		}
	}
}

And this is the result:

There are several ways to use this prototype… I’ll show you how to integrate it into a Box2D world during next days.

Download the source code.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.