|
Race car basics: simple speed simulation
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.Timer;
import flash.text.TextField;
[SWF(width = '500', height = '550')]
public class RaceCarBasics extends Sprite
{
[Embed(source="carstart.png")]
private var TheCar:Class;
private var carPicture:DisplayObject = new TheCar();
[Embed(source="grass1.jpg")]
private var GrassOnePicture:Class;
[Embed(source="grass2.jpg")]
private var GrassTwoPicture:Class;
[Embed(source="grass3.jpg")]
private var GrassThreePicture:Class;
[Embed(source="street.jpg")]
private var TheStreet:Class;
private var roadway:DisplayObject = new TheStreet();
private var grassOneA:DisplayObject = new GrassOnePicture();
private var grassOneB:DisplayObject = new GrassOnePicture();
private var grassTwoA:DisplayObject = new GrassTwoPicture();
private var grassTwoB:DisplayObject = new GrassTwoPicture();
private var grassThreeA:DisplayObject = new GrassThreePicture();
private var grassThreeB:DisplayObject = new GrassThreePicture();
private var bounds:Sprite = new Sprite();
private var roadLines:Sprite = new Sprite();
private var roadLinesMirror:Sprite = new Sprite();
private var carSpeed:Number = 5;
private var theRoad:Sprite = new Sprite();
private var slowDown:Timer = new Timer(500, 15);
private var speedUp:Timer = new Timer(500, 15);
private var stillDriving:Boolean = false;
private var yourCar:Sprite = new Sprite();
private var MPHField:TextField = new TextField();
private var grassDelay:Number = 500;
private var grassMove:Timer;
public function RaceCarBasics()
{
grassMove = new Timer(grassDelay, 0);
theRoad.graphics.lineStyle(2, 0x000000);
theRoad.graphics.drawRect(0, 0, 500/2, 550+10);
theRoad.x = 500/2 - theRoad.width/2;theRoad.y = -2;
this.addChild(theRoad);
roadway.x = theRoad.x;
this.addChild(roadway);
grassOneA.x = theRoad.x - grassOneA.width;
grassTwoA.x = grassOneA.x;
grassThreeA.x = grassTwoA.x;
this.addChild(grassThreeA);
this.addChild(grassTwoA);
this.addChild(grassOneA);
grassOneB.x = theRoad.x + theRoad.width;
grassTwoB.x = grassOneB.x;
grassThreeB.x = grassTwoB.x;
this.addChild(grassThreeB);
this.addChild(grassTwoB);
this.addChild(grassOneB);
roadLinesMirror.graphics.lineStyle(1, 0x000000);
roadLinesMirror.graphics.beginFill(0xFFFFFF);
roadLines.graphics.lineStyle(1, 0x000000);
roadLines.graphics.beginFill(0xFFFFFF);
for (var i:int = 0; i<550; i += 25)
{roadLines.graphics.drawRect(0, i, 5, 15);
roadLinesMirror.graphics.drawRect(0, i, 5, 15);}
roadLines.x = 500/2 - roadLines.width/2;roadLines.y = 0;
this.addChild(roadLines);
roadLinesMirror.x = 500/2 - roadLinesMirror.width/2;
roadLinesMirror.y = 0-roadLinesMirror.height-10;
this.addChild(roadLinesMirror);
yourCar.addChild(carPicture);
yourCar.x = 500/2 - yourCar.width/2+42;
yourCar.y = 550-115;
this.addChild(yourCar);
MPHField.text = "MPH: ";
MPHField.y = 35;
MPHField.x = theRoad.x + 25;
this.addChild(MPHField);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPresses);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleases);
slowDown.addEventListener(TimerEvent.TIMER, slowIt);
speedUp.addEventListener(TimerEvent.TIMER, speedIt);
stage.addEventListener(Event.ENTER_FRAME, movementGovern);
grassMove.addEventListener(TimerEvent.TIMER, theGrassMovement);
}
private function theGrassMovement(event:TimerEvent):void
{
swapChildren(grassOneA, grassTwoA);swapChildren(grassOneB, grassTwoB);
swapChildren(grassTwoA, grassThreeA);swapChildren(grassTwoB, grassThreeB);
}
private function slowIt(event:TimerEvent):void
{
if (carSpeed >= 6)
{carSpeed -= 1;}
}
private function speedIt(event:TimerEvent):void
{
if (carSpeed <= 15)
{carSpeed += 1;}
}
private function movementGovern(event:Event):void
{
grassDelay = Number(300 - Math.floor(carSpeed*20));
if (stillDriving == true)
{roadLines.y += carSpeed;roadLinesMirror.y += carSpeed;grassMove.start();}
else
{
if (slowDown.running!=false)
{roadLines.y += carSpeed;roadLinesMirror.y += carSpeed;}
else
{grassMove.stop();}
}
if (roadLines.y > 550)
{roadLines.y = 0;roadLinesMirror.y = 0-roadLinesMirror.height-10;}
var themph:String = carSpeed-5 +"0";
MPHField.text = "MPH: "+themph;
}
private function keyReleases(event:KeyboardEvent):void
{
if (event.keyCode == 38)
{
stillDriving = false;
if (speedUp.running == true)
{speedUp.stop();speedUp.reset();}
if (slowDown.running == false)
{slowDown.reset();slowDown.start();}
else
{slowDown.stop();slowDown.reset();slowDown.start();}
}
}
private function keyPresses(event:KeyboardEvent):void
{
var thekey:String = "" + event.keyCode + "";
if (thekey == "38" || thekey == "37" || thekey == "39")
{
if (thekey == "38") {
if (slowDown.running == true)
{slowDown.stop();slowDown.reset();}
stillDriving = true;
if (speedUp.running == false)
{speedUp.reset();speedUp.start();}
}
if (thekey == "37")
{yourCar.x -= 10;}
if (thekey == "39")
{yourCar.x += 10;}
}
if (thekey == "40")
{roadLines.y -= carSpeed;roadLinesMirror.y -= carSpeed;stillDriving = false;}
if (thekey == "40" && roadLines.y < 0)
{roadLines.y = 550;roadLinesMirror.y = 550-roadLinesMirror.height-10;}
}
}
}
|