我如何在闪光灯中更换主播放器?

时间:2013-11-27 17:51:56

标签: actionscript-3 flash actionscript

我是关于脚本的新手,我想做一个小小的免费商店,我的游戏是关于一辆必须避开物品的跑车,所以在商店我想要当用户按下时选择http://prntscr.com/270ws5主要这辆车http://prntscr.com/270y45成为他们选择的车,谢谢

//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyPressed: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 = 20;
//the current speed of the jump;
var jumpSpeed:Number = 0;

//adding a listener to mcMain which will make it move 
//based on the key strokes that are down
player.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
    //if certain keys are down then move the character
    if(leftKeyDown){
        player.x -= mainSpeed;
    }
    if(rightKeyDown){
        player.x += mainSpeed;
    }
    if(upPressed || mainJumping){
        mainJump();
     gotoAndStop(2)
      gotoAndStop(3)
       gotoAndStop(4)
        gotoAndStop(5)


    }
}

//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;
    }
    if(event.keyCode == 38 || event.keyCode == 87){
        upPressed = true;
        gotoAndStop(2)
    }
    if(event.keyCode == 39 || event.keyCode == 68){
        rightKeyDown = true;
    }
    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){
        upPressed = false;
         gotoAndStop(2)
      gotoAndStop(3)
       gotoAndStop(4)
        gotoAndStop(5)

    }
    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;
        player.y += jumpSpeed;
    } else {
        //then continue jumping if already in the air
        //crazy math that I won't explain
        if(jumpSpeed < 0){
            jumpSpeed *= 1 - jumpSpeedLimit/75;
            if(jumpSpeed > -jumpSpeedLimit/5){
                jumpSpeed *= -1;
            }
        }
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
            jumpSpeed *= 1 + jumpSpeedLimit/50;
        }
        player.y += jumpSpeed;
        //if main hits the floor, then stop jumping
        //of course, we'll change this once we create the level
        if(player.y >= stage.stageHeight - player.height){
            mainJumping = false;
            player.y = stage.stageHeight - player.height;
        }
    }
}

这是我的汽车脚本,非常感谢有人可以帮助我,提高我的脚本知识

1 个答案:

答案 0 :(得分:0)

您为playerMovieClip类提供了一个属性,因为它似乎在您的游戏中代表汽车的电影剪辑,然后分配给选定汽车的实例。然后,只要您想要playergotoAndStop(),就可以将其解决为附加实例。因此,实例将表示可更改的图形,而包装器对象(player)负责处理,碰撞和其他非图形化的东西。

class Player extends Sprite {
    private var _car:MovieClip; // the instance
    public function Player() {
        _car=new MovieClip();
        addChild(_car);
    }
    public function get car():MovieClip { return _car; } // getter function
    public function set car(value:MovieClip):void {
        // setter function. We need to clean up our player from old car
        if (!value) return; // null car - no wai
        if (_car) removeChild(_car);
        _car=value;
        _car.x=0;
        _car.y=0; // place car at zero relative to player
        // this way you move the player, not the car, 
        // but MC playing is the car, not the player
        addChild(_car);
   }
   ...
}

请注意,您也可以将跳转处理重新定位到Player类中,这样您就可以调用player.jump()并且不关心实际值,然后播放器必须存储跳跃常数,速度,州(在空中或接触地面)等等。