2011-04-02 21 views
1

我想要做的是獲得4個movieclipps(leaf1,leaf2,leaf3,leaf4)以播放附加到它們上的聲音,當它們被拖動到另一個動畫片段時NatureTarget)並按下播放按鈕,以便按照拖動的順序播放聲音。我知道我需要使用數組和推動功能和循環...但我迷路了。任何幫助將非常感激。謝謝。使用數組和循環以及push函數

+0

你可以發佈你的代碼,以便我們可以看到你有多遠? – shanethehat 2011-04-02 11:43:11

回答

0

我做了一個粗略的例子,希望給你一個關於如何去解決你的問題的想法。如果你發佈了一些你的代碼,我可以定製這個答案。無論如何,只需複製代碼並將其粘貼到文檔根目錄即可。

package 
{ 
    import flash.display.Sprite; 
    import flash.display.StageAlign; 
    import flash.display.StageScaleMode; 
    import flash.events.Event; 
    import flash.events.MouseEvent; 
    import flash.media.Sound; 
    import flash.net.URLRequest; 

    public class Main extends Sprite 
    { 
     private var _leafVector:Vector.<Leaf>; 
     private var _nature:Nature; 
     private var _playButton:PlayButton; 
     private var _leafPlayList:LeafPlayList; 

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

     }// end function 

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

      stage.align = StageAlign.TOP_LEFT; 
      stage.scaleMode = StageScaleMode.NO_SCALE; 

      var soundsXml:XML = 

      <sounds> 
       <sound name="sound1" url="sounds/sound1.mp3" /> 
       <sound name="sound2" url="sounds/sound2.mp3" /> 
       <sound name="sound3" url="sounds/sound3.mp3" /> 
       <sound name="sound4" url="sounds/sound4.mp3" /> 
      </sounds> 

      _leafVector = new Vector.<Leaf>(); 

      for (var i:uint = 0; i < soundsXml.children().length(); i++) 
      { 
       var sound:Sound = new Sound(new URLRequest(soundsXml.sound[i][email protected])); 

       var leaf:Leaf = new Leaf(String(i+1), sound, soundsXml.sound[i][email protected]); 
       leaf.x = i * leaf.width; 
       addChild(leaf); 

       leaf.addEventListener(MouseEvent.MOUSE_DOWN, onLeafMouseDown); 
       leaf.addEventListener(MouseEvent.MOUSE_UP, onLeafMouseUp); 

       _leafVector.push(leaf); 

      }// end for 

      _nature = new Nature(); 
      _nature.x = _leafVector[3].x + _leafVector[3].width; 
      addChildAt(_nature, 0); 

      _playButton = new PlayButton(); 
      _playButton.x = _nature.x; 
      _playButton.y = _nature.height; 
      addChild(_playButton); 

      _playButton.addEventListener(MouseEvent.CLICK, onPlayButtonClick); 

      _leafPlayList = new LeafPlayList(); 
      _leafPlayList.x = _playButton.x; 
      _leafPlayList.y = _playButton.y + _playButton.height; 
      addChild(_leafPlayList); 

     }// end function 

     private function onLeafMouseUp(e:MouseEvent):void 
     { 
      var leaf:Leaf = Leaf(e.currentTarget); 
      leaf.stopDrag(); 

      if (leaf.dropTarget == _nature) 
      { 
       _leafPlayList.addLeafSound(leaf.soundName, leaf.sound); 
       leaf.removeEventListener(MouseEvent.MOUSE_DOWN, onLeafMouseDown); 
       leaf.removeEventListener(MouseEvent.MOUSE_UP, onLeafMouseUp); 

      }// end function 

     }// end function 

     private function onLeafMouseDown(e:MouseEvent):void 
     { 
      var leaf:Leaf = Leaf(e.currentTarget); 
      this.setChildIndex(leaf, this.numChildren-1); 
      leaf.startDrag(); 

     }// end function 

     private function onPlayButtonClick(e:MouseEvent):void 
     { 
      _leafPlayList.playLeafSounds(); 

     }// end function 

    }// end class 

}// end package 

import flash.display.Sprite; 
import flash.media.Sound; 
import flash.text.TextField; 

internal class Leaf extends Sprite 
{ 
    private var _sound:Sound; 
    private var _soundName:String; 

    public function get soundName():String 
    { 
     return _soundName; 
    }// end function 

    public function get sound():Sound 
    { 
     return _sound; 

    }// end function 

    public function Leaf(text:String, sound:Sound, soundName:String) 
    { 
     _sound = sound; 
     _soundName = soundName; 
     graphics.lineStyle(1); 
     graphics.beginFill(0x00FF00); 
     graphics.drawCircle(25, 25, 25); 
     graphics.endFill(); 

     var textField:TextField = new TextField(); 
     textField.text = text; 
     textField.x = width/2; 
     textField.y = height/2; 
     textField.selectable = false; 
     addChild(textField); 

    }// end function 

}// end class 

internal class Nature extends Sprite 
{ 
    public function Nature() 
    { 
     graphics.beginFill(0xFFFF00); 
     graphics.drawRect(0, 0, 200, 200); 
     graphics.endFill(); 

    }// end function 

}// end class 

internal class PlayButton extends Sprite 
{ 
    public function PlayButton() 
    { 
     graphics.lineStyle(1); 
     graphics.beginFill(0xFF0000); 
     graphics.drawRect(0, 0, 100,25); 
     graphics.endFill(); 

     var textField:TextField = new TextField(); 
     textField.text = "PLAY BUTTON"; 
     textField.selectable = false; 
     addChild(textField); 

    }// end function 

}// end class 

import flash.events.Event; 
import flash.media.SoundChannel; 

internal class LeafPlayList extends Sprite 
{ 
    private var _soundChannel:SoundChannel; 
    private var _leafSoundVector:Vector.<Sound>; 
    private var _position:int; 
    private var _playListTextField:TextField; 

    public function LeafPlayList() 
    { 
     _playListTextField = new TextField(); 
     addChild(_playListTextField); 
     _soundChannel = new SoundChannel(); 
     _leafSoundVector = new Vector.<Sound>(); 

    }// end function 

    public function addLeafSound(soundName:String, sound:Sound):void 
    { 
     _playListTextField.appendText("\n" + soundName); 
     _leafSoundVector.push(sound); 

    }// end function 

    public function playLeafSounds():void 
    { 
     _soundChannel.stop(); 
     _position = 1; 
     _soundChannel = _leafSoundVector[_position - 1].play(); 
     _soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete); 

    }// end function 

    private function onSoundChannelSoundComplete(e:Event):void 
    { 
     if (!(_position == _leafSoundVector.length)) _position++ 
     else _position = 1; 

     playNexLeafSound(); 

    }// end function 

    private function playNexLeafSound():void 
    { 
     _soundChannel = _leafSoundVector[_position - 1].play(); 
     _soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundChannelSoundComplete); 

    }// end function 

}// end class