views:

1247

answers:

3

Hi, i want to load an image to stage using Loader and then I want to make it draggable. Loading is done by selecting from tilelist as seen below, but I have no idea about drag and drop in AS3, can anybody help me? I want to make it as simple as possible.

Here is my code to load image:

var charge1:Loader = new Loader();
addChild(charge1);
this.addChildAt(charge1, getChildIndex(bg));
charge1.x = 280;
charge1.y = 270;

...

function setCharge1(e:Event):void{
    trace(e.target.selectedItem.source);

    this.charge1.load(new URLRequest(e.target.selectedItem.source)); 
    this.charge1.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
}
A: 

Loader is a subclass of Sprite, so you can use startDrag and stopDrag method.

For example:

charge1.addEventListener("mouseDown", function() {
    charge1.startDrag();
});

stage.addEventListener("mouseUp", function() {
    charge1.stopDrag();
});
yuku
im afraid it is not, due to adobe documentationInheritance: Loader -> DisplayObjectContainer -> InteractiveObject -> DisplayObject -> EventDispatcher -> Objectand your code gives me this:Error #1069: Property stopDrag not found on flash.display.Loader and there is no default value.
Dungeo
Sorry, I was confused between DisplayObject and Sprite. Hope that James Hay's answer helps you :)
yuku
+1  A: 

yuku was going along the right lines but what you want to do is get the contents of the loaderInfo which is actually the clip that is loaded in. something like

private function onComplete(event : Event) : void
{
  var loadedClip : MovieClip = LoaderInfo(event.target).content;

  loadedClip.addEventListener(MouseEvent.MOUSE_DOWN, function(event : MouseEvent) 
  {
    loadedClip.startDrag();
  });

  stage.addEventListener(MouseEvent.MOUSE_UP, function(event : MouseEvent) 
  {
      loadedClip.stopDrag();
  });

}
James Hay
hmmm, seems to make sense, but it gives me error, because content of the loader is not a movie clip, but a bitmap. Can i convert it somehow? And please fix the redundant leftparen before MouseEvent.MOUSE_UP, just not to confuse somebody ;)
Dungeo
If that's the base then you may want to draw that bitmap onto a movieclip using the Sprite.graphics property and using the Graphics.beginBitmapFill() method http://livedocs.adobe.com/flex/3/langref/index.html
James Hay
i think i got the point, but still i am not sure how to join it with loader, could you please explain it to me more exactly? Sorry, i am a beginner :-)
Dungeo
not needed yet, i have solved it already, but i have one more question - can i somehow restrict the area, within it the movie clip is draggable? i mean - it can be dragged it inside an invisible rectangle for example, but it cant go outside it.
Dungeo
+1  A: 

Use the following code:

this.addEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
this.addEventListener(MouseEvent.MOUSE_UP, dropMovie);
this.buttonMode = true;
private function dragMovie(event:MouseEvent):void
{
  this.startDrag();
}

private function dropMovie(event:MouseEvent):void
{
  this.stopDrag();
}
Ahmy