Create a Flash game like Cirplosion – AS3 version

Read all posts about "" game

Some days ago I published Create a Flash game like Cirplosion using AS2, now it’s time to do it using AS3.

AS3 is becoming more and more popular so I think it’s time to give it more exposure.

This is the uncommented AS3 version of Cirplosion.

I would like you to compare it with AS2 one.

I am preparing a line-by-line explanation of all differences between AS2 and AS3 versions.

This will be very helpful for all people out there that are afraid to start coding AS3.

While AS2 is far from being obsolete (in my opinion), you can’t ignore it exists and it’s better than AS2

package {
	import flash.display.Sprite;
	import flash.ui.Mouse;
	import flash.events.*;
	public class cirplosionas3 extends Sprite {
		var grow_rate = 2;
		var enemies = 15;
		var enemy_speed = 3;
		var speed_add = 3;
		var addspeed_range = 50;
		var has_grown = false;
		var can_grow = false;
		var explode = false;
		var playersprite: player = new player();
		public function cirplosionas3() {
			Mouse.hide();
			for (x=1; x<=enemies; x++) {
				var enemysprite: enemy = new enemy();
				enemysprite.x = Math.random()*450+25;
				enemysprite.y = Math.random()*450+25;
				enemysprite.anglez = Math.random()*6.28318531;
				enemysprite.addspeed = 0;
				enemysprite.addEventListener(Event.ENTER_FRAME,move_enemy);
				addChild(enemysprite);
			}
			addChild(playersprite);
			addEventListener(Event.ENTER_FRAME,main);
			addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
			addEventListener(MouseEvent.MOUSE_UP,mouseup);
		}
		public function move_enemy(event:Event) {
			var current_enemy:enemy = (event.currentTarget as enemy);
			var x_dist = 0;
			var y_dist = 0;
			var distance = 0;
			current_enemy.x+=(enemy_speed+current_enemy.addspeed)*Math.cos(current_enemy.anglez);
			current_enemy.y+=(enemy_speed+current_enemy.addspeed)*Math.sin(current_enemy.anglez);
			if (current_enemy.x>500) {
				current_enemy.x -= 500;
			}
			if (current_enemy.y>500) {
				current_enemy.y -= 500;
			}
			if (current_enemy.x<0) {
				current_enemy.x += 500;
			}
			if (current_enemy.y<0) {
				current_enemy.y += 500;
			}
			x_dist = current_enemy.x-playersprite.x;
			y_dist = current_enemy.y-playersprite.y;
			distance = x_dist*x_dist+y_dist*y_dist;
			if (distance<(current_enemy.width/2+playersprite.width/2)*(current_enemy.width/2+playersprite.width/2)) {
				if (can_grow) {
					reset_player();
				}
				if (explode) {
					current_enemy.removeEventListener(Event.ENTER_FRAME,move_enemy);
					removeChild(current_enemy);
				}
			}
			else {
				if (distance<(current_enemy.width/2+playersprite.width/2+addspeed_range)*(current_enemy.width/2+playersprite.width/2+addspeed_range) && explode) {
					current_enemy.anglez -= 3.14159265;
					current_enemy.addspeed = speed_add;
				}
			}
		}
		public function main(event:Event) {
			playersprite.x = mouseX;
			playersprite.y = mouseY;
			if (can_grow) {
				playersprite.width += grow_rate;
				playersprite.height += grow_rate;
				if (playersprite.x-playersprite.width/2<0) {
					reset_player();
				}
				if (playersprite.x+playersprite.width/2>500) {
					reset_player();
				}
				if (playersprite.y-playersprite.width/2<0) {
					reset_player();
				}
				if (playersprite.y+playersprite.width/2>500) {
					reset_player();
				}
			}
			if (explode) {
				reset_player();
			}
		}
		public function mousedown(event:Event) {
			if (!has_grown) {
				can_grow = true;
			}
			else {
				explode = true;
			}
		}
		public function mouseup(event:Event) {
			if (can_grow) {
				has_grown = true;
				can_grow = false;
				playersprite.alpha = 0.5;
			}
		}
		public function reset_player() {
			playersprite.alpha = 1;
			playersprite.width = 10;
			playersprite.height = 10;
			explode = false;
			has_grown = false;
			can_grow = false;
		}
	}
}

Download the source code… no need to publish the result because it’s the same as AS2 version.

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

215 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
// Stairs
// 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