I'm an as3 newbie, i have one button as name tombol fuctioning to Loading External swf & going to Specific Frame by clicking buttons, this works fine.
import flash.events.MouseEvent;
tombol.addEventListener(MouseEvent.CLICK, tekan2);
function tekan2 (e:MouseEvent):void {
function loadSWF(swfURL){
var myLoader:Loader = new Loader();
var mySWF:URLRequest = new URLRequest(swfURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
myLoader.load(mySWF);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.currentTarget.content);
loadEvent.currentTarget.content.gotoAndStop(swfFrame);
}
function onProgressHandler(myProgress:ProgressEvent){
var percent:Number = Math.round(myProgress.bytesLoaded/myProgress.bytesTotal*100);
trace(percent+"% loaded");
}
var swfFrame:Number=2;
loadSWF("2.swf");
}
This file save as 1.swf and loading file 2.swf go to frame 2, but file 1.swf still loading, so this script loading 2 file swfs(file 2.swf frame 2 loading with file 1.swf appears). How to file 1.swf can unloading same time when I click the button, so just file 2.swf frame appears on stage.
Generally you can cancel a Loader's load operation by using the command close(). Before cancelling you need to make sure that the Loader actually loads data, otherwise an Error will be thrown:
if (myLoader.contentLoaderInfo.bytesLoaded > 0 &&
myLoader.contentLoaderInfo.bytesLoaded < myLoader.contentLoaderInfo.bytesTotal)
{
myLoader.cancel();
}
EDIT:
You can access the loader of 1.swf by root.loaderInfo.
In your function loadSWF you can add the following code to prevent 1.swf from loading further:
if (root.loaderInfo.loader && root.loaderInfo.bytesLoaded > 0 &&
root.loaderInfo.bytesLoaded < root.loaderInfo.bytesTotal)
{
root.loaderInfo.loader.cancel();
}
However, I'm not completely sure if you really can cancel the loading process of 1.swf (I have never tried out something like this). Architecture-wise, it would make sense to have a third SWF file, which is your Main SWF, and loads your other SWF files (1.swf and 2.swf, ...). In your main SWF you would need to define the Loader as class variable and not as a local variable as you are currently doing. Whenever you load a SWF, check if another loading operation is in progress and cancel it in that case. Ideally, you would place the button tombol in the Main SWF.
Related
I have a code that loads 4 swf files into main swf file one after another , but unfortunately I faced 2 problems : here is my code :
import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
progress_mc.scaleX = 0;
var loaderIndex:Number = -1;
var currentLoader:SWFLoader;
var swfs:LoaderMax = new
LoaderMax({onComplete:completeHandler,onProgress:progressHandler,onChildComplete:childCompleteHandler});
swfs.append(new SWFLoader("part1.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("part2.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("part3.swf", {container:container_mc, autoPlay:false}));
swfs.append(new SWFLoader("part4.swf", {container:container_mc, autoPlay:false}));
function progressHandler(e:LoaderEvent):void {
progress_mc.scaleX = e.target.progress;
}
function childCompleteHandler(e:LoaderEvent):void {
trace(e.target + " loaded");
e.target.content.visible = false;
}
function completeHandler(e:LoaderEvent):void {
trace("all swfs loaded");
progress_mc.visible = false;
initCurrentLoader();
addEventListener(Event.ENTER_FRAME, trackSWFPlayback);
}
function initCurrentLoader() {
loaderIndex++;
if (loaderIndex == swfs.numChildren) {
//reset back to 0 if the last swf has already played
loaderIndex = 0;
}
//dynamically reference current loader based on value of loaderIndex
currentLoader = swfs.getChildAt(loaderIndex);
//make the content of the current loader visible
currentLoader.content.visible = true;
//tell the current loader's swf to to play
currentLoader.rawContent.gotoAndPlay(1);
}
function trackSWFPlayback(e:Event):void {
//trace(currentLoader.rawContent.currentFrame);
//detect if the loaded swf is on the last frame
if (currentLoader.rawContent.currentFrame == currentLoader.rawContent.totalFrames) {
trace("swf done");
//hide and stop current swf
currentLoader.content.visible = false;
currentLoader.rawContent.stop();
//set up and play the next swf
initCurrentLoader();
}
}
load_btn.addEventListener(MouseEvent.CLICK, loadSWFs)
function loadSWFs(e:MouseEvent):void{
load_btn.visible = false;
swfs.load();
}
problem 1 : part1.swf that is a swf file not made with adobe flash does't load. here is the error I can see each time :
RangeError: Error #2006: The supplied index is out of bounds.
at flash.display::DisplayObjectContainer/getChildAt()
at dblogo()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at dbmovie()
SWFLoader 'loader2' (part2.swf) loaded
SWFLoader 'loader1' (part1.swf) loaded
SWFLoader 'loader4' (part4.swf) loaded
SWFLoader 'loader3' (part3.swf) loaded
all swfs loaded
swf done
poblem 2 : I have a line in my script that stops last swf . as I want to load swf files that maybe huge I rather that to unload instead of just stoping swfs . how to unload each swf instead of stoping it .
as I'm completely new to flash and as3 can every one simply edit my as3 to fix these two main problems .
Any Any Any Suggestions are EXTREMELY appreciated...Thanks a lot
I believe this check is wrong - if you would only have one swf the numChildren would be 1 but its index would be 0.
Start with the index 0 instead of -1:
var loaderIndex:Number = 0;
And then:
if (loaderIndex == swfs.numChildren - 1) // index 0 equals numchildren = 1
{
//reset back to 0 if the last swf has already played
// Why do you reset the index if you only want to play the swfs once ? You should just stop here and remove your enterframe event listener
loaderIndex = 0;
}
loaderIndex++;
As for unloading there is a way to remove the loader with swfs.remove(loaderInstance) but you would need to keep the instances of your loaders. In this case you would not need to increase your loaderIndex but always keep it at 0. Another way is swfs.dispose() which will get rid of ALL content in your LoaderMax (in this case you can only do this after the last swf have played)
I am trying to learn Action Script (self study) and therefore I took a project for myself.So this question might be way too simple or idiotic. If it is i apologise.
The goal is simple. I have 2 swf to embed within my swf. when my swf will run, it will load 1st swf by default. when you click a button, it will load the second swf.you can return back to the first swf using a different button.
After researching I came up with the action script mentioned below. The buttons work and the 1st swf work. But the second swf does not load for some reason. No compilation error found (but got an output error "TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event#2e1785d9 to OpenEvent." but i think that is comming for the working swf because of the xml it is trying to load) . wondering why the second swf is not loading even though I used a similar code as the first and how to rectify it.
url to my swf : http://itnotes.in/RLC/swf/Radio/muses-1.2/radio-tv.swf
my fla file (flash cs6 as3) : itnotes.in/RLC/swf/Radio/muses-1.2/radio-tv.fla
Any help deeply appreciated
Security.allowDomain("avastarentertainment.com")
Security.allowDomain("itnotes.in")
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
var Xpos:Number = 110;
var Ypos:Number = 180;
var swf:MovieClip;
var loader:Loader=new Loader();
loader.load(new URLRequest('http://itnotes.in/RLC/swf/Radio/muses-1.2/muses.swf?url=http://listen.181fm.com:8002&lang=auto&codec=mp3&tracking=true&volume=65&autoplay=true&buffering=5&skin=http://itnotes.in/RLC/swf/Radio/muses-1.2/simple-gray/ffmp3-simple-gray.xml&title=Vishara%20Designs'));
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
/////////////////////////////////////////////////////////////////////////////
//Radio Function
radio.addEventListener(MouseEvent.CLICK, RadioBtnClick);
function RadioBtnClick(event:MouseEvent):void{
removeChild(loader);
SoundMixer.stopAll(); //stop all sounds...
loader.load(new URLRequest('http://itnotes.in/RLC/swf/Radio/muses-1.2/muses.swf?url=http://listen.181fm.com:8002&lang=auto&codec=mp3&tracking=true&volume=65&autoplay=true&buffering=5&skin=http://itnotes.in/RLC/swf/Radio/muses-1.2/simple-gray/ffmp3-simple-gray.xml&title=Vishara%20Designs'));
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
}
/////////////////////////////////////////////////////////////////////////////
//TV Function
tv.addEventListener(MouseEvent.CLICK, TvBtnClick);
function TvBtnClick(event:MouseEvent):void{
removeChild(loader);
SoundMixer.stopAll(); //stop all sounds...
loader.load(new URLRequest("http://avastarentertainment.com/avanced2avan/AVAncedPlayer_TX_DeSiRe_TGZ_MS_vww861102_181powerTop40_4_29_16rev11EpCc_SSER.swf"));
loader.x=Xpos;
loader.y=Ypos;
addChild(loader);
}
Your codes don't have any problems, test your project's output on your browser {in maximized window mode}.
Note:
The file
AVAncedPlayer_TX_DeSiRe_TGZ_MS_vww861102_181powerTop40_4_29_16rev11EpCc_SSER.swf
doesn't work in another domain. so it must load within http://avastarentertainment.com/
domain (another contents required for loading this file, which are accessible only on that domain {copyright} )
Noob ask:
I have many separate .swf files, they are like stairs..
for example:
menu.swf load scene1.swf
scene1.swf load scene2.swf
and so on...
There is a button in menu.swf to load scene1.swf and there is a button in scene1.swf to load scene2.swf and so on...
*every .swf file has it own code, I use Adobe Flash CS6 and Actionscript 3.0
I use loader to load those .swf file
var myLoader:Loader = new Loader();
var intro:URLRequest = new URLRequest("Intro.swf");
myLoader.load(intro);
addChild(myLoader);
My problem:
When I create project on menu.swf to menu.exe... press the button... it can't load any .swf file.. T-T"
so.. what should I do..?
here is sample of my project:
https://drive.google.com/folderview?id=0B7S5VF_EUl_dbEg1WmNCYVBfQTA&usp=sharing
*based on my project Little Red Riding Hood.swf is the main menu. It works fine when I run it with .swf
Edit:
This is my full code of the menu, perhaps anyone can help me out.. ^^
*this code works fine when I run it with .swf
btnRead.addEventListener(MouseEvent.CLICK, read);
btnPlay.addEventListener(MouseEvent.CLICK, read);
function read(event:MouseEvent):void {
var myScene1:Loader = new Loader();
var scene1:URLRequest = new URLRequest("scene1.swf");
myScene1.load(scene1);
addChild(myScene1);
}
btnAbout.addEventListener(MouseEvent.CLICK, about);
function about(event:MouseEvent):void {
gotoAndStop(4);
}
btnScene.addEventListener(MouseEvent.CLICK, scene);
btnScene1.addEventListener(MouseEvent.CLICK, scene);
function scene(event:MouseEvent):void {
gotoAndStop(5);
}
import flash.system.fscommand;
btnQuit.addEventListener(MouseEvent.MOUSE_DOWN, closeApp);
function closeApp(event:MouseEvent):void {
fscommand("quit");
}
I am using this code for load and unload an external swf for a button. when I click on button, swf loads and when again I click on button swf unloads, but sound still exist and just swf screen disappeares.
Please correct this code, I want swf to completetely unload.
printer.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
var fl_Loader:Loader;
//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
{
if(fl_ToLoad)
{
fl_Loader = new Loader();
addChild(fl_Loader);
fl_Loader.load(new URLRequest("quiz.swf"));
printer.x=100;
printer.y=100;
}
else
{ fl_Loader.unloadAndStop();
removeChild(fl_Loader);
fl_Loader = null;
printer.x=155;
printer.y=334;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad = !fl_ToLoad;
}
If you can access the code in loaded swf than you may add Event.UNLOAD listener. In the event handler you can do some cleaning:
stop all sounds,
stop videos,
remove event listeners (specially these attached to the Stage)
This way, the unloading should be smooth and easy.
I am working in flash and as3. I am new to as3. I was trying to load and unload the SWF file.
My project contains 2 files, one is index file and the other is animal file. In the index page I have button for animal. When I click on this button the animal SWF starts executing. But the problem is when I click on index button of animal SWF, it is not showing index page again, instead it is showing message as
Unable to load SWF
My index page code is:
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
swfLoadError);
function swfLoadComplete(evt:Event):void
{
var loader:Loader = Loader(evt.target.loader);
addChild(loader.content);
swfLoader.removeEventListener(Event.COMPLETE, swfLoadComplete);
}
function swfLoadError(evt:IOErrorEvent):void
{
trace("Unable to load swf ");
swfLoader.removeEventListener(IOErrorEvent.IO_ERROR, swfLoadError);
}
So what to do to load the index SWF from animal SWF?
You probably forgot to add to the displaylist. Use addChild.
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
this.addChild(swfLoader);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html
Update: I see its not loaded. You have an IOErrorEvent.IO_ERROR. Mostly this means the file is not located at that url. Check the location or use a http debugger to find out which location you are trying to open.