0 Replies Latest reply on Jul 24, 2006 2:01 AM by shogo2040

    Drag and Drop with mx.effects.SoundEffect

    Level 1
      I am trying to get a soundeffect to play after a successful drag and drop operation. Unfortunately, there is no dragDropEffect, so I cannot use mxml for adding sound when a drop is completed. (However a sound effect can be used when the user first clicks on something before drag occurs by using mouseDownEffect.)

      Below is my code which attempts doing this from one tile to another with an image. For debugging purposes I have added an Alert to show where the sound effect fails to play. Can anyone see what is wrong with my code?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="createSoundEffects();" layout="horizontal">
      <mx:Script>
      <![CDATA[
      import mx.managers.DragManager;
      import mx.core.DragSource;
      import mx.events.DragEvent;
      import mx.effects.SoundEffect;
      import mx.controls.Alert;

      [Embed(source="C:/FLEX_PROJECTS/photos/bin/openWind-Mark_E_B-8076_hifi.mp3")]
      private var soundFxOpenSwoosh:Class;

      [Embed(source="C:/FLEX_PROJECTS/photos/bin/closeWin-Mark_E_B-8077_hifi.mp3")]
      private var soundFxCloseSwoosh:Class;

      private var dropSound:SoundEffect;

      //create the sound effect when first loaded (see mx:Application tag) for the creationComplete attribute
      private function createSoundEffects():void {
      dropSound = new SoundEffect();
      dropSound.useDuration = false;
      dropSound.loops = 0;
      dropSound.source = soundFxCloseSwoosh;
      }

      // Drag initiator event handler, called by
      // the image's mouseMove event.
      private function dragMe(event:MouseEvent, img1:Image, format:String):void {
      var dragInitiator:Image=Image(event.currentTarget);
      var ds:DragSource = new DragSource();

      ds.addData(img1, format);
      var imageProxy:Image = new Image();
      imageProxy.source = img1.source;
      imageProxy.height = img1.height;
      imageProxy.width = img1.width;
      imageProxy.alpha=.8;

      //Call the drag method from the manager
      DragManager.doDrag(dragInitiator, ds, event, imageProxy, 0, 0, 1.00);
      }

      private function doDragEnter(event:DragEvent):void {
      DragManager.acceptDragDrop(Tile(event.target));
      }

      private function doDragDrop():void {
      dropSound.end(); //see page 569 in flex2_devguide.pdf for why .end() is called here
      dropSound.play();
      Alert.show("dropSound.source is:" + dropSound.source);
      }
      ]]>
      </mx:Script>

      <mx:SoundEffect id="dragStartSound" useDuration="false" loops="0" source="{soundFxOpenSwoosh}"/>

      <mx:HDividedBox width="100%" height="100%">
      <mx:Tile width="100%" height="100%" backgroundColor="#999999">
      <mx:Image id="myimg" mouseDownEffect="{dragStartSound}" source=".\2.png" mouseMove="dragMe(event, myimg, 'img');" />
      </mx:Tile>
      <mx:Tile width="100%" height="100%" dragEnter="doDragEnter(event)" dragDrop="doDragDrop()" backgroundColor="#999999">
      </mx:Tile>
      </mx:HDividedBox>

      </mx:Application>