I'm trying to figure out how to create a racing game in flash (like many tutorials already out there on the web).
However instead of keeping the level static and moving the car around - Is it possible to keep the player's car motionless in the center of the screen and rotate the level around the player?
Here's a runnable demo that should do what you want; use the up, left, down and right keys to control the car.
The important stuff is in the last 5 or 6 lines near the bottom.
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Untitled2 extends Sprite{
private var level:Sprite = new Sprite();
private var car:Sprite = new Sprite();
private var carVelocityX:Number = 0;
private var carVelocityY:Number = 0;
private var keyLeft:Boolean;
private var keyRight:Boolean;
private var keyUp:Boolean;
private var keyDown:Boolean;
public function Untitled2(){
level.graphics.lineStyle(4, 0xFF0000);
level.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
car.graphics.beginFill(0x000000);
car.graphics.moveTo( -5, -5);
car.graphics.lineTo( 5, -5);
car.graphics.lineTo( 8, 0);
car.graphics.lineTo( 5, 5);
car.graphics.lineTo( -5, 5);
addChild(level);
level.addChild(car);
car.x = 100;
car.y = 100;
addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp, false, 0, true);
}
private function onKeyUp(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT)
keyLeft = false;
else if(event.keyCode == Keyboard.RIGHT)
keyRight = false;
else if(event.keyCode == Keyboard.UP)
keyUp = false;
else if(event.keyCode == Keyboard.DOWN)
keyDown = false;
}
private function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT)
keyLeft = true;
else if(event.keyCode == Keyboard.RIGHT)
keyRight = true;
else if(event.keyCode == Keyboard.UP)
keyUp = true;
else if(event.keyCode == Keyboard.DOWN)
keyDown = true;
}
private function onEnterFrame(event:Event):void{
// Other stuff for controlling the car
carVelocityX *= 0.98;
carVelocityY *= 0.98;
car.x += carVelocityX;
car.y += carVelocityY;
if(keyLeft) car.rotation -= 7;
if(keyRight) car.rotation += 7;
var carRotationRadians:Number = car.rotation / 180 * Math.PI;
if(keyUp){
var speed:Number = Math.sqrt(carVelocityX * carVelocityX + carVelocityY * carVelocityY);
speed += 0.1;
carVelocityX = Math.cos(carRotationRadians) * speed;
carVelocityY = Math.sin(carRotationRadians) * speed;
}
if(keyDown){
carVelocityX *= 0.8;
carVelocityY *= 0.8;
}
// Add 90 degrees because we want the car pointing up
carRotationRadians += Math.PI * 0.5
// Rotate the cars position to get its stage coordinates
var carX:Number = Math.cos(-carRotationRadians) * car.x - Math.sin(-carRotationRadians) * car.y;
var carY:Number = Math.sin(-carRotationRadians) * car.x + Math.cos(-carRotationRadians) * car.y;
// Position and rotate the level
level.rotation = -car.rotation - 90; // Add 90 degrees because we want the car pointing up
level.x = stage.stageWidth * 0.5 - carX;
level.y = stage.stageHeight * 0.5 - carY;
}
}// End class Untitled2
}// End package