2014-01-09 134 views
0

我不知道這個問題是否完全代表我想問的問題,但我無法確定如何命名我想要做的事情。如何創建一個層次結構菜單/圖標

所以即時嘗試創建一個3x3網格,並在每個單元格中我有一個圖像。 我只能點擊列的第二個單元格之前,只有當同一列的第一個被點擊之前。我的意思是

我可以點擊單元格號:5只有當4點擊之前,

我可以單擊單元格號:9僅在8和7已被點擊之前

我可以單擊單元格號:1,4,7隨時隨地。

並且當它們被點擊它們的α到達0.1(所以我知道該小區已經被點擊。)

enter image description here

目前我用於創建網格和改變任何阿爾法邏輯對象,我點擊但我沒有邏輯層次結構。

public function Main() 
    { 

     var index:int = 0 
     var col:int = 3 
     var row:int = 3 
     for (var i:int = 0; i < row; i++) 
     { 
      for (var j:int = 0; j < col; j++) 
      { 
       var cls:Class = Class(getDefinitionByName("Bitmap" + (index + 1))); 

       myImage = new Bitmap(new cls()); 
       var myImage_mc:MovieClip = new MovieClip(); 
       myImage_mc.addChild(myImage) 
       myImage_mc.x = 100 + i * (myImage_mc.width + 10) 
       myImage_mc.y = 100 + j * (myImage_mc.height + 10) 
       this.addChild(myImage_mc); 
       myImage_mc.mouseEnabled = true 
       myImage_mc.addEventListener(MouseEvent.CLICK, onClick); 

       index++ 
      } 
     } 
    } 

    private function onClick(ev:MouseEvent):void 
    { 
     trace(ev.target.name) 
     ev.currentTarget.alpha = 0.1; 
    } 

回答

1

的另一種方式是使用一個鏈接變量鏈接元件。

public class BitmapButton extends Sprite { 
    public var next:BitmapButton = null; 
} 

public class Main extends Sprite { 
    public function Main():void 
    { 
     if (stage) init(); 
     else addEventListener(Event.ADDED_TO_STAGE, init); 
    } 


    private function init(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 

     var index:int = 0 
     var col:int = 3 
     var row:int = 3 

     for (var j:int = 0; j < col; j++) 
     { 
      var lastRowElement:BitmapButton = null; 

      for (var i:int = 0; i < row; i++) 
      { 
       var bmpd:BitmapData = new BitmapData(100, 100, false, Math.floor(0xff + Math.random() * 0xffffff)); 

       var myImage:Bitmap = new Bitmap(bmpd); 
       var myImage_mc:BitmapButton = new BitmapButton(); 

       myImage_mc.addChild(myImage) 
       myImage_mc.x = 100 + j * (myImage_mc.width + 10); 
       myImage_mc.y = 100 + i * (myImage_mc.height + 10); 

       myImage_mc.name = i + "_" + j; 

       this.addChild(myImage_mc); 

       myImage_mc.mouseChildren = false; 
       myImage_mc.mouseEnabled = false; 

       myImage_mc.addEventListener(MouseEvent.CLICK, onClick); 

       if (lastRowElement == null) { 

        myImage_mc.mouseEnabled = true; 

       } else { 
        lastRowElement.next = myImage_mc; 
       } 

       lastRowElement = myImage_mc; 

       index++ 
      } 
     } 

    } 


    private function onClick(ev:MouseEvent):void 
    { 
     trace(ev.target.name) 


     if (ev.target is BitmapButton) { 
      var btn:BitmapButton = ev.target as BitmapButton; 

      ev.currentTarget.alpha = 0.1; 

      if(btn.next != null) { 
       btn.next.mouseEnabled = true; 
      } 
     } 
    } 

} 
+0

到目前爲止,每個excample都使用Dictionary類。我必須看到這是如此偉大 – GregorII

1

像這樣

package 
{ 
    import flash.display.MovieClip; 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    import flash.utils.Dictionary; 

    public class Grid extends Sprite 
    { 
     private var _box : Dictionary = new Dictionary; 
     private var _conditions : Dictionary = new Dictionary; 
     private var _clicked : Dictionary = new Dictionary; 

     public function Grid() 
     { 
      var index:int = 0 
      var col:int = 3 
      var row:int = 3 
      for (var i:int = 0; i < row; i++) 
      { 
       for (var j:int = 0; j < col; j++) 
       { 
        //var cls:Class = Class(getDefinitionByName("Bitmap" + (index + 1))); 

        //myImage = new Bitmap(new cls()); 
        var myImage_mc:MovieClip = new MovieClip(); 
        //myImage_mc.addChild(myImage) 

        _box[ index ] = myImage_mc; 

        // Conditions for the 2nd and 3nd line etc. 
        if(i != 0) 
         _conditions[ myImage_mc ] = _box[ index - col ]; 

        // ------ DEBUG 
        myImage_mc.graphics.beginFill(0xFFFFFF * Math.random()); 
        myImage_mc.graphics.drawRect(0,0,100,100); 
        myImage_mc.graphics.endFill(); 
        // ------ END DEBUG 

        // Note i/j are invert from your example 
        myImage_mc.x = 100 + j * (myImage_mc.width + 10); 
        myImage_mc.y = 100 + i * (myImage_mc.height + 10); 
        this.addChild(myImage_mc); 
        myImage_mc.mouseEnabled = true 
        myImage_mc.addEventListener(MouseEvent.CLICK, onClick); 

        index++ 
       } 
      } 
     } 

     protected function onClick(ev:MouseEvent):void 
     { 
      var neededClickElement : MovieClip = _conditions[ ev.currentTarget ]; 

      // Check if we need an active movieclip before 
      if(neededClickElement != null && ! _clicked[ neededClickElement ]) 
       return; 

      _clicked[ ev.currentTarget ] = true; 
      ev.currentTarget.alpha = 0.1; 
     } 
    } 
}