access of content of loaded swf - actionscript-3

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.

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.

Flash-Animated-GIF-Library

I'm trying to use the Flash-Animated-GIF-Library. It serves to load an animated gif. It does it with the fileReference class, where you have to browse your folders, choose an animated gif and then it'll show it on the stage. I need the animated gif to show without that browsing part. Is it in anyway possible to use that class to load animated gifs directly, the same way you'd load and show an image with the Loader class? If so - how?
Yes, you have 2 options.
Use the Loader or URLLoader class. Example1, Example 2 (get bytearray)
Embed the image and get ByteArrayAsset. Example
The minimal code for option1 (Loader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"));
}
private function loaderComplete(event:Event):void
{
    var loaderInfo:LoaderInfo = LoaderInfo(event.target);
    var byteArray:ByteArray = loaderInfo.bytes;     
player.loadBytes(byteArray);
}
The minimal code for option1 (URLLoader):
protected function handleCreationComplete(event:FlexEvent):void
{
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, loaderComplete);
loader.load(new URLRequest("yourgif.gif"))
}
private function loaderComplete(event:Event):void
{
player.loadBytes(event.target.data);
}
And for option2:
[Embed(source="yourgif.gif",mimeType="application/octet-stream")]
public var YourGif:Class;
protected function handleCreationComplete(event:FlexEvent):void
{
var byteArrayAsset:ByteArrayAsset = new YourGif();
player.loadBytes(byteArrayAsset);
// should work, too
//player.loadBytes(new YourGif() as ByteArray);
}

ActionScript URLLoader URLRequest

I've got some problem with EventListener for loader:URLLoader. How can I determine if a file is already uploaded to the server or not?
var myRequest:URLRequest = new URLRequest("script.php");
loader.load(myRequest);
loader.addEventListener(Event.COMPLETE, redirect);
private function redirect(event:Event):void
{
navigateToURL(new URLRequest("http://example.com/"), "_self");
}
How can I determine if a file is already uploaded to the server or not?
If you want to see if a file exists, then you can add a listener for an IOErrorEvent.IO_ERROR to the URLLoader alongside your listener for Event.COMPLETE.
var urlRequest:URLRequest = new URLRequest("http://bleh.com/file.php");
var urlLoader:URLLoader = new URLLoader(urlRequest);
urlLoader.addEventListener(Event.COMPLETE, complete);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, failure);
function complete(e:Event):void
{
initialize(true);
}
function failure(e:IOErrorEvent):void
{
initialize(false);
}
function initialize(fileExists:Boolean):void
{
urlLoader.removeEventListener(Event.COMPLETE, complete);
urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, failure);
trace(fileExists);
}

AS3 Attaching event listeners to elements of an array

I am having trouble attaching eventListeners to the elements within my array, any help/suggestions would be great.. thanks for reading
var urls:Array=["../showcaseThumbnails/1.jpg",
"../showcaseThumbnails/2.jpg",
"../showcaseThumbnails/3.jpg",
"../showcaseThumbnails/4.jpg",
"../showcaseThumbnails/5.jpg",
"../showcaseThumbnails/6.jpg"];
var loader:Loader;
for (var i:int=0;i<urls.length;i++)
{
loader=startLoading(urls[i]);
loader.y=510;
loader.x=i*54+314;
addChild(loader);
}
function startLoading(url:String):Loader
{
var loader:Loader=new Loader();
loader.load(new URLRequest(url));
return loader;
}
I guess you don't need one more array. you can use loaderInfo's url property to find out the URL.function onLoad(e:Event):void {
var url:String = e.traget.url;
var index:Number = urls.indexOf(url);
}
var urls:Array=["../showcaseThumbnails/1.jpg",
"../showcaseThumbnails/2.jpg",
"../showcaseThumbnails/3.jpg",
"../showcaseThumbnails/4.jpg",
"../showcaseThumbnails/5.jpg",
"../showcaseThumbnails/6.jpg"];
var loaders:Array = [];//array to store loaders
var loader:Loader;
for (var i:int=0;i<urls.length;i++)
{
loader=startLoading(urls[i]);
loaders.push(loader);
loader.y=510;
loader.x=i*54+314;
addChild(loader);
}
function startLoading(url:String):Loader
{
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
loader.load(new URLRequest(url));
return loader;
}
function onLoad(e:Event):void
{
var index:Number = loaders.indexOf(LoaderInfo(e.target).loader);
trace(urls[index]);
//call the corresponding function from here based on index.
}

Making loaded image draggable 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();
});