2011-05-09 65 views
1

我正在製作Flash遊戲(基本上是同一款遊戲的一個版本),並且我使用了tileList作爲填充movieClip的棋盤。我想了個向mouseOvermouseOutmouseClick事件作出響應。tileList單元對as3中的鼠標事件做出響應

從尋找其他的問題/答案,我的想法,我需要一個定製imageCell。這是要走的路嗎?我希望能夠通過將下面的動作放入movieClips本身來獲得我想要的回覆,但這似乎不起作用。 (順便說一句我是新來的社區,但已經從谷歌搜索在這裏受罪幾次...感謝您的幫助,我從你美妙的人接受了。乾杯。)

this.addEventListener(MouseEvent.MOUSE_OVER, OverHandler); 
this.addEventListener(MouseEvent.MOUSE_OUT, OutHandler); 
this.addEventListener(MouseEvent.CLICK, ClickHandler); 

this.stop(); 

function OverHandler(event:MouseEvent):void 
{ 
event.target.play(); 
} 

function OutHandler(event:MouseEvent):void 
{ 
event.target.stop(); 
} 

function ClickHandler(event:MouseEvent):void 
{ 
    event.target.play(); 
} 

回答

0

我可能會成立所有在有事件出現,並準備在遊戲中你的作品的一個基類。像這樣:

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.MouseEvent; 

    public class Piece extends MovieClip 
    { 
     /** 
     * Constructor 
     */ 
     public function Piece() 
     { 
      addEventListener(MouseEvent.CLICK, _click); 
      addEventListener(MouseEvent.ROLL_OVER, _rollOver); 
      addEventListener(MouseEvent.ROLL_OUT, _rollOut); 
     } 

     /** 
     * Called on MouseEvent.CLICK 
     */ 
     protected function _click(e:MouseEvent):void 
     { 
      trace(this + " was clicked"); 
     } 

     /** 
     * Called on MouseEvent.ROLL_OVER 
     */ 
     protected function _rollOver(e:MouseEvent):void 
     { 
      trace(this + " was rolled over"); 
     } 

     /** 
     * Called on MouseEvent.ROLL_OUT 
     */ 
     protected function _rollOut(e:MouseEvent):void 
     { 
      trace(this + " was rolled off"); 
     } 

     /** 
     * Destroys this 
     */ 
     public function destroy():void 
     { 
      // listeners 
      addEventListener(MouseEvent.CLICK, _click); 
      addEventListener(MouseEvent.ROLL_OVER, _rollOver); 
      addEventListener(MouseEvent.ROLL_OUT, _rollOut); 

      // from DisplayList 
      if(parent) parent.removeChild(this); 
     } 
    } 
} 

如果您需要點擊,ROLL_OVER或ROLL_OUT做任何事情對某些作品獨特的,只是延長和做這樣的:

package 
{ 
    import flash.events.MouseEvent; 

    public class MyPiece extends Piece 
    { 
     override protected function _click(e:MouseEvent):void 
     { 
      trace("mypiece was clicked"); 
     } 
    } 
}