2012-01-12 71 views
0

我有一個叫做_player mc的字符,當鼠標點擊時它會移動。我需要它,所以當我點擊相機隨角色移動。 (我不想讓舞臺移動)我到處搜索,但無法找到它。我將如何去做這件事?這是我的代碼。as3中的相機跟隨字符

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

您可以爲所有資產創建持有者:背景,敵人,玩家。

當例如玩家走向右邊時,將主保持器向左移動。這樣背景和其中的所有內容都會滾動到左側,但是您的播放器向右移動,播放器將視覺上保持在屏幕中央。

所以你持有人和玩家將有相反的速度:

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

這就是我的意思是:

Example of game

+0

但我試圖做一個互動遊戲與一個以上人。 – thor625 2012-01-13 23:27:27

+0

與你說的人是敵人?我編輯了我的答案。 – Ben 2012-01-14 09:47:08

+0

我的意思是另一個人,如在mmo – thor625 2012-01-14 16:39:34