Flash Loader works only inside of Flash - actionscript-3

I need to load flex swf file into a flash (a button on witch I need to apply flex skin). I'm using flash Loader() and inside of Flash it works fine (swf is loaded and displayed). But when I try run a compiled flash swf, the Loader does not load the flex swf. There are no errors handled with IOErrorEvent.IO_ERROR and the ProgressEvent.PROGRESS just sais that 0 bytes was loaded. Than nothing else happen.
Here is a part of my code (just display flex button and set a label):
var flexBtn:String = "button.swf"; //swf file is in the same folder as flash source code
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadComplete);
myLoader.load(new URLRequest(flexBtn));
stage.addChild(myLoader);
var timer:Timer;
function loadComplete(event:Event):void
{
timer = new Timer(200);
timer.addEventListener(TimerEvent.TIMER, timeHandler);
timer.start();
}
function timeHandler(event:Event):void
{
timer.stop();
var myClip:MovieClip = myLoader.content as MovieClip;
myClip.x = 10;
myClip.y = 10;
if (myClip.currentFrame == 2){
myClip.application.flexButton.label = "FlexButton";
}
}
I have tried different versions of SDK and different Loaders but it didn't work.
All suggestion will be appreciated :-)

Add SecurityErrorEvent - I guess it's because of a privacy policy error.
And of course you must be sure they are in the exact same folder (correct url).

Related

how to go to next scene after loaded swf played in As3 Flash cc

i am currently working in adobe flash cc 2014 and i have loaded one .swf file that contains about 5000 frames of animation. And then i want to go to next scene after this loaded file finished playing.
this is the code i have, just simple loader code :
stop();
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("jenissendi.swf");
myLoader.load(url);
addChild(myLoader);
now, what should i do to this code?
can someone give me a simple step, because i am still newbie here
thanks.
The thing about Loader class that can confuse beginners is that events, related to loading process, are dispatched from a LoaderInfo object attached to Loader rather than Loader itself.
stop();
var myLoader:Loader = new Loader;
var url:URLRequest = new URLRequest("jenissendi.swf");
myLoader.contentLoaderInfo.addEventListener(Event.INIT, onInit);
myLoader.load(url);
addChild(myLoader);
function onInit(e:Event):void
{
nextScene();
}
First, you will need to listen for when your content has completed it's load - as you won't know how many frames the content has until then.
Then, you need to figure out when the timeline of that loaded content has finished playing.
Here is a code example with comments explaining what's going on.
stop();
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("jenissendi.swf");
//before you load, listen for the complete event on the contentLoaderInfo object of your loader
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, contentLoaded, false, 0, true);
myLoader.load(url);
addChild(myLoader);
//this function runs when the content is fully loaded
function contentLoaded(e:Event):void {
var loadedSwf:MovieClip = myLoader.content as MovieClip; //this is the main timeline of the loaded content
loadedSwf.addFrameScript(loadedSwf.totalFrames - 1, contentFinished);
//the line above tells the movie clip to run the function 'contentFinished' when it reaches the last frame.
}
//this function runs when the loaded content reaches it's last frame
function contentFinished():void {
//clean up to avoid memory leaks
removeChild(myLoader);
loadedSwf.addFrameScript(loadedSwf.totalFrames - 1, null);
nextScene();
}
addFrameScript has a few nuances. First, is that it reads frame numbers as 0 based. So that means the first frame is frame 0. That's why you subtract 1 from the totalframes to get the last frame. Second, addFrameScript is an undocumented feature - which means it may on some future flash player/AIR release not work anymore - though that's very unlikely at this point.
It's also very important to remove your frame scripts (by passing null as the function) to prevent memory leaks.

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

Loader Complete event does not work with browser (AS3)

Im trying to load an image in a movie clip and change its size as follow:
var loader:Loader = new Loader();
public function setProfilePicture(url:String){
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplte);
loader.load(new URLRequest(url));
addChild(loader);
}
protected function onComplte(event:Event):void
{
EventDispatcher(event.target).removeEventListener(event.type, arguments.callee);
var image:DisplayObject = (event.target as LoaderInfo).content;
image.width = 132;
image.height = 132;
}
The code above works fine when I execute it with adobe flash CS5, but when I try to open it with a browser (e.g. Chrome), the size of images does not change to 132x132. I tried to put addChild(loader) in the onComplete function, but in this time when I open it with a browser, the image won't be even loaded, while executing with adobe flash CS5 remains as before.
My suggestion is that when we open it by browser, the function onComplete does not work, but WHY???
Any idea will be appreciated.
Try this hack:
protected function onComplte( event:Event ):void {
EventDispatcher( event.target ).removeEventListener( event.type, arguments.callee );
var loaderInfo:LoaderInfo = LoaderInfo( event.target );
loaderInfo.loader.scaleX = 132 / loaderInfo.width;
loaderInfo.loader.scaleY = 132 / loaderInfo.height;
}
check this link: actionscript3 (flash) don't load images form user file in chrome
Verify that it works in a different browser other than chrome. This is most likely the pepper flash problem in chrome
I played with your onComplte function and somehow the content property of LoaderInfo is not accessible from in there. If all else fail, the size of the image can still be controlled from within setProfilePicture by scaling the Loader:
public function setProfilePicture(url:String){
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplte);
loader.load(new URLRequest(url));
loader.scaleX = 10; ////
loader.scaleY = 10; ////
addChild(loader);
}

Preload a bit of FLV or F4V with the main project loader

My project has a main loader that load all the assets for the project. I need to load a bit of my video with it.
The video has 16mb, i want to load 3mb to use after the main loader is completed.
I've tried to open a new connection using Netconnection/Netstream to load 3mb and close the connection, but when the project starts and the video is played, a new connection is opened loading it from beginning.
I'm trying to find a way that i can use those 3mb already loaded. Doing this way, the user don't need to wait a main loader and a secondary loader (buffertime).
That's my code, sorry guys.
var loader:Loader = new Loader();
var nc:NetConnection = new NetConnection();
var ns:NetStream = new NetStream(nc);
var client:Object = new Object();
var swfRatio:Number;
var videoRatio:Number;
function init():void
{
nc.connect(null);
client.onCuePoint = cuePointHandler;
client.onMetaData = metaDataHandler;
ns.client = client;
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoader);
addEventListener(Event.ENTER_FRAME, progressTotal);
loader.load(new URLRequest("swf/main.swf"));
ns.play("f4v/main_movie.f4v");
ns.pause();
}
function progressLoader(event:ProgressEvent):void
{
swfRatio = (event.bytesLoaded / event.bytesTotal);
}
function progressTotal():void
{
//Here i get the amount that i want to preload from my video, in this case i want 3mb or 3072000 bytes
videoRatio = (ns.bytesLoaded / 3072000);
//This is a variable that i use to fill my loader asset and verify if my content its totaly loaded.
var frameValue:int = ((videoRatio + swfRatio) / 2) * 100;
if (frameValue >= 100)
{
removeEventListener(Event.ENTER_FRAME, progressTotal);
// Here i close my connection, i suppose that i need to use the same connection in my player.
ns.close();
ns = null;
nc.close();
nc = null;
loaderComplete();
}
}
function loaderComplete():void
{
removeChild(assetLoader);
//Here i add my player to the stage, i want to use the preloaded video with him.
addChild(loader.content);
}
function cuePointHandler(infoObject:Object):void {
trace(infoObject.name);
}
function metaDataHandler(infoObject:Object):void {
trace("metaData");
}
Then in my player that i've just loaded and added to the stage i'm using OSMF to help me with controls.
To test the "preloaded video" i'm doing this:
private var mediaPlayer:MediaPlayerSprite;
private function _init(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, _init);
mediaPlayer = new MediaPlayerSprite();
addChild(mediaPlayer);
//And here OSMF start to load the entire main_movie.f4v again.
mediaPlayer.resource = new URLResource("f4v/main_movie.f4v");
}
It looks like your strategy would work if you let the video download completely. I'm assuming if the video downloaded completely, the browser may cache it and make it available to the next NetStream that comes along.
Your strategy looks OK otherwise. What you are doing (playing, then pausing the video immediately) is the way to start buffering the video. But since there are two NetStream's being used (one by the main loader, the other by the OSMF player) this won't work.
Perhaps you can devise a scheme where you pass the NetStream from the main loader into the loaded SWF (main.swf). So that it can use the data it's already downloaded. Just a thought, I've never tried this.
Another idea would be to get the OSMF player in your main.swf to do the buffering. That would mean the buffering would start happening only after main.swf is loaded (which may be too late).

Why do MovieClipLoader events not fire when loaded into an AS3 wrapper?

While trying to answer this question: Call to an AS2 function from the AS3 container I have come across a roadblock. The setup is an AS3 SWF which loads an AS2 SWF, which in turn loads another AS2 SWF. Communication between the AS3 SWF and the parent AS2 SWF is achieved through localConnection.
child_as2.swf - This is a very simple timeline animation of a box moving across the screen with the following code on frame 1:
stop();
function playMovie() {
play();
}
parent_as2.swf - This is the intermediary AS2 container which loads in child_as2.swf. The load is triggered by a LocalConnection call:
import mx.utils.Delegate;
this._lockroot = true;
var container:MovieClip = createEmptyMovieClip("container", 10);
//mustn't cast this or the Delegate breaks
var mcLoader = new MovieClipLoader();
mcLoader._lockroot = true;
mcLoader.onLoadInit = Delegate.create(this,onMCLoadInit);
function onMCLoadInit() {
trace("load init");
container.playMovie();
}
//LocalConnection code
var myLC:LocalConnection = new LocalConnection();
myLC.loadChild = function(){
mcLoader.loadClip("child_as2.swf", container);
trace("loading");
}
myLC.connect("AVM");
parent_as3.swf - This is the outer wrapper, written in AS3. It loads parent_as2.swf, and communicates with it via LocalConnection:
var myLC:LocalConnection = new LocalConnection();
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT,onLoaded);
loader.load(new URLRequest("parent_as2.swf"));
addChild(loader);
function onLoaded(event:Event):void {
//setTimeout hack to circumvent #2000 Security context error
setTimeout(function() {
myLC.send("AVM", "loadChild");
},1);
}
The issue is that the onMCLoadInit function in parent_as2 is never called when it is loaded inside the AS3 wrapper, although the load does take place. The events also fail when using a listener object in place of Delegate. The box from child_as2.swf is visible, but never starts moving. However, if I run parent_as2.swf on it's own and start the load without the LocalConnection it works fine. It also wroks correctly when triggered from an external LocalConnection call. Why does the AS3 wrapper prevent the MovieClipLoader's events from firing?
Update:
So accepting that no events can be fired from the MovieClipLoader in parent_as2.swf, I have modified the code to detect the loadInit state by a combination of polling MovieClipLoader.getProgress() and the existance of a function in child_as2.swf. It's not pretty but it seems to work. I would still much rather be able to offer a solution using events though.
var container:MovieClip = createEmptyMovieClip("container", 10);
var mcLoader:MovieClipLoader = new MovieClipLoader();
var loadStarted:Boolean;
var checkingInt:Number;
function checkProgress() {
var progObj:Object = mcLoader.getProgress(container);
if(progObj.bytesLoaded == progObj.bytesTotal && loadStarted) {
//load complete, wait for loadInit
if(typeof(container.playMovie) == "function") {
//loadInit
clearInterval(checkingInt);
container.playMovie();
}
}
//ensures the first loop is ignored due to inaccuracy with reporting
loadStarted = true;
}
//LocalConnection code
var myLC:LocalConnection = new LocalConnection();
myLC.loadChild = function() {
loadStarted = false;
mcLoader.loadClip("child_as2.swf", container);
checkingInt = setInterval(checkProgress,5);
}
myLC.connect("AVM");
I think its the delegate scope in as2 when loaded into a parent the parent becomes _root.
So "this" would be referring to the parent root where the function does not exist.
Have you tried putting this._lockroot = true; in parent_as2?
Here is a better explanation
Also as a side note to your security hack.
The proper fix for that would be to have the child contact the parent and issue an "I am ready type command" which would start the communication events from parent to child.
setTimeout is just delaying any calls to the child giving it time to initialize which could be bad on slower computers.
I did alot of loading AS2 into AS3 a few years ago. If you can't tell lol