Create a Flash Racing Game Tutorial – AS3 version

Read all posts about "" game

The porting of the most popular AS2 tutorials continues with a basic AS3 version of the Flash racing game script.

I said “basic” because it does not have all features, this time it covers only movement and collision detection because I want to add more features before polishing it.

In the detail, I am developing an artificial intelligence algorithm to include cars controlled by the computer.

Meanwhile, this is the main class

package {
	import flash.display.Sprite;
	public class racing extends Sprite {
		public var car:car_mc;
		public var ground:ground_mc = new ground_mc();
		public function racing():void {
			car = new car_mc(450,300);
			addChild(ground);
			addChild(car);
		}
	}
}

And this is car_mc class:

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	public class car_mc extends Sprite {
		public var acceleration:Number=0.4;
		public var speed_decay:Number=0.96;
		public var rotation_step:Number=10;
		public var max_speed:Number=10;
		public var back_speed:Number=1;
		public var speed:Number=0;
		public var accelerate,brake,turn_left,turn_right:Boolean=false;
		public function car_mc(posx:int,posy:int):void {
			x=posx;
			y=posy;
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		public function init(e:Event):void {
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,on_key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP,on_key_up);
		}
		public function on_enter_frame(e:Event):void {
			if (accelerate&&speed-1) {
				speed-=back_speed;
			}
			var speed_x:Number=Math.sin(rotation*0.0174532925)*speed;
			var speed_y:Number=- Math.cos(rotation*0.0174532925)*speed;
			if (turn_left) {
				rotation -= rotation_step*(speed/max_speed);
			}
			if (turn_right) {
				rotation += rotation_step*(speed/max_speed);
			}
			y+=speed_y;
			x+=speed_x;
			var point_left:Point=new Point(-9,0);
			var point_right:Point=new Point(9,0);
			var point_front:Point=new Point(0,-13);
			var point_back:Point=new Point(0,13);
			point_left=localToGlobal(point_left);
			point_right=localToGlobal(point_right);
			point_front=localToGlobal(point_front);
			point_back=localToGlobal(point_back);
			var par:racing=this.parent as racing;
			if (par.ground.hitTestPoint(point_left.x,point_left.y,true)) {
				rotation+=5;
				speed*=0.85;
			}
			if (par.ground.hitTestPoint(point_right.x,point_right.y,true)) {
				rotation-=5;
				speed*=0.85;
			}
			if (par.ground.hitTestPoint(point_front.x,point_front.y,true)) {
				speed*=0.55;
			}
			if (par.ground.hitTestPoint(point_back.x,point_back.y,true)) {
				speed*=0.55;
			}
			if (Math.abs(speed)>0.3) {
				speed*=speed_decay;
			} else {
				speed=0;
			}
		}
		public function on_key_down(e:KeyboardEvent):void {
			if (e.keyCode==38) {
				accelerate=true;
			}
			if (e.keyCode==40) {
				brake=true;
			}
			if (e.keyCode==37) {
				turn_left=true;
			}
			if (e.keyCode==39) {
				turn_right=true;
			}
		}
		public function on_key_up(e:KeyboardEvent):void {
			if (e.keyCode==38) {
				accelerate=false;
			}
			if (e.keyCode==40) {
				brake=false;
			}
			if (e.keyCode==37) {
				turn_left=false;
			}
			if (e.keyCode==39) {
				turn_right=false;
			}
		}
	}
}

And this is the result

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