什么是AS3相当于以下AS2代码?

时间:2017-04-26 17:52:25

标签: actionscript-3 flash actionscript-2 movieclip moviecliploader

我正在尝试将以下AS2代码转换为AS3,因为我有Adobe Flash CC,据我所知(从研究,试验和错误),它不支持AS2代码。任何帮助将不胜感激......

onClipEvent(load) {
speed = 0;
acceleration = 0.4;
speedDecay = 0.96;
maxSpeed = 10;
backSpeed = 1;
}

onClipEvent(enterFrame) {
    if(Math.abs(speed) > 0.3) { 
        speed *= speedDecay;
    }else {
        speed = 0;
    }
    if(Key.isDown(Key.UP)) {
        if (Math.abs(speed) >= maxspeed) {
            speed += acceleration;
            }
        }
    if(Key.isDown(Key.DOWN)) {
        if(speed < 0.5) 
        speed = -2;
        else
        speed--;
    }
        if (Math.abs(speed)> 0.5) {
        if (Key.isDown(Key.LEFT)) {
            _rotation -= 10;
         }
         if (Key.isDown(Key.RIGHT)) {
            _rotation += 10;
            }
        }
       x = Math.sin(_rotation*(Math.PI/180))*speed;
       y = Math.cos(_rotation*(Math.PI/180))*speed*-1;

       if (!_root.ground.hitTest(_x+x, _y+y, true)) {
       _x += x;
       _y += y;
       }else {
        speed -= speed*1.5;   
       }
}

此代码位于我的汽车游戏的Car层中。

1 个答案:

答案 0 :(得分:1)

将此代码放在第1帧的汽车动画片段中:

var speed: Number = 0;
var acceleration: Number = 0.4;
var speedDecay: Number = 0.96;
var maxSpeed: Number = 10;
var backSpeed: Number = 1;
var pressedKeys: Object = {}

stage.addEventListener(Event.ENTER_FRAME, loop)
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown)
stage.addEventListener(KeyboardEvent.KEY_UP, keyup)

function keydown(e: KeyboardEvent) {
    pressedKeys[e.keyCode] = true
}
function keyup(e: KeyboardEvent) {
    if (pressedKeys[e.keyCode]) {
        pressedKeys[e.keyCode] = false
    }
}

function loop(e: Event) {
    if (Math.abs(speed) > 0.3) {
        speed *= speedDecay;
    } else {
        speed = 0;
    }
    if (pressedKeys[Keyboard.UP]) {
        if (Math.abs(speed) >= maxSpeed) {
            speed += acceleration;
        }
    }
    if (pressedKeys[Keyboard.DOWN]) {
        if (speed < 0.5)
            speed = -2;
        else
            speed--;
    }
    if (Math.abs(speed) > 0.5) {
        if (pressedKeys[Keyboard.LEFT]) {
            this.rotation -= 10;
        }
        if (pressedKeys[Keyboard.RIGHT]) {
            this.rotation += 10;
        }
    }
    var vx: Number = Math.sin(this.rotation * (Math.PI / 180)) * speed;
    var vy: Number = Math.cos(this.rotation * (Math.PI / 180)) * speed * -1;
    if (!MovieClip(root).ground.hitTestPoint(this.x + vx, this.y + vy, true)) {
        this.x += vx;
        this.y += vy;
    } else {
        speed -= speed * 1.5;
    }
}