try catch issues actionscript3 IOErrorEvent - actionscript-3

This is image share app written in as3. in my ftp there is a file "/Images" and images are inside of that. names are 1.jpg 2.jpg 3.jpg until 21.jpg .(21 images inside of folder). as you can see there are 2 variable totalImages and imageNumber. I entered totalImages = 100.
when I click next button its going next page(next image. Starting 1.jpg). But when it comes image 21(last image),if I click next button i cannot pass image 22.(there is no 22.jpg).And I take this error on Output on Flash. Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never Completed. I want to use try catch method and see error inside of swf. but I dont know where i put try and catch in the code. Thank you in advance
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;
//*****************CREATING VARIABLES*****************************//
//the array has all picture labels
//var myGlow:GlowFilter = new GlowFilter(0xffffff,1,10,10,255);// saving the glow
//var myShadow:DropShadowFilter = new DropShadowFilter(6);// saving the shadow to be applied later
var totalImages=100;
var imageNumber=1;
var myLoader:Loader = new Loader();
//************LOADING IMAGES TO STAGE********************//
//elearningau.com/nondynamic/
var myRequest:URLRequest = new URLRequest("http://www.elearningau.com/nondynamic/Images/"+imageNumber+".JPG");
myLoader.load(myRequest);
addChildAt(myLoader,1);// will be added under the buttons layer but over the texture
//************CENTERING THE PICS****************//
myLoader.contentLoaderInfo.addEventListener(Event.INIT, getImageInfo);
function getImageInfo(event:Event){
myLoader.width = 485;
myLoader.height = 360;
var imgX=(stage.stageWidth- myLoader.width)/(-1)*(1/25);
var imgY=(stage.stageHeight- myLoader.height)/2;
myLoader.x=imgX;
myLoader.y = imgY;// lines 37,38,39 & 40 are centering the loader formulae
//myLoader.filters = [myGlow, myShadow];// adding a white color glow / grey shadow
//var myTween:Tween = new Tween(myLoader, "alpha", None.easeNone, 0,1,2,true);//apply fade in
}
//**************GOING TO NEXT IMAGE********************************//
rightButton.addEventListener(MouseEvent.CLICK, nextImage);
function nextImage(event:MouseEvent){
if(imageNumber<totalImages)
{
imageNumber++;
}
else (imageNumber=1);
reload();
}
//**************GOING TO PREVIOUS IMAGE********************************//
leftButton.addEventListener(MouseEvent.CLICK, previousImage);
function previousImage(event:MouseEvent){
if(imageNumber>1)
{
imageNumber--;
}
else (imageNumber=totalImages);
reload();
}
//*****************RELOADING****************************//
//elearningau.com/nondynamic/
function reload(){
removeChild(myLoader);
myRequest= new URLRequest("http://www.elearningau.com/nondynamic/Images/"+imageNumber+".JPG");
myLoader.load(myRequest);
addChildAt(myLoader,1);// will be added under the buttons layer but over the texture
}

You are missing the error handlers from your loader:
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
function onError(e:IOErrorEvent):void {}
try/catch does not work with asynchronous events, and load is asynchronous.

Related

How to successfully unload a swf, and go to parent swf, after pressing the exit button

Using a code snippet in as3 flash, but struggling for it to actually work:
I would like to unload the child SWF (which is a video) and go back into the parent SWF (a video selection page). Tried so many different ways and need a quick and easy solution. Thanks.
Below does not seem to work...
exit_btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
import fl.display.ProLoader;
var fl_ProLoader:ProLoader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;
function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
fl_ProLoader.unload();
removeChild(fl_ProLoader);
fl_ProLoader = null;
}
I use this code it's tested
import flash.filesystem.*;
import flash.events.MouseEvent;
import flash.net.*;
import flash.events.*;
var _file: File;
_file = File.documentsDirectory.resolvePath("./Directory to swf file/mySwf.swf");
if (_file.exists) {
trace("SWF loading ...........");
displayingSWF(_file.nativePath)
} else {
trace("the file doesn't exit");
}
//////////////// DIsplay the SWF story file //////////////////
var mLoader: Loader ;
function displayingSWF(lnk) {
var inFileStream: FileStream = new FileStream();
inFileStream.open(_file, FileMode.READ);
var swfBytes: ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
mLoader = new Loader();
var loaderContext: LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoadComplete);
mLoader.loadBytes(swfBytes, loaderContext);
}
function onSwfLoadComplete(e: Event): void {
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoadComplete);
addChild(mLoader);
}
exit_btn.addEventListener(MouseEvent.CLICK, dimising);
function dimising(e: MouseEvent): void {
mLoader.unloadAndStop();
removeChild(mLoader);
}

How to add two UILoader inside a UILoader and save the contents of the UILoader using actionscript 3

I have been into a research these past hours in figuring out how to add a UILoader inside a UILoader and save all the contents of a UILoader. I have two instance of UILoader (loader_inner and loader_outer) and they are both placed manually using the UILoader component.
The code goes something like this:
loader_inner.load(new URLRequest("background_1.jpg"));
loader_outer.load(new URLRequest("rabbit.jpg"));
loader_outer.addChild(loader_inner);
var bitmapData:BitmapData = new BitmapData(loader_outer.width,loader_outer.height);
bitmapData.draw(loader_outer);
var jpgencoder:JPGEncoder = new JPGEncoder(100);
var mybyte:ByteArray = jpgencoder.encode(bitmapData);
var myfile:FileReference = new FileReference();
myfile.save(mybyte,"test.jpg");
The problem is during save, I can only see the a white image in file but not the two contents. I am expecting a background with a rabbit in the image file that is placed exactly on the two components drawn manually in the work space.
UILoader.load is an asynchronous action. As such, you are attempting to draw the loader to a bitmap before it has actually had a chance to load the images. You will need to listen for COMPLETE events on both inner and outer loaders:
import com.adobe.images.JPGEncoder;
import flash.display.Bitmap;
import flash.events.Event;
loader_inner.addEventListener(Event.COMPLETE, onLoadComplete);
loader_outer.addEventListener(Event.COMPLETE, onLoadComplete);
loader_inner.load(new URLRequest("background_1.jpg"));
loader_outer.load(new URLRequest("rabbit.jpg"));
loader_outer.addChild(loader_inner);
function onLoadComplete(e:Event):void
{
// check to make sure that *both* loaders have fully loaded
if(loader_inner.percentLoaded && loader_outer.percentLoaded)
{
var bitmapData:BitmapData = new BitmapData(loader_outer.width,loader_outer.height);
bitmapData.draw(loader_outer);
var jpgencoder:JPGEncoder = new JPGEncoder(100);
var mybyte:ByteArray = jpgencoder.encode(bitmapData);
var myfile:FileReference = new FileReference();
myfile.save(mybyte,"test.jpg");
}
}

Loading multiple external swf and navigate using buttons on child

I'm trying to load and unload many external swf files into main blank swf. And use navigation buttons (next/back) that located on the externally loaded child pages to navigate (load and unload pages).
I tried to dispatch event from child to main to unload. It works fine only with the first page, as the dispatcher is bubbling only once.
I have a default page to be loaded and dispatcher listener at the main swf as follows:
var cont:Sprite= new Sprite();
var swfLoader:Loader = new Loader();
var swfFile:URLRequest = new URLRequest("Page1.swf"); //default page
addChild(cont);
swfLoader.load(swfFile);
cont.addChild(swfLoader);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
function swfLoadedHandler(event:Event):void {
MovieClip(event.currentTarget.content).addEventListener("eventTriggered", removeLoader);
}
function removeLoader(event:Event):void {
SoundMixer.stopAll();
this.swfLoader.unloadAndStop();
this.removeChild(cont);
this.cont = null;
swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoadedHandler);
trace ("dispatched");
}
Also the next button code on child page1.swf is as follwos:
nxt_pg.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_8);
function fl_MouseClickHandler_8(event:MouseEvent):void {
content_mc.gotoAndStop(1);
dispatchEvent(new Event("eventTriggered", true));
SoundMixer.stopAll();
//Code of Loading new page here
}
And the same code for back button.
So, I wonder if anyone could help me with code or better technique..Thanks in advance fore your help.
I worked around this by removing Next and Back buttons from child SWFs and put it on container inside movie clip and change its visibility as needed.
At index page (page0.swf) I simply set 'buttons_mc.visible = false;' and make it visible at rest of pages.
The most important thing here is using 'addChildAt(myLoader,0)' instead of 'addChild(myLoader)' to make the buttons movie clip on top of the loaded SWFs.
All this done only on container code and no need to do any extra coding with the external loaded SWFs.
Below is the code for the container swf:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
var swfNumber=0;
var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest("swf/"+"Page"+swfNumber+".swf");
buttons_mc.visible = false;
myLoader.load(myRequest);
addChildAt(myLoader,0);// will be added under the buttons layer
trace(getChildIndex(myLoader));
//==============
buttons_mc.nxt_pg.addEventListener(MouseEvent.CLICK, nextSwf);
function nextSwf(event:MouseEvent){
if(swfNumber<totalSwf){swfNumber++}
else (swfNumber=0);
reload();
trace("Next");
}
buttons_mc.prv_pg.addEventListener(MouseEvent.CLICK, previousSwf);
function previousSwf(event:MouseEvent){
if(swfNumber>1){swfNumber--}
else (swfNumber=0);
reload();
trace("Back");
}
//==============
function reload(){
if (swfNumber==0){buttons_mc.visible = false;}
else (buttons_mc.visible = true);
myLoader.unloadAndStop();
removeChild(myLoader);
myRequest= new URLRequest("swf/"+"Page"+swfNumber+".swf");
myLoader.load(myRequest);
addChildAt(myLoader,0);
}

ActionScript 3 Flash error 1009 and 2025 combo box

Im trying to insert a music using combo box from the component asset. yesterday works fine, but today suddenly it stopped working and it gave me this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
everytime I clicked on the combo box these error appears, strangely it works fine yesterday.
this is the code for the first frame:
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import flash.media.SoundTransform;
import flash.events.MouseEvent;
//Declare MUSIC Variables
var musicSelection:Sound;
var musicChannel:SoundChannel;
var musicTrack:String = "music/dearly_beloved.mp3";
var musicVolume:Number=0.2;
/*var isMuted = false;*/
loadMusic();
function loadMusic():void
{
//first stop old sound playing
SoundMixer.stopAll();
musicSelection = new Sound();
musicChannel = new SoundChannel();
//create and load the required soun
musicSelection.load(new URLRequest(musicTrack));
//when loaded - play it
musicSelection.addEventListener(Event.COMPLETE, musicLoaded);
}
function musicLoaded(evt:Event):void
{
//finished with this listener
musicSelection.removeEventListener(Event.COMPLETE, musicLoaded);
//play music
playMusic();
}
function playMusic():void
{
//play the random or selected music
musicChannel = musicSelection.play();
//setting the volume control property to the music channel
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
//but add this one to make repeats
musicChannel.addEventListener(Event.SOUND_COMPLETE, playAgain);
}
function playAgain(evt:Event):void {
// remove this listener and repeat
musicChannel.removeEventListener(Event.SOUND_COMPLETE, playAgain);
playMusic();
}
and this is the code for the second frame:
import flash.events.Event;
menuBtn.addEventListener(MouseEvent.CLICK, goToMenu)
function goToMenu(evt:Event):void
{
gotoAndPlay(2);
}
// BUTTON EVENT LISTENERS
musicCombo.addEventListener(Event.CHANGE, updateMusic);
volumeSL.addEventListener(Event.CHANGE, setSlider);
//process COMBO BOX changes
function updateMusic(evt:Event):void
{
if (musicCombo.selectedItem.data == "none")
{
//no music is required so stop sound playing
SoundMixer.stopAll();
}
else
{
//otherwise load in the selected music
musicTrack = "music/" + musicCombo.selectedItem.data;
loadMusic();
}
}
function setSlider(evt:Event):void
{
//identify the button clicked
var mySlider:Object = (evt.target);
//adjusting to volume of the music channel
musicVolume = mySlider.value;
musicChannel.soundTransform = new SoundTransform(musicVolume, 0);
}
the error occurs whenever I clicked on the combo box and when I tried to insert another combo box without putting any data, the same error occurs. hope you guys can help me ASAP cause this is due on friday this week xD
thanks
Alright, found the answer. It seems I need to put these 3 lines of code:
import fl.events.ComponentEvent;
import fl.controls.ComboBox;
import fl.data.DataProvider;

AS3 button firing off multiple times if clicked fast only

ok so, i wrote this code:
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.display.Stage;
stop();
var loader:Loader = new Loader();
var defUrlReq = new URLRequest("indexCoontentLoad.swf");
var urlRequest:URLRequest = new URLRequest();
var myLoadedSwf:MovieClip = null;
var swfStage:Stage = this.stage;
/////////////// INITIATE LOADERS ///////////////
loader.load(defUrlReq);
/////////////// START MAIN HANDLER FUNCTION ///////////////
/////IMPORT DEFAULT SWF /////
loader.contentLoaderInfo.addEventListener(Event.INIT, loadedHandler);
function loadedHandler(event:Event){
myLoadedSwf = event.target.content;
addChild(myLoadedSwf);
trace(myLoadedSwf);
myLoadedSwf.gotoAndPlay("intro");
trace("STEP 1 -- ext def swf loaded");
}
///////END IMPORT. ///////////////
///// START LISTENERS AND THEIR FUNCTIONS /////
load1.addEventListener(MouseEvent.CLICK,btn4Clicked);
load2.addEventListener(MouseEvent.CLICK,btn4Clicked);
load3.addEventListener(MouseEvent.CLICK,btn4Clicked);
///// END LISTENERS /////
///// START FUNCTIONS /////
function btn4Clicked(e:MouseEvent):void { //-- START btn4Loaded
if (e.target == load1 || e.target == load2 || e.target == load3) {
myLoadedSwf.gotoAndPlay("outro");
removeChild(myLoadedSwf);
urlRequest = new URLRequest(e.target.name+".swf");
loader.load(urlRequest);
addChild(myLoadedSwf);
}
}
and it works, once clicked, it does what it has to do. Ofcourse, me trying to break it, i found that if i click the buttons fast, it will re-import the external swfs causing me to have multiple instances of the external swf.
so in short, if i click like normal(slow ) ie like a person that clicked to view a section etc, then its fine, if i click fast or repeated clicking ie like a person that double clicks etc, then the problem occurs.
any ideas how to fix this?
thanks in advance.
edit*** heres a link to test file to show what i mean
http://www.somdowprod.net/4testing/flash/tst
When you set doubleClick to enabled on your movieclip, this will work. The Flash runtime will thencheck for you if it is a double click and only trigger your method once. If you want to listen for the double clicks, you can by changing the event handler.
mySprite.doubleClickEnabled = true;
mySprite.addEventHandler(MouseEvent.CLICK, onClick);
Good luck.
You could try adding a boolean variable that is set to false. Once the .swf is loaded then change that variable to equal true. Then don't let the swf be loaded unless it is set to false. That way it'll only be allowed to be loaded once.
var isLoaded:Boolean = false;
function btn4Loaded(e:Event):void
{ //-- START btn4Loaded
if(!isLoaded)
{
if (e.target == load1 || e.target == load2) {
myLoadedSwf.gotoAndPlay("outro");
removeChild(myLoadedSwf);
urlRequest = new URLRequest(e.target.name+".swf");
loader.load(urlRequest);
addChild(myLoadedSwf);
isLoaded = false;
}
}
} // end btn4Loaded.