在Actionscript 3游戏上只设置一次变量?

时间:2010-10-30 18:32:13

标签: actionscript-3 actionscript

我正在尝试制作一款简单的游戏。我已经设置了一个“动作脚本”图层,我将所有代码放入其中。然后我有一个图像,实例名称是“播放器”。这是我的代码:

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());
}

突然,脚本每次都会重置xspeed(和yspeed),因此速度不会上升或下降。如何防止这种情况?

我正在使用Actionscript尝试我的翅膀,所以我想提供一些提示和帮助:)

Martti Laine

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您的时间轴上有多个框架,因此每次播放头移动到另一个框架时,脚本会再次运行,所有内容基本上都会重置。

这是我将如何做到的(如果你愿意尝试一些更高级的东西):
首先确保在舞台上没有选择任何内容,并在Class:标题下找到标记为PUBLISH的字段 将文字DocumentClass粘贴到其中。
现在与fla文件位于同一目录中,创建文件名DocumentClass.as
然后将以下内容粘贴到其中,保存并运行fla文件。

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

}

当然,如果现在有点超出你的范围,我已经修改了原始代码:

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;
}