Unable to Load an external Swf - actionscript-3

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

Related

How to Load & Unload File SWF at same time

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.

Load Embed SWF on Frame 2?

Is it possible to load on frame 2 a swf embed in another swf?
I need to produce a single, main swf that contains a sound swf. The sound swf contains exported sound files for instantiation in code. The sound swfs vary based on the version of the main swf hence the desire to embed at compile time. I currently embed the sound swf in the main swf code using an [Embed] metadata tag which all works. However, this seems to load the swfs on frame 1 and I'd rather it load on frame 2 after my preloader runs.
I've checked around the web and this question seems to get asked but never really answered.
To clarify, I am embedding a swf and its symbols in a main class with code like this:
[Embed(source = 'sound.swf', symbol = 'music')] public var music:Class;
The embed swf and symbols load on the initial main swf load. If I set the sound swf to export on frame 2, it does not compile. Library contents in the main swf load on frame 2 to allow the preloader in frame 1 to run. I'm trying to find out if there is a way to make the assets in the sound.swf to also load on frame2.
This seems to solve the issue for me. Sample code below. I embed a swf containing sound assets, but use "application/octet-stream" for the mimeType. Then later I can "load" the swf using the ActionScript Loader class. A symbol in the loaded swf can then be instantiated using "getDefinitionByName". Also, you can instantiate using "getDefinition" if you have a reference to the loaded swf, see the commented out line below. So far this works for me.
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.events.Event;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.utils.getDefinitionByName;
public class TestEmbed extends MovieClip
{
[Embed(source="EmbedAudio.swf", mimeType="application/octet-stream")]
public var EmbedAudio : Class;
public function TestEmbed()
{
super();
// load embed swf
var context : LoaderContext = new LoaderContext (false,ApplicationDomain.currentDomain);
context.allowCodeImport = true;
var loader : Loader = new Loader ();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.loadBytes (new EmbedAudio (), context);
}
private function loader_complete(evt:Event):void {
// instantiate the class
/*var ClassDefinition:Class = evt.target.applicationDomain.getDefinition("k3n1_01") as Class;*/
var ClassDefinition:Class=getDefinitionByName("k3n1_01") as Class;
var k3n1_01:Sound = new ClassDefinition();
k3n1_01.play();
}
}
}

button to open a swf in AS 3.0

I have been trying to find the appropriate code for this during hours. But none of the ones I found seem to work. I really need help with it...I am desperate...
I have 2 swf files. I added a button to the end of the 1st swf file (the next button).I want to go to the 2nd swf file when pressing this button. I know how to do it with actionscript 2.0 (LoadMovie)...but I started my swf in actionscript 3.0 and now ...I need to code that in AS 3.0.
Could anybody help me with this code?? I tried with some of the codes i have seen from other users, but i don't know what i am doing wrong that any of them work for me !!
Thank you very much!
This should help:
// Set the URL of the second movie
var url:String = "url_of_your_second_movie"
// Create the loader object using the specified URL
var loader:Loader=new Loader();
var request:URLRequest;
request= new URLRequest(url);
// Set the COMPLETE event handler
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete );
// Start the load
loader.load( request );
// Fill the COMPLETE event handler as needed
private function onComplete(e:Event):void
{
// Add the loaded content to the display list
addChild(e.currentTarget.content);
// Do whatever else is needed once the movie is completed
}

AS3 Loader keeps reloading same swf over and over

I have a flash animation made on the main timeline of the SWF with a couple of layers, some functions and some keyframe labels. For example, i have a movieclip of a star that come across the screen and then triggers a dispatchEvent for the main timeline to go to frame label "next".
Here is a sample of the actionscript used on the main timeline :
Stars.addEventListener("fadeInTitle",_fadeInTitle);
function _fadeInTitle(e:Event):void {
Title.gotoAndPlay("fadeIn");
Stars.removeEventListener("fadeInTitle",_fadeInTitle);
}
stop();
That SWF alone works perfectly. Problem comes when I try to load this SWF into another one.
What happens is the loader keeps reloading the SWF over and over, overlapping them and the actionscript that's on the main timeline of the loaded SWF is ignored, the timeline plays continuously. Here is the code i use to load the SWF :
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad(){
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("Fly.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.target.content);
}
function onProgressHandler(mProgress:ProgressEvent){
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
}
startLoad();
There's nothing special there. Just a simple loader.
I have found a workaround by putting the entire animation inside one main movieclip and put that movieclip on the main timeline (one keyframe, one layer, no actionscript) and then load it. That way it works fine but it feel more like a patch than a solution. I would really like to know why it's bugging when you try to load an external SWF that use the main timeline with multiple layers, keyframes and actionscript.
Any help/hint will be greatly appreciated.
Thanks a lot for reading.
m
First take the loader instantiation out of a function, it is not necessary. Second, kill your listeners in the onComplete function. Third, if this loader code is on the timeline of your loader shell, make sure that there is not more than one frame, or if there is (not sure why there would be though) place a stop on the frame containing the loader code. Or even better, use a Document class to contain your loader code rather than putting it on the timeline. My best guess is you were calling startLoad() over again somehow. Also, make sure you have the flash debug player installed so you are getting proper error reporting when viewing this in a browser.
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("Fly.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
function onCompleteHandler(loadEvent:Event)
{
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
addChild(loadEvent.target.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
}

Unloading swfs loaded with Loader::load()

I'm using Loader::load() successfully to load swfs into my main swf and then I add them as a child of a Sprite. When other events occur I want to remove the swfs as needed. I have looked at unload() and at removeChildAt() without success.
I only added the addChild() call to try to pin down the loaded instance so I could remove it. The loading works fine without the addChild();
I have also tried publishing to player v.10 and using myLoader.unloadAndStop(); but this has no effect either;
The following demo code shows my problem. I see one child added and one removed but intro.swf is still playing away.
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
var myLoader:Loader = new Loader();
var holderMC:Sprite = new Sprite();
var myRequest:URLRequest = new URLRequest('intro.swf');
myLoader.load(myRequest);
holderMC.addChild(myLoader);
trace("initial:"+holderMC.numChildren); // traces initial:1
while(holderMC.numChildren > 0) {
holderMC.removeChildAt(0);
trace("now there are:"+holderMC.numChildren); // traces now there are :0
}
myLoader.unload();
// Edit- also tried:
myLoader.unloadAndStop();
myLoader = null;
Any thoughts?
You definitely have something else going on here. First, how are you seeing the "intro.swf"? You are creating holderMC, and adding the loaded swf as a child, but when are you adding holderMC to the display list?
The proper way to remove a movie from the view would be:
holderMC.removeChild(myLoader);
In order to let the content of holderMC to be flagged for garbage collection you need to set it to null. So,
holderMC.removeChild(myLoader);
myLoader.unload(); // this will set the content (the movie itself) to null.
myLoader = null; // npw the loader can be garbage collected too
If you are doing a removeChild and it is still showing, then you will need to post more code to show where the problem really is.