Talking about Racing game, Actionscript 3, Flash and Game development.
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
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.