as3 play movieclip once - actionscript-3

I would be very thankful if you help me with this problem.
I´m trying to play in my application for ipad one MovieClip once. i tried to do stopping in this way, but the movie dont stop
var loader:Loader = new Loader();
var swfFile:URLRequest= new URLRequest("/test.swf");
loader.load(swfFile);
movieClip = new MovieClip();
movieClip.addChild(loader);
movieClip.addFrameScript(movieClip.totalFrames - 1, callbackFunc);
movieClip.play();
private function callbackFunc():void
{
movieClip.stop();
}

var loader:Loader = new Loader();
var swfFile:URLRequest= new URLRequest("/test.swf");
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onFileLoaded);
loader.load(swfFile);
//I assume you have declared 'movieClip'?
//if not do:
//var movieClip:MovieClip;
private function onFileLoaded(e:Event):void
{
movieClip = loader.content;
addChild(movieClip);
movieClip.play();
addEventListener(Event.ENTER_FRAME, onEnter, true, 0, false);
}
private function onEnter(e:Event):void
{
if (movieClip.currentFrame == movieClip.totalFrames)
{
movieClip.stop();
removeEventListener(Event.ENTER_FRAME, onEnter, true, 0, false);
//do other stuff
}
}
This should do what you need.

var loader:Loader = new Loader();
var swfFile:URLRequest= new URLRequest("/test.swf");
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
loader.load(swfFile);
private function callbackFunc():void
{
movieClip.stop();
}
function onCompleteHandler(loadEvent:Event)
{
movieClip = MovieClip(loadEvent.currentTarget.content);
addChild(movieClip);
movieClip.addFrameScript(movieClip.totalFrames - 1, callbackFunc);
movieClip.play();
}

Your code will not work because it's not the movieClip that is played, it's the external SWF that you load into it that will play it's keyframes. The created movieClip only has 1 keyframe, and on that 1 keyframe the external SWf is placed. You should add the stop() function into the external SWF. If you do this correct the external SWF plays once, and is then stopped.
You can also wrap the external SWF into a new MovieClip and put the code you already have onto it...
Or If you want full control, you can adapt the external SWF code so that this dispatches an event when the last frame is played. Provide a custom stop / replay function on the SWF which you can then call from the parent SWF.
Good luck!

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

Actionscript 3 auto mouse click on XY stage coordinates

Is there any way to auto-click on XY stage coordinates upon when SWF is loaded?
EDIT: By the way, this is the whole code, and it is a cued youtube video. Just want to click anywhere on the cue to play it automatically after SWF loading.
Security.allowDomain("www.youtube.com");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
loader.content.addEventListener("onError", onPlayerError);
loader.content.addEventListener("onStateChange", onPlayerStateChange);
loader.content.addEventListener("onPlaybackQualityChange",
onVideoPlaybackQualityChange);
}
function onPlayerReady(event:Event):void {
trace("player ready:", Object(event).data);
player = loader.content;
player.setSize(300, 250);
player.cueVideoById("AfTCtRDsWVw",0);
player.mute();
}
function onPlayerError(event:Event):void {
trace("player error:", Object(event).data);
}
function onPlayerStateChange(event:Event):void {
trace("player state:", Object(event).data);
}
function onVideoPlaybackQualityChange(event:Event):void {
trace("video quality:", Object(event).data);
}
Yes, you should just manually dispatch your MouseEvent.CLICK Event to your stage:
var yourX:Number = 40; //Whatever you want
var yourY:Number = 40; //Whatever you want
//Make sure you have a link to the stage (usually after Event.ADDED_TO_STAGE event occured)
stage.dispatchEvent(new MouseEvent(
MouseEvent.CLICK,
true,
false,
yourX,
yourY);
If you need a certain DisplayObject to relate to this MouseEvent, you should pass an additional parameter to the MouseEvent constructor:
var yourX:Number = 40; //Whatever you want
var yourY:Number = 40; //Whatever you want
//Make sure you have a link to the stage (usually after Event.ADDED_TO_STAGE event occured)
stage.dispatchEvent(new MouseEvent(
MouseEvent.CLICK,
true,
false,
yourX,
yourY,
linkToYourRelatedDisplayObject);
==UPDATE==
Well, I've made it. A bit of "hacking" and here it is.
Your youtube player contains a safeLoader object, which contains a videoApplication, which contains a LargePlayerButton. This LargePlayerButton has a MouseEvent.CLICK event listener, so we need to dispatch out event to this button instead of dispatching it to the stage.
Here's the complete code with autoClick simulation:
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.DisplayObjectContainer;
// The player SWF file on www.youtube.com needs to communicate with your host
// SWF file. Your code must call Security.allowDomain() to allow this
// communication.
Security.allowDomain("www.youtube.com");
// This will hold the API player instance once it is initialized.
var player:Object;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
}
function autoClick():void
{
//=========================
//Some nested children, we need to dig through a bit to get to the LargePlayButton
var safeLoader:DisplayObjectContainer = (loader.content as DisplayObjectContainer).getChildAt(0) as DisplayObjectContainer;
var videoApplication:DisplayObjectContainer = safeLoader.getChildAt(0) as DisplayObjectContainer;
var largePlayBtn:DisplayObjectContainer = videoApplication.getChildAt(6) as DisplayObjectContainer;
//=========================
//And finally dispatching our event to this button. It will think that a person has clicked it
largePlayBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK,
true,
true,
stage.stageWidth / 2,
stage.stageHeight / 2));
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(300, 250);
player.cueVideoById("AfTCtRDsWVw",0);
//====================================================
//As long as player is loaded we can call our function
autoClick();
}
It was a nice exercise, cheers :)
Youtube seems to have an intelligent anti hack feature. I'm able to get the children of SafeLoader on the IDE, but on the flash player, or the web, the SafeLoader just isn't accessible, and no children after that can be controlled. This is why the code works on the IDE but not outside. Maybe it has to do with sandbox security, but maybe it's more complex.
I doubt very much that it is possible, for obvious security reasons : http://www.adobe.com/devnet/flashplayer/articles/fplayer10_uia_requirements.html

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

Load External SWF through ComboBox AS3

I am trying to create a ComboBox that loads and unloads an external SWF onto stage using ActionScript 3.0 using Flash CS5.
Currently there are 2 list items in the combo box: Home and About.
Upon selecting Home or About option from ComboBox it displays both Home and About SWF at once when selected.
I only want 1 SWF to be displayed only when selected, not all.
menuList.addItem({label:"Choose"});
menuList.addItem({label:"Home",path:"home_load.swf"});
menuList.addItem({label:"About",path:"about.swf"});
menuList.addEventListener(Event.CHANGE, Home);
menuList.addEventListener(Event.CHANGE, About);
var loader:Loader = new Loader();
loader.unloadAndStop();
function Home(e:Event):void
{
if (e.currentTarget.selectedItem.path)
{
var loader:Loader = new Loader();
//loader.unloadAndStop();
loader.load(new URLRequest("home_load.swf"));
addChild(loader);
//loader.unloadAndStop();
loader.x = 0;
loader.y = 190;
}
}
function About(e:Event):void
{
if (e.currentTarget.selectedItem.path)
{
//loader.unloadAndStop();
var loader:Loader = new Loader();
loader.load(new URLRequest("about.swf"));
addChild(loader);
//loader.unloadAndStop();
loader.x = 0;
loader.y = 190;
}
}
You may have to do addChild after the swf file has completed loading.
//These listeners detect when the file has finished loading, and if the
//correct file is loaded.
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
//The load method then loads the SWF file into the loader object.
swfLoader.load(my_url);
//This function adds the external SWF to the stage.
function finishLoading(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}
//This function displays a message if the file is not found.
function errorHandler(errorEvent:Event):void {
trace("file missing");
}

Instantiate Document Class in as3

hi need to instantiate document class in as3. I have a main movie which will load load.swf. The document class of load.swf is LoadedMovie. i use this code :
public function Main()
{
var url:URLRequest = new URLRequest("load.swf");
loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
loader.load(url);
}
function finishLoading(e:Event)
{
var sn:LoadedMovie=new LoadedMovie();
addChild(sn);
}
it's added the movieclip and but my framescripts were not working. i have stop at last frame.
Is it any other way to do this.
The Loader will contain an instance of your loaded swf's document class once it's loaded. Simply addChild the loader and you're in business.
var url:URLRequest = new URLRequest("load.swf");
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, finishLoading);
loader.load(url);
addChild(loader);
Instanciating from a loaded swf is slightly more complicated, you have to deal with application domains and such.