mardi 28 juin 2016

Creating First Person Controls in Actionscript 3

I'm trying to make first person movement controls for a simple FPS I'd like to make in flash, but I'm struggling with directional movement.

Summery: I'd like to be able to create FPS controls in flash, but can't seem to get it right.

Details: I'm using a method by which rotations are applied to a movieclip (mc) that remains stationary with its pivot point underneath your point of view, so that the world seemingly rotates around you when the mouse is moved.

I then apply directional movements to a moveclip (box) nested within the rotating movieclip (mc) to give the illusion of walking around in a first person view.

mc - Handles rotation (a container for box)

-----box - Handles translation (box serves as the floor, so I have it as a black square)

The problem begins when you rotate mc and begin moving with the arrow keys. box's translation doesn't take mc's rotation in consideration, and so you're still locked to the x and y axis of box so, no matter where you face, you're always locked onto those axis as opposed to the traditional move forward wherever you're facing that most FPS games employ.

I haven't a clue of where to go from here, so any help would be greatly appreciated. :3

Timeline code:

//You’ll need a symbol named mc. With another symbol named box within it.
//mc should be centered on the stage.
//stage size 1920x1080


var mL: Boolean = false;
var mR: Boolean = false;
var mD: Boolean = false;
var mU: Boolean = false;

var speed: int = 10;

mc.rotationX=90;
mc.y=1000;

mc.x=932.1;
mc.z=0;
mc.width=980;
mc.height=98;



stage.addEventListener(Event.ENTER_FRAME, EF);

function EF(e: Event): void {
//Rotates mc based on distance of mouseX to the center of the stage.
    if(mouseX<stage.stageWidth){
        mc.rotationY += speed*((mouseX-stage.stageWidth/2)*.0001);
    }
    if(mouseX>stage.stageWidth){
        mc.rotationY += speed*((mouseX-stage.stageWidth/2)*.0001);
    }


//Arrow key controls.
    if (mL == true) {
        mc.box.x += speed;
    }
    if (mR == true) {
        mc.box.x -= speed;
    }
    if (mD == true) {
        mc.box.y += speed;
    }
    if (mU == true) {
        mc.box.y -= speed;
    }

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, kD);
function kD(e: KeyboardEvent): void {
//Key down conditionals.
    if (e.keyCode == Keyboard.LEFT && mR == false && mD == false && mU == false) {
        mL = true;
    }
    if (e.keyCode == Keyboard.LEFT && mR == false && mD == false && mU == true) {
        mL = true;
        mU = true;
    }
    if (e.keyCode == Keyboard.UP && mR == false && mD == false && mL == true) {
        mL = true;
        mU = true;
    }
    if (e.keyCode == Keyboard.LEFT && mR == false && mD == true && mU == false) {
        mL = true;
        mD = true;
    }
    if (e.keyCode == Keyboard.DOWN && mR == false && mU == false && mL == true) {
        mL = true;
        mD = true;
    }


    if (e.keyCode == Keyboard.RIGHT && mL == false && mD == false && mU == false) {
        mR = true;
    }
    if (e.keyCode == Keyboard.RIGHT && mL == false && mD == false && mU == true) {
        mR = true;
        mU = true;
    }
     if (e.keyCode == Keyboard.UP && mL == false && mD == false && mR == true) {
        mR = true;
        mU = true;
    }
    if (e.keyCode == Keyboard.RIGHT && mL == false && mD == true && mU == false) {
        mR = true;
        mD = true;
    }
    if (e.keyCode == Keyboard.DOWN && mL == false && mU == false && mR == true) {
        mR = true;
        mD = true;
    }


    if (e.keyCode == Keyboard.UP && mR == false && mD == false && mL == false) {
        mU = true;
    }
    if (e.keyCode == Keyboard.DOWN && mL == false && mR == false && mU == false) {
        mD = true;
    }


}

stage.addEventListener(KeyboardEvent.KEY_UP, kU);
function kU(e: KeyboardEvent): void {
//Key up conditionals.
    if (e.keyCode == Keyboard.LEFT) {
        mL = false;
    }
    if (e.keyCode == Keyboard.RIGHT) {
        mR = false;
    }
    if (e.keyCode == Keyboard.UP) {
        mU = false;
    }
    if (e.keyCode == Keyboard.DOWN) {
        mD = false;
    }
}

Aucun commentaire:

Enregistrer un commentaire