Making loaded image draggable actionscript 3 - actionscript-3

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);
}

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();
});
}

This is the easiest way to me...
/* Load Image */
var myLoader:Loader = new Loader();
imageContainer.addChild(myLoader);
var url:URLRequest = new URLRequest("photo.jpg");
myLoader.load(url);
/* Drag and Drop */
imageContainer.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
function pickUp(event:MouseEvent):void
{
imageContainer.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, dopIt);
function dopIt(event:MouseEvent):void
{
imageContainer.stopDrag();
}

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();
}

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();
});

Related

How Can know the Size of Image ( Kb) in Action script 3.0?

I am Writing code to load image From Server With a Timer Function Of 2 seconds . I want to know the Size of Each image . Is it Possible to Know in ActionScript 3.0?
enter code here
public class Example extends Sprite {
public function Example() {
var myTimer:Timer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER,runMany);
myTimer.start();
function runMany(e:TimerEvent):void {
var loader:Loader=new Loader();
var url:String= "http:/google.com.example3";
loader.load(new URLRequest(url));
addChild(loader);
}
}
}
}
You can use the contentLoaderInfo.bytesTotal property of the Loader class.
trace(loader.contentLoaderInfo.bytesTotal);
Or you can add an event handler:
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
function progressHandler(event:ProgressEvent):void
{
trace(event.bytesTotal);
}
I made a small change on the above code
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
function progressHandler(event:ProgressEvent):void
{
trace(event.bytesLoaded);
}
Instead of bytestotal. I put bytesloaded. It works and shows the loading.

Draw over a movieclip

I have a paint application. I want to put a layer(movieclip with a square) and I want to draw over it.
Until now I cant paint but when I'm trying to add movieclip child, I cant draw over that movieclip. Which is the way to make something like this?
This is my code:
import flash.display.Sprite;
var myThick=10;
var currentColor = 0X000000;
var myCanvas:Sprite;
init();
function init():void {
drawCanvas();
var clearSwatch:Sprite = new Sprite();
clearSwatch.graphics.beginFill(0xFFFFFF);
clearSwatch.graphics.lineStyle(2, 0x000000);
clearSwatch.graphics.drawCircle(30, 370, 20);
clearSwatch.graphics.endFill();
addChild(clearSwatch);
clearSwatch.buttonMode = true;
clearSwatch.addEventListener(MouseEvent.CLICK, clearCanvas);//clear
}
function drawCanvas():void {
myCanvas = new Sprite();
myCanvas.graphics.beginFill(0xF0F0F0);
myCanvas.graphics.drawRect(60, 20, stage.stageWidth-80, stage.stageHeight+40);
myCanvas.graphics.endFill();
myCanvas.graphics.lineStyle(myThick, currentColor);
addChild(myCanvas);
myCanvas.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
//stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
myCanvas.buttonMode = true;
}
function startDraw(event:MouseEvent):void {
myCanvas.graphics.moveTo(event.localX, event.localY);
myCanvas.addEventListener(MouseEvent.MOUSE_MOVE, paintLine);
}
function stopDraw(event:MouseEvent):void {
myCanvas.removeEventListener(MouseEvent.MOUSE_MOVE, paintLine);
}
function paintLine(event:MouseEvent):void {
myCanvas.graphics.lineTo(event.localX, event.localY);
event.updateAfterEvent();
}
function clearCanvas(event:MouseEvent):void {
myCanvas.graphics.clear();
drawCanvas();
}
How can I make it work?
thank you!
Add both listeners to stage, along with mouse move listener, but use graphics commands over myCanvas.
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDraw);
function startDraw(event:MouseEvent):void {
myCanvas.graphics.moveTo(event.localX, event.localY);
myCanvas.addEventListener(MouseEvent.MOUSE_MOVE, paintLine);
}
Etc.

access of content of loaded swf

I want to access content of a loaded SWF file. I used following code,
function _browse(e:MouseEvent):void
{
loader.load(new URLRequest("artwork3.swf.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loading);
target_clip.addChild(loader);
}
function loading(event:Event)
{
trace(target_clip.getChildAt(0));
trace(target_clip.getChildAt(1));
}
Please help me.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleComplete );
var request:URLRequest = new URLRequest("artwork3.swf.swf");
loader.load(request);
protected function handleComplete(event:Event):void
{
DisplayObject loadedSwf = target_clip.addChild(event.currentTarget.content as DisplayObject) as DisplayObject;
//you can access variables from loaded swf
trace(loadedSwf.name);
}
function _browse(e:MouseEvent):void
{
loader.load(new URLRequest("artwork3.swf.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loading);
target_clip.addChild(loader);
}
function loading(event:Event)
{
var myLoadedSwf:MovieClip = loader.content as MovieClip;
trace(myLoadedSwf.getChildAt(0));
trace(myLoadedSwf.getChildAt(1));
//trace(target_clip.getChildAt(0));
//trace(target_clip.getChildAt(1));
}
Try MovieClip(loader.content) instead of target_clip like so,
function _browse(e:MouseEvent):void
{
loader.load(new URLRequest("artwork3.swf.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loading);
target_clip.addChild(loader);
}
function loading(event:Event)
{
trace(MovieClip(loader.content).getChildAt(0));
trace(MovieClip(loader.content).getChildAt(1));
}
Best luck.

Register some properties in event handling

I want to show several images to my application
sourceAudioFile =(event.target as File);
sourceAudioFile.data;
var myLoader:Loader = new Loader();
var url :URLRequest = new URLRequest("file:///" + sourceAudioFile.nativePath);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
myLoader.load(url);
Registration Listener
private function onImageLoaded(e:Event):void
{
var image : Bitmap= new Bitmap(e.target.content.bitmapData);
image.scaleX = 0.5 ;
image.scaleY = 0.5 ;
}
Is there any way by which i can add some custom values to Event.COMPLETE to know what type of image is being loaded.
like e.imageName , e.noOfImage which i can access in complete handler & provide values to it when i register this event.
Thanks in advance
Yes you can, try registering event like:
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event){
dispatchEvent(new CustomEvent(CustomEvent.COMPLETE, "<imageType>"));
}, false, 0, true);
And Add new Class, extending the Event class:
public class CustomEvent extends Event {
public static const COMPLETE:String = "CustomEventComplete";
public var imageType:String = "";
public function CustomEvent(type:String, imageType:String = "", bubbles:Boolean = false, cancelable:Boolean = false){
super(type, bubbles, cancelable);
this.imageType = imageType;
}
override public function clone():Event {
return new CustomEvent(type, customTarget, bubbles, cancelable);
}
}
Now you can register for CustomEvent.COMPLETE & get the image type in the listener.
You can find out what ahs been loaded by checking LoaderInfo's url property
private function onImageLoaded(e:Event):void
{
var cli:LoaderInfo = e.target as LoaderInfo;//you added listener to the contentLoaderInfo property
trace(cli.url);//url of the media being loaded
//do something with it
}
also in this SO question you can get more sophisticated asnwer to your problem:)
pass arguments to event listener
Yes, there's a simple way:
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded(imageName, noOfImage));
myLoader.load(url);
function onImageLoaded(imageName:String, noOfImage:int):Function {
return function(e:Event):void {
// Respective "e", "imageName" and "noOfImage" are available for you here.
}
}
If you need to remove it later, change the first line to:
var functionOnImageLoaded:Function = onImageLoaded(imageName, noOfImage);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, functionOnImageLoaded);
Then later just do it like this:
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, functionOnImageLoaded);
I've answered this before. See it here.

AS3 Mouse Over Change Image

Currently I'm trying to change image loaded in a sprite with mouseover event and change it back with mouseout. But it's not working correctly, am i missing something?
public class Tab extends Sprite
{
var imageLoader:Loader = new Loader();
var TabSprite:Sprite = new Sprite();
var SkinImages:Array = [Skin.TAB_ACTIVE,Skin.TAB_DISABLED,Skin.TAB_HOVER,Skin.TAB_VIEW];
public function Tab()
{
for each (var Image:String in SkinImages){
imageLoader.load(new URLRequest(Image));
}
TabSprite.buttonMode = true;
addChild(TabSprite);
TabSprite.addChild(imageLoader);
TabSprite.addEventListener(MouseEvent.MOUSE_OVER, onTabHover);
}
private function onTabHover(e:MouseEvent){
trace("HOVER");
TabSprite.removeEventListener(MouseEvent.MOUSE_OVER, onTabHover);
imageLoader.load(new URLRequest(Skin.TAB_HOVER));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
TabSprite.addEventListener(MouseEvent.MOUSE_OUT, onTabOut);
});
}
private function onTabOut(e:MouseEvent){
trace("OUT");
TabSprite.removeEventListener(MouseEvent.MOUSE_OUT, onTabOut);
imageLoader.load(new URLRequest(Skin.TAB_VIEW));
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
TabSprite.addEventListener(MouseEvent.MOUSE_OVER, onTabHover);
});
}
}
You shouldn't nest listeners that way. Just add two in the constructor:
TabSprite.addEventListener(MouseEvent.ROLL_OVER, onTabHover);
TabSprite.addEventListener(MouseEvent.ROLL_OUT, onTabOut);
Note changing MOUSE_OVER to ROLL_OVER it's better in most cases. You shouldn't also load images at every mouse event. Preload them, and then use. Also using anonymous functions in listeners is bad practice as you are not able to remove that listener:
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event):void{
TabSprite.addEventListener(MouseEvent.MOUSE_OUT, onTabOut);
});
And in fact you are not removing it - this is bad.
for each (var Image:String in SkinImages){
imageLoader.load(new URLRequest(Image));
}
I doubt it works, I think you cannot load many images at once by using one loader.
Try this:
public class Tab extends Sprite
{
var imageOverLoader:Loader = new Loader();
var imageOutLoader:Loader = new Loader();
var TabSprite:Sprite = new Sprite();
var SkinImages:Array = Skin.TAB_ACTIVE,Skin.TAB_DISABLED,Skin.TAB_HOVER,Skin.TAB_VIEW];
public function Tab()
{
TabSprite.buttonMode = true;
this.addChild(TabSprite); // you also need to add as a Child "Tab" object in the Main Class
imageOutLoader.load(new URLRequest(Skin.TAB_VIEW));
imageOverLoader.load(new URLRequest(Skin.TAB_HOVER));
TabSprite.addChild(imageOutLoader);
TabSprite.addEventListener(MouseEvent.ROLL_OVER, onTabHover);
TabSprite.addEventListener(MouseEvent.ROLL_OUT, onTabOut);
}
private function onTabHover(e:MouseEvent){
TabSprite.removeChild(imageOutLoader);
TabSprite.addChild(imageOverLoader);
trace("HOVER");
}
private function onTabOut(e:MouseEvent){
TabSprite.removeChild(imageOverLoader);
TabSprite.addChild(imageOutLoader);
trace("OUT");
}
}
Try this.