Loader Complete event does not work with browser (AS3) - actionscript-3

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

Related

Flash AS3 load and replace movie

I have a loader mc...an animation that loops - when you click a button I want a new movie to load on top and replace the loader mc
Here is my current code:
btn_load.addEventListener(MouseEvent.CLICK, btn_load_press);
function btn_load_press(e:MouseEvent)
{
var reloadRequest:URLRequest = new URLRequest("new-movie.swf");
var loader:Loader = new Loader();
loader.load(reloadRequest);
addChild(loader);
}
But this puts it on top on my loader...effectively meaning the loader is still running in the background.
This is really simple in AS2 - and I'm not sure if it's even possible in AS3
To do this we'll need to add a listener for your loader.contentLoaderInfos complete event, then remove your animation once everything has finished loading.
btn_load.addEventListener(MouseEvent.CLICK, btn_load_press);
function btn_load_press(e:MouseEvent)
{
var reloadRequest:URLRequest = new URLRequest("new-movie.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loader_complete);
loader.load(reloadRequest);
addChild(loader);
}
function loader_complete(e:Event)
{
e.target.removeEventListener(Event.COMPLETE,loader_complete);
removeChild(loader_mc);
}

Flash Loader works only inside of Flash

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

How to load new swf on mouseclick event of movieclip in as3?

I want to create a flip effect button, on click event of which a external swf should be opened removing all the contents of current swf.
I have created both components, a button and also a movieclip.
Neither of its load ( new URLRequest()) code working.
Below is the complete code I am using. Where btn2 is class name of my movie clip
and 2.swf is external swf I want to load.
stop();
var bc2:btn2 = new btn2();
bc2.buttonMode = true;
bc1.addEventListener(MouseEvent.CLICK, mouseClick);
var loader = new Loader();
function mouseClick(event:MouseEvent): void {
loader.unload();
loader.load(new URLRequest("2.swf"));
addChild(loader);
}
First btn2 instance what you are instantiating needs to be added for it to be visible.
Secondly you are trying to load the swf even before its loaded.
I have modified the code
stop();
var bc2:btn2 = new btn2();
bc2.buttonMode = true;
this.addChild(bc2);
bc2.addEventListener(MouseEvent.CLICK, mouseClick);
var loader = new Loader();
function mouseClick(event:MouseEvent): void {
loader.unload();
loader.load(new URLRequest("2.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
}
function completeHandler(event:Event):void{
this.addChild(loader);
}
This should work...

Can't load image in swf

I get error with load image into swf file. Here is my code:
private var loader:Loader = new Loader( );
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
addChild(loader);
}
e very thing is OK, logo of Google appeared. But I need to manipulate pixel the image have loaded. I add a image Bitmap :
private var loader:Loader = new Loader( );
private var image:Bitmap;
public function Battle()
{
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"));
}
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
this.addChild(image);
}
There is nothing appear. I can't find any solution on internet. I try and detect :
private function onComplete(event:Event):void
{
image = Bitmap(loader.content);
//after above line, rest of function never run
addChild(image); // <-- no run ##
trace("this no run"); // <-- no run ##
}
I use Flash Builder 4 and have no error or any warning.
Can somebody show me a solution, please?
Thank for reading.
You can't access pixel data of an image if image is in different domain than your application (and there is no crossdomain policy file)
1 You need crossdomain.xml on Google site :)
2 You need use checkpolicy:
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.load(new URLRequest("http://www.google.com.vn/images/srpr/logo4w.png"), context);
but it works only if Google add crossdomain.
3 You can use little serverside proxy on your domain. Flash call proxy to get file with your url, proxy download it and return to flash...

Flash Youtube Embed Issue

Ive embedded a youtube video into a flash file with different frames for different pages, basically when i click off the page displaying my video, the youtube video is still on screen and the sound carrys on playing. I want the video only to be displayed on one frame, and the sound to stop when i switched frames. Here is my code:
Security.allowDomain("www.youtube.com");
var player : Object;
var mute : Boolean = false;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
playButton.addEventListener(MouseEvent.CLICK, playVideo);
pauseButton.addEventListener(MouseEvent.CLICK, pauseVideo);
muteButton.addEventListener(MouseEvent.CLICK, muteVideo);
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
}
function onPlayerReady(event:Event):void {
player = loader.content;
player.setSize(520, 265);
player.x = 230;
player.y = 370;
player.cueVideoByUrl("http://www.youtube.com/v/G-P9fO4g2Jc", 0);
}
function playVideo(event:MouseEvent):void {
player.playVideo();
}
function pauseVideo(event:MouseEvent):void {
player.pauseVideo();
}
function muteVideo(event:MouseEvent):void {
if(!mute){
player.mute();
mute = true;
return;
}
else {
player.unMute();
mute = false;
}
}
Thanks in advance
The addChild method adds child to the hole movieClip rather than to the currentFrame, actually there isn't way to add child to the give frame.
Possible solutions are:
Create empty sprite in the frame where you need to insert the player and use this sprite as the holder. Don't forget to stop the video when you leave the frame (you can listen the Event.REMOVED_FROM_STAGE event of player for example and stop video in handler).
add onEnterFrame event listener and check the currentFrame property, when it's the target frame add video otherwise remove video.