views:

35

answers:

1

Hello

I'm trying to create a simple game. I've set up a layer "actionscript", where I put all the code in. Then I have a image, instance name is "player". Here's my code:

stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);

var power = 5;
var friction = 0.95;

var xspeed = 0;
var yspeed = 0;

function KeyDown(keyEvent:KeyboardEvent) {
if(keyEvent.keyCode==37) { //left
        xspeed -= power;
    }
    if(keyEvent.keyCode==39) { //right
        xspeed += power;
}

xspeed *= friction;

player.x += xspeed;

//trace(keyEvent.keyCode.toString());
}

Suddenly, the script resets xspeed (and yspeed) every time so the speed cannot go up or down. How to prevent this?

I'm trying my wings in Actionscript, so I would like to have some tips and help :)

Martti Laine

+1  A: 

I think your problem is that you have more than one frame on your timeline, so the every time the playhead moves to another frame the script is run again and everything is basically reset.

Here's how I would do it (If you're willing to try something a little more advanced):
First move make sure nothing is selected on stage and find the field labeled Class: under the PUBLISH heading
Paste the text DocumentClass into it.
Now in the same directory as your fla file create a file name DocumentClass.as
Then paste the following into it, save it and run the fla file.

package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;

// Start class
// This class must extend MovieClip
public class DocumentClass extends MovieClip{
    private var acceleration:Number = 2;
    private var friction:Number = 0.95;
    // Limits the player's speed to prevent it from moving too fast
    private var maxSpeed:Number = 6;

    // The players velocity
    private var playerSpeedX:Number = 0;
    private var playerSpeedY:Number = 0;

    // Keeps track of what keys are pressed
    private var keyLeft:Boolean;
    private var keyRight:Boolean;

    // The contructor for DocumentClass - this is automatically called
    public function DocumentClass(){
        addEventListener(Event.ENTER_FRAME, eventEnterFrame, false, 0, true);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, eventKeyDown, false, 0, true);
        stage.addEventListener(KeyboardEvent.KEY_UP, eventKeyUp, false, 0, true);
    }

    // This function is called when a key is pressed
    private function eventKeyDown(keyEvent:KeyboardEvent):void{
        switch(keyEvent.keyCode){
            // Left
            case 37: keyLeft = true; break;
            // Right
            case 39: keyRight = true; break;
        }
    }
    // This function is called when a key is released
    private function eventKeyUp(keyEvent:KeyboardEvent):void{
        switch(keyEvent.keyCode){
            // Left
            case 37: keyLeft = false; break;
            // Right
            case 39: keyRight = false; break;
        }
    }

    // This function is called every frame.
    // Move the player every frame instead of in the keyboard events.
    private function eventEnterFrame(event:Event):void{
        // If the left key is pressed and the player's speed is under the limit.
        if(keyLeft && playerSpeedX > -maxSpeed)
            playerSpeedX -= acceleration;
        // Otherwise if the right key is pressed and the player's speed is under the limit.
        else if(keyRight && playerSpeedX < maxSpeed)
            playerSpeedX += acceleration;

        playerSpeedX *= friction;

        // I find that when the playerSpeedX values start to get really small the player
        // will seems to just slowly move for a long so I clip the speed when it becomes very small
        if(playerSpeedX < 0.1 && playerSpeedX > -0.1) playerSpeedX = 0;

        player.x += playerSpeedX;
    }


}// End class

}

Of course if that's a little beyond you at the moment I've modified your original code a little bit:

stage.addEventListener(KeyboardEvent.KEY_DOWN, eventKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, eventKeyUp);
addEventListener(Event.ENTER_FRAME, eventEnterFrame);

var power:Number = 5;
var friction:Number = 0.95;

var keyLeft:Boolean;
var keyRight:Boolean;

var xspeed:Number = 0;
var yspeed:Number = 0;

function eventKeyDown(keyEvent:KeyboardEvent):void{
    // Left
    if(keyEvent.keyCode==37){
        keyLeft = true;
    }
    // Right
    if(keyEvent.keyCode==39){
        keyRight = true;
    }
}

function eventKeyUp(keyEvent:KeyboardEvent):void{
    // Left
    if(keyEvent.keyCode==37){
        keyLeft = false;
    }
    // Right
    if(keyEvent.keyCode==39){
        keyRight = false;
    }
}

function eventEnterFrame(event:Event):void{
    if(keyLeft) xspeed -= power;
    if(keyRight) xspeed += power;

    xspeed *= friction;

    if(xspeed < 0.1 && xspeed > -0.1) xspeed = 0;

    player.x += xspeed;
}
cmann
Thanks! This will help me learn as3 more :)
Martti Laine