Unloading swfs loaded with Loader::load() - actionscript-3

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.

Related

Image Overlapping - action script 3 / adobe flash / adobe animate

I am working on a project which is loading png(png1) on run-time in front of another png(png2) image(which is placed earlier)
I have done this and it is working properly, the problem is after some time png1 transparenting to the background even the png2 placed in middle of png1 and background, bellow i have attached screenshots of the issue and the code.
import flash.net.URLLoader;
import flash.net.URLRequest;
var fl_TimerInstance: Timer = new Timer(1000);
fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);
fl_TimerInstance.start();
var fl_SecondsElapsed: Number = 1;
function fl_TimerHandler(event: TimerEvent): void {
var imageLoader: Loader = new Loader();
var image: URLRequest = new URLRequest("C:\\Users\\Public\\Pictures\\pic.png"); //png1 = pic.png
imageLoader.load(image);
Zerolocation.addChild(imageLoader);
}
ScreenShots:
Before Error - https://drive.google.com/file/d/19a0t2jEGfDoX2voQ96rap4XpvDlGMWBd/view?usp=sharing
Error - https://drive.google.com/file/d/1a--EIEXz2Qzt5SBfl8Y8SxDIAG3DkYZf/view?usp=sharing
Timeline - https://drive.google.com/file/d/1s2uPSpOYAcfEJqdNqD4QpDGla8Gvs5LC/view?usp=sharing
It would be much appreciated if anyone can give me a clue about what is wrong the with this.
You need to replace your png1 each time you load a new file. Best if you'd check if the file is still the same so not to download it again and again each second. The reason of why does the png2 stop being displayed is exactly because you have too many transparent layers in front of png2. I recall having 24 pictures with alpha channel on top of each other caused me to lose some background that would otherwise be visible. To solve your issue I say you add a Sprite container in front of your png2 layer wise, then you could just clear all the children of that container to remove obsolete imageLoaders, then add your newly downloaded picture. Something in line of this:
// Zerolocation has a child named "runtimePic" to place loaded pics
var didWeLoad:Boolean=true;
function fl_TimerHandler(event: TimerEvent): void {
if (!didWeLoad) return; // why downloading more while we haven't finished?
var imageLoader: Loader = new Loader();
var image: URLRequest = new URLRequest("C:\\Users\\Public\\Pictures\\pic.png"); //png1 = pic.png
imageLoader.load(image);
imageLoader.addEventListener(Event.COMPLETE,loaded);
didWeLoad=false;
}
function loaded(e:Event):void {
didWeLoad=true; // allowing more downloading
e.target.removeEventListener(Event.COMPLETE,loaded); // clean up, or else you'll leak memory
// remove old pic(s)
Zerolocation.runtimePic.removeChildren();
// and now add new pic
Zerolocation.runtimePic.addChild(e.target);
}
Be warned, this code does not handle downloading errors.

Unable to Load an external Swf

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

StageWebView in an AS3-Script

I allreday read a lot of helpful stuff in that forum. Now it's my first time I ask for specific help.
I'm pretty new to Flash and have a problem I struggle for more then a week now. The most efficient and elegant way for my problem is to put a StageWebView-Call into an as-File.
That would be the plan:
In my flash-File: Show a PDF document "xyz" and put it on the stage.
I alreday tried it with Switch-Case - But then I have trouble to get rid of the PDF's.
That was my Idea:
First the new as-File...
package {
import flash.display.MovieClip;
import flash.media.StageWebView;
import flash.geom.Rectangle;
import flash.filesystem.File;
import flash.display.Sprite;
import flash.display.Stage;
public class mypdf {
public var MyWebView:StageWebView
public var file:String
public var pdf:File
public function mypdf(ActFile:String) {
MyWebView = new StageWebView();
file = ActualFile; //MARKING #1
pdf = File.applicationDirectory.resolvePath(file);
MyWebView.stage = stage; //MARKING #2
MyWebView.viewPort = new Rectangle (200, 200, 400, 400);
MyWebView.loadURL(pdf.nativePath);
}
}
}
Than I want to call that in my flash-File...
stop();
var mynewpdf:mypdf = new mypdf("test.pdf");
Two erros are shown:
1120: Access of undefined property error ActualFile (at Marking #1)
1120: Access of undefined property error Stage (at Marking #2)
With a lot more work I could avoid the first error by defining a lot of different as-Scripts for each pdf.
My main problem is the second error.
It would be really nice if someone had any good ideas.
Bye,
Stephan
The second error means that you need to pass the stage to the web view. Either pass it to mypdf class as parameter, or make mypdf DisplayObject (extend Sprite for example) and add it to stage.
I'm not sure this will solve your issue anyways - I think StageWebView can simply display html. The PDF is displayed in your browser because an external plugin for that is launched.
In AIR the situation seems different: http://sujitreddyg.wordpress.com/2008/01/04/rendering-pdf-content-in-adobe-air-application/
StageWebView is wont support for nativePath, instead of using this, you can try with pdf.url. And StageWebView also having support for open .pdf files.
public function mypdf(ActFile:String) {
MyWebView = new StageWebView();
file = ActualFile; //MARKING #1
pdf = File.applicationDirectory.resolvePath(file);
MyWebView.stage = stage; //MARKING #2
MyWebView.viewPort = new Rectangle (200, 200, 400, 400);
addChild( MyWebView );
MyWebView.loadURL(pdf.url);
}
Because, StageWebView will support file:/// format, but in nativePath we got C://.., so, this will help you. Or
Simply convert your StageWebView to Display object, and then added it to your container by using addElement().
You can convert it by,
var _view:SpriteVisualElement = new SpriteVisualElement();
_view.addChild( MyWebView);
this.addElement( view );
To test by, simply call this method in added_to_stage method to test if stage is having or not. This error will come if stage is not setted means also.

AS3 Setup Buttons for All Frames in Document Class

I'm making a simple interface with Flash. Let's say we've got:
frame 1: 1 button that advances to frame 10 (goto10)
frame 10: 2 buttons, one advances to frame 20 (goto20), one opens a URL (openURL)
frame 20: 3 buttons, one goes back to frame 1 (goto1), one goes to frame 10 (goto10), and one opens a URL (openURL)
package {
import flash.display.MovieClip
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.display.SimpleButton;
public class NKE_DocumentClass extends MovieClip {
public var goto1:SimpleButton = new SimpleButton();
public var goto10:SimpleButton = new SimpleButton();
public var goto20:SimpleButton = new SimpleButton();
public var openURL:SimpleButton = new SimpleButton();
public function NKE_DocumentClass() {
goto1.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{clickGo(self,1)});
goto10.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{clickGo(self,10)});
goto20.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{clickGo(self,20)});
openURL.addEventListener(MouseEvent.CLICK,function(self:MouseEvent):void{urlGo(self,"http://google.com")});
}
public function clickGo(event:MouseEvent, nextCue:int):void {
gotoAndStop(nextCue);
trace("Advanced to: " + nextCue);
}
public function urlGo(event:MouseEvent, goURL:String):void {
var request:URLRequest = new URLRequest(goURL);
new URLLoader(request);
trace("Executed URL: " + goURL);
}
}
}
Problem is, once I leave frame 1, none of the buttons work... they're simply unresponsive. It seems like the code doesn't stay loaded once it leaves frame 1.
Thoughts?
I'm pressuming the problem is because when this code is first executed (as a Document Class) the only button that exists is the button on frame 1? This is under the assumption than you've created buttons in the Flash IDE then added them to the stage from the library on the specific keyframes.
I see you've created the SimpleButtons programmatically but since they haven't been added to the stage in the code you've shown, the presumption is that you've just called them the same names as the buttons you've placed on stage? Correct me if I'm wrong and I'll try to offer some other advice if the below doesn't help.
One solution would be to create them all on the first frame then switch their visibility on and off depending on when you need them.
If you're not sure how:
goto10.visible = false;
etc etc
I can't remember now without testing but if you have placed them all on the stage on different keyframes this may cause a problem.
Back in the days of putting code on the timeline if you put code on frame 1 but it referenced objects that weren't on frame 1 then the code would fail (this is probably what's happening with your document class - it's running when not all objects exist).
I would make sure they're all on one layer without any keyframes, from frame 1, and you just switch their visibility on and off. Alternatively, let your classes add and remove the buttons and other interface elements and don't use the timeline at all.

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