Flash Elasticity prototype – AS3 version

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers