AS3如何使精灵面部方向移动

时间:2016-05-19 03:53:27

标签: actionscript-3 flash

所以我有两个动画片段:mcMain,这是我面向右边的角色,我有mcMainLeft,这是我面向左边的角色。我试图实现代码,以便在按下左箭头时,mcMainLeft可见并向左移动。对于mcMain和右边也是如此。所以最终发生的事情是,当我向左移动时,它向左移动,角色朝左,但是然后我会点击右箭头并向右移动,它不会移动当前位置的当前角色,它'从不同的位置开始。我不确定这笔交易是什么。

这是我的代码:

//These variables will note which keys are down
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 15;
//the current speed of the jump;
var jumpSpeed:Number = 0;

//set coordinates of pacman left and right
mcMain.x = 270;
mcMain.y = 370;
mcMainLeft.x = 270;
mcMainLeft.y = 370;

//make pacman left invisible on startup
mcMainLeft.visible = false;

//move character function
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
mcMainLeft.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
    //if certain keys are down, then move the character
    if(leftKeyDown ){
        mcMainLeft.x -= mainSpeed;
    }
    if(rightKeyDown){
        mcMain.x += mainSpeed;
    }
    if(upKeyDown || mainJumping){
        mainJump();
    }
}

//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
    //making the booleans true based on the keycode
    //WASD Keys or arrow keys
    if(event.keyCode == 37 || event.keyCode == 65){
        leftKeyDown = true;
        mcMain.visible = false;
        mcMainLeft.visible = true;
    }
    if(event.keyCode == 38 || event.keyCode == 87){
        upKeyDown = true;
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = true;
        mcMain.visible = true;
        mcMainLeft.visible = false;
    }
    if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = true;
    }
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
    //making the booleans false based on the keycode
    if(event.keyCode == 37 || event.keyCode == 65){
        leftKeyDown = false;
    }
    if(event.keyCode == 38 || event.keyCode == 87){
        upKeyDown = false;
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = false;
    }
    if(event.keyCode == 40 || event.keyCode == 83){
        downKeyDown = false;
    }
}

//jumping function
function mainJump():void{
    //if main isn't already jumping
    if(!mainJumping){
        //then start jumping
        mainJumping = true;
        jumpSpeed = jumpSpeedLimit*-1;
        mcMain.y += jumpSpeed;
        mcMainLeft.y += jumpSpeed;
    } else {
        //then continue jumping if already in the air
        if(jumpSpeed < 0){
            jumpSpeed *= 1 - jumpSpeedLimit/75;
            if(jumpSpeed > -jumpSpeedLimit/5){
                jumpSpeed *= -1;
            }
        }
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
            jumpSpeed *= 1 + jumpSpeedLimit/50;
        }
        mcMain.y += jumpSpeed;
        mcMainLeft.y += jumpSpeed;
        //if main hits the floor, then stop jumping
        //of course, we'll change this once we create the level
        if(mcMain.y || mcMainLeft.y >= stage.stageHeight - mcMain.height || mcMainLeft.height){
            mainJumping = false;
            mcMain.y = stage.stageHeight - mcMain.height;
            mcMainLeft.y = stage.stageHeight - mcMainLeft.height;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

那是因为你基本上有两个角色对象(mcMain和mcMainLeft),但总是只移动其中一个。另一个是看不见的并且保持在起始位置。

制作一个包含两个帧的MovieClip,每个帧包含mcMain和mcMainLeft。在第一帧放置一个stop(),这样movieclip就不会自行循环。然后使用组合的动画片段作为你的角色:

function moveChar(event:Event):void{
    //if certain keys are down, then move the character
    if(leftKeyDown ){
        myNewCharacter.x -= mainSpeed;
    }
    if(rightKeyDown){
        myNewCharacter.x += mainSpeed;
    }
    if(upKeyDown || mainJumping){
       mainJump();
    }
}

而不是将可见性跳转切换到新动画片段的正确帧,以显示面向左或右的角色:

myNewCharacter.gotoAndStop(2); // or 1

答案 1 :(得分:0)

mcMain.scaleX = -1; // will face left

mcMain.scaleX = 1; // will face right

然后您可以使用.scaleX的值作为逻辑代码块中的变量。