相机跟随as3中的角色

时间:2012-01-12 23:55:27

标签: actionscript-3 flash-cs4

我有一个名为_player mc的角色,单击鼠标时会移动。我需要它,以便当我点击相机移动角色。 (我不想让舞台移动)我到处搜索但找不到它。我该怎么做呢?这是我的代码。

package {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;

public class Main extends MovieClip {
    // player

    public var _player:MovieClip;

    // player settings
    private var _playerSpeed:Number = 10;

    // other vars
    private var _destinationX:int;
    private var _destinationY:int;

    //box
    private var boxAmount:Number=0;
    private var boxLimit:Number=20;
    private var _root:Object;
    //$txt
    public var money:int=0;
    public var gold:int=0;
    public var my_scrollbar:MakeScrollBar;

    //$$
    public var testnumber:Number = 1;



    public function Main() {

        $box.click$.move$.buttonMode=true;
        $box.click$.clickmini$.buttonMode=true;




createPlayer();


        // add listeners
        stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        stage.addEventListener(MouseEvent.CLICK, mouseHandler);


        //box add listener
        addEventListener(Event.ENTER_FRAME, eFrame);

        //moneybox
        $box.click$.move$.addEventListener(MouseEvent.MOUSE_DOWN, startmoving$);
        $box.click$.move$.addEventListener(MouseEvent.MOUSE_UP, stopmoving$);
        $box.click$.clickmini$.addEventListener(MouseEvent.CLICK, c$mini);

        my_scrollbar=new MakeScrollBar(scroll_mc,scroll_text);



    }
        private function createPlayer():void
    {
        _destinationX = stage.stageWidth / 2;
        _destinationY = stage.stageHeight / 2;

        _player = new Player();
        _player.x = stage.stageWidth / 2;
        _player.y = stage.stageHeight / 2;
        stage.addChild(_player);
    }


    private function enterFrameHandler(event:Event):void
    {
        _player.x += (_destinationX - _player.x) / _playerSpeed;
        _player.y += (_destinationY - _player.y) / _playerSpeed;
    }


    private function mouseHandler(event:MouseEvent):void
    {
        _destinationX = event.stageX;
        _destinationY = event.stageY;

        rotatePlayer();
    }

    private function rotatePlayer():void
    {
        var radians:Number = Math.atan2(_destinationY - _player.y, _destinationX - _player.x);
        var degrees:Number = radians / (Math.PI / 180) + 90;
        _player.rotation = degrees;
    }





    //boxadding
    private function eFrame(event:Event):void {
        if (boxAmount<=boxLimit) {
            boxAmount++;

            var _box:Box=new Box  ;
            _box.addEventListener(MouseEvent.CLICK,boxclick);
            _box.buttonMode=true;
            _box.y=Math.random()*stage.stageHeight;

            _box.x=Math.random()*stage.stageWidth;

            addChild(_box);

        }

    }

    public function boxclick(event:MouseEvent):void {
        var _box:Box=event.currentTarget as Box;
        logtxt.appendText("You collected " + testnumber +  " boxes" );
        _destinationX = _box.y + 40 + (_player.height / 2);
        _destinationY = _box.x;


        logtxt.scrollV=logtxt.maxScrollV;
        var randVal$:Number=Math.random();
        if (randVal$>=0.49) {
            money+=100;
        } else if (randVal$ <= 0.50 && randVal$ >= 0.15) {
            money+=200;
        } else if (randVal$ <= 0.14 && randVal$ >= 0.02) {
            gold+=10;
        } else if (randVal$ == 0.01) {
            money+=200;
            gold+=20;
        }


            testnumber ++;



        boxAmount--;


        $box.box$in.box$insins.Moneytxt.text=String(money);
        $box.box$in.box$insins.Goldtxt.text=String(gold);
        removeChild(_box);

    }

    private function startmoving$(event:MouseEvent):void {
        $box.startDrag();
    }
    private function stopmoving$(event:MouseEvent):void {
        $box.stopDrag();
    }
    private function c$mini(event:MouseEvent):void {
        $box.click$.move$.visible=false;
        $box.box$in.visible=false;

        $box.y=200;
        $box.x=100;
        $box.click$.clickmini$.addEventListener(MouseEvent.CLICK, reclickbox$);
        $box.click$.clickmini$.removeEventListener(MouseEvent.CLICK, c$mini);


    }
    private function reclickbox$(event:MouseEvent):void {
        $box.click$.clickmini$.addEventListener(MouseEvent.CLICK, c$mini);
        $box.click$.clickmini$.removeEventListener(MouseEvent.CLICK, reclickbox$);
        $box.y=70;
        $box.x=250;
        $box.click$.move$.visible=true;
        $box.box$in.visible=true;
    }

    public function scroll_text( n:Number ) {

        logtxt.scrollV = Math.round( ( logtxt.maxScrollV - 1 ) * n ) + 1;
    }

}
}

1 个答案:

答案 0 :(得分:1)

您可以为所有资产创建持有者:背景,敌人,玩家。

例如,当玩家向右走时,将主支架向左移动。这样,背景及其中的所有内容都会向左滚动,但您的播放器会向右移动,播放器将在视觉上保留在屏幕的中央。

所以你的持有者和玩家将拥有相反的速度:

player.x += speed;
holder.x -= speed;

这就是我的意思:

Example of game