ActionScript 3.0: load an image programmatically - actionscript-3

I'm developing an Blackberry Playbock app with ActionScript 3.0. I'm very very new with this.
I have the following project structure (I'm using Adobe Flash Builder "Burrito"):
project
|
src
|
assets
|
images
On image folder I have several PNGs images that I want to load programmatically.
How can I do that?
And
What GUI component I must use to show an image?

This example loads one image and uses buttons to change it:
// create a Loader object to hold things that you will load
var myLoader:Loader = new Loader();
// position the Loader
myLoader.x = 250;
myLoader.y = 0;
// put something into the Loader
myLoader.load(new URLRequest("tree.jpg"));
// make the Loader visible
addChild(myLoader);
// button listeners
top_btn.addEventListener(MouseEvent.CLICK, loadPhoto);
last_btn.addEventListener(MouseEvent.CLICK, unloadAny);
// button functions
function loadPhoto(e:MouseEvent):void {
myLoader.load(new URLRequest("sailboat.jpg"));
addChild(myLoader);
}
// this function empties the Loader object
function unloadAny(e:MouseEvent):void {
removeChild(myLoader);
}

Use the Loader class.

if u want to show multiple image using for loop then follow this code:
var IMAGE_URL:String = new String("image/");
for(var i=1;i<=13;i++){
addChild(imageLoder(i,i*25));
}
private function imageLoder(ranvalue:int,cor:int):Loader{
var ldr:Loader = new Loader();
ldr.load(new URLRequest(IMAGE_URL+ranvalue+".jpg"));
var xcord=5;
var ycord=5;
ldr.x = cor;
ldr.y = ycord+20;
return ldr;
}

Related

Unloading an swf file and loading another swf file in as3

I want to unload a previously loaded swf file(welcome.swf) and load another swf file(topics.swf) when a user clicks a button that is located inside the first loaded swf file(i.e the button, is nested inside the welcome swf file that is to be unloaded).I have tried all manner of codes but none seems to be working and Im at my wits end.Kindly someone assist.Thanks in advance.Below is the code in my main file and the external swf that is to be unloaded.
Main file
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
box.addChild(loader);
External swf file
var loader = new Loader();`
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loader.unloadAndStop();
loader=null;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
loader.x = 0;
loader.y = 0;
addChild(loader);
}
I think you should load topics.swf at Main Loader.
So you should access Main Loader from welcome.swf.
Doesn't this code work?
External swf file
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
var target: Object = this;
// Search the Main Loader from child swf.
while (target.parent){
target = target.parent;
if (target is Loader){
var loader:Loader = target as Loader;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
break;
}
}
}
I would probably move all the loading/unloading responsibility in a main file, with inner swf just letting the main know about some events inside through loaderInfo.sharedEvents just like this:
main file:
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
function topicsRequestedHandler(event:Event):void {
loader.contentLoaderInfo.sharedEvents.removeEventListener(
"loadTopics", topicsRequestedHandler);
loader.unloadAndStop();
//load "Topics.swf" here
}
loader.contentLoaderInfo.sharedEvents.addEventListener(
"loadTopics", topicsRequestedHandler);
box.addChild(loader);
and in external file:
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loaderInfo.sharedEvents.dispatchEvent(new Event("loadTopics"));
}
(not tested but should work)
So using this scheme for all the loaded files, you will keep loading/unloading management in one place.

AS3 browse and load video

I'm currently working on a AS3 AIR project which involves allowing the user to browse a video file and load that same video onto the stage. I've managed to allow the user to browse for a video file type and according to my traces it completes loading the video but this is as far as I've got. There is plenty of tutorials which involve how to load video files from local sources or external links but nothing to show me what to do with a browsed file to display it on the stage. Here is the code so far for browsing to the video file:
private function browseVideo():void {
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, videoFileSelected);
var videoTypeFilter:FileFilter = new FileFilter("Video files", "*.3g2; *.3gp; *.asf; *.asx; *.avi; *.flv; *.m4v; *.mov; *.mp4; *.mpg; *.rm; *.swf; *.vob; *.wmv;");
fileReference.browse([videoTypeFilter]);
}
private function videoFileSelected(e:Event):void {
fileReference.addEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.addEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
fileReference.load();
}
function onVideoFileLoaded(e:Event):void {
var fileReferenceTarget:FileReference = e.target as FileReference;
var data:ByteArray = fileReferenceTarget["data"];
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
var videoLoader:Loader = new Loader();
videoLoader.loadBytes(data);
videoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onVideoLoaderComplete);
trace("video file loaded");
}
function onVideoFileLoadError(e:Event):void {
fileReference.removeEventListener(Event.COMPLETE, onVideoFileLoaded);
fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onVideoFileLoadError);
trace("video file load failed");
}
function onVideoLoaderComplete(e:Event):void {
var loadedContent:DisplayObject = e.target.content;
var loader:Loader = e.target.loader as Loader;
scope.addChild(loader);
}
To play a video using AS3 ( Flash ) you can use a Video object on which you can attach a NetStream object, you can also use an FLVPlayback component. For flex, take a look on my answer of this question where I put an example of playing a video stream. And in all cases, I think that you don't need a FileReference object because a File is suffisant to get the path of your local file and then play it with any manner you want.
Take a look on this example :
function browseVideo():void {
var file:File = new File();
file.addEventListener(Event.SELECT, videoFileSelected);
file.browse([videoTypeFilter]);
}
function videoFileSelected(e:Event):void {
playVideo(e.currentTarget.nativePath);
}
function playVideo(video_path:String){
// using a Video + NetStream object
var nc:NetConnection = new NetConnection();
nc.connect(null);
var ns:NetStream = new NetStream(nc);
ns.client = this;
var video:Video = new Video();
video.attachNetStream(ns);
addChild(video);
ns.play(video_path);
// using an FLVPlayback component inserted on the Stage
flvplayback.load(video_path);
flvplayback.play();
}
For more details on how to work with video, you can take a look here, you can find all what you need to know about video ( loading videos, supported formats, cue points, ... ).
Hope that can help.

Go to specific frame after external swf has been loaded AS3

I have been looking for solutions around here but I can't seem to get it right.
Basically I am trying to load an external swf after clicking on a 'Next' button and it will automatically go to a specific frame eg. frame 8 instead of frame 1.
At first I've got an error of using of using MovieClip function in a Loader and such.
Here's my code
nextBtn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_1);
var fl_Loader1:Loader;
var fl_ToLoad1:Boolean = true;
function fl_ClickToLoadUnloadSWF_1(event:MouseEvent):void
{
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
var fl_Loader1:MovieClip = event.target.content as MovieClip;
fl_Loader1.gotoAndStop(8);
}
else
{
fl_Loader1.unload();
removeChild(fl_Loader1);
fl_Loader1 = null;
}
fl_ToLoad1 = !fl_ToLoad1;
}
You can access content of loaded swf only after event. Complete was dispatched while loading swf file on to the stage.
Define event handler method to start from 8 th frame
function loaderCompleteHandler(evt:Event):void {
var loadedMovie:MovieClipp = evt.currentTarget.content as MovieClip;
loadedMovie.gotoAndStop(8);
}
Replace if block with below lines of code
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
}
Happy coding :)

Add bitmap to stage from CameraRoll loaded image

I need to use the code below to load an image from the CameraRoll with Adobe Air on iOS 8. (it will also be used to read the EXIF data from the loaded image)
I would like to add the bitmap via addChild() to the stage as soon as the onMediaLoadedCameraRoll function gets triggered. How to do that?
var loaderCameraRoll:Loader
var deviceCameraRoll:CameraRoll
var dataSourceCameraRoll:IDataInput;
var mediaPromiseCameraRoll:MediaPromise;
function loadImageFromCameraRoll(e:Event=null):void {
deviceCameraRoll = new CameraRoll();
deviceCameraRoll.addEventListener(MediaEvent.SELECT, onSelectCameraRoll);
deviceCameraRoll.browseForImage();
}
function onSelectCameraRoll(event:MediaEvent):void {
mediaPromiseCameraRoll = event.data;
dataSourceCameraRoll = mediaPromiseCameraRoll.open();
var eventSource:IEventDispatcher = dataSourceCameraRoll as IEventDispatcher;
eventSource.addEventListener( Event.COMPLETE, onMediaLoadedCameraRoll );
}
function onMediaLoadedCameraRoll(event:Event):void {
// display loaded image
}
The documentation says this about the matter:
The data property is a MediaPromise object, which you can load using the loadFilePromise() method of the Loader class.
This is followed by an example that does exactly that:
var imagePromise:MediaPromise = event.data;
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, imageLoaded );
imageLoader.loadFilePromise( imagePromise );
As seen in the example code, you should always add the listeners for a Loader to its contentLoaderInfo property.

Loading another swf file using AS3

I'm trying to load in another swf on a button click using Aaction Script 3.
The problem I'm having is that it just seems to load and mix the movies together. Is is possible to load and replace on stage the newly loaded swf similar to how you could do this in AS2 using loadMovieNum()
This is what I have so far:
//Add event listener for button click
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
var loader:Loader = new Loader()
loader.load(request);
addChild(loader);
}
Many thanks
Use the loader like this:
//Add event listener for button click
var singleLoader:Loader = new Loader();
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
singleLoader.load(request);
addChild(loader);
}
What you're doing is you're creating a new Loader every single time for every new SWF you're loading. Just use a single loader and each time you load content on it, it should replace the existing content. If not, adjust the code like so:
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
singleLoader.unloadAndStop(true);
singleLoader.load(request);
addChild(loader);
}
See the documentation for more: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html
Update
If you want to clear everything off the stage when you do this, see the following question & answer My Actionscript 3.0 Stage will not clear.