actionscript 3 external swf loads above the close button - actionscript-3

I'm an as3 newbie, i'm loading external swf files by clicking different buttons but using only one function which works fine.
//main
mainCloseBtn.visible = false;
movieloaderbg.visible = false;
M1L1Btn1.visible = false;
//Load activities
var Xpos:Number = 0;
var Ypos:Number = 0;
var swf:MovieClip;
var loader:Loader = new Loader();
loader.x = Xpos;
loader.y = Ypos;
addChild(loader);
// Btns Universal function
function btnClick(event:MouseEvent):void {
removeChild(loader);
var newSWFRequest:URLRequest = new URLRequest("activities/" + event.target.name + ".swf");
loader.load(newSWFRequest);
loader.x = Xpos;
loader.y = Ypos;
addChildAt(loader,3);
mainCloseBtn.visible = true;
}
// Btn listeners
wrong.addEventListener(MouseEvent.CLICK, btnClick);
// Back btn
backBtn.addEventListener(MouseEvent.CLICK, backBtnClick);
function backBtnClick(evt:MouseEvent):void {
gotoAndStop("M1Lessons");
}
// Video Button
videoBtn.addEventListener(MouseEvent.CLICK, videoBtnClick);
function videoBtnClick(evt:MouseEvent):void {
mainCloseBtn.visible = true;
M1L1Btn1.visible = true;
M1L1Btn1.gotoAndPlay(2);
}
//Main Close Button
mainCloseBtn.addEventListener(MouseEvent.CLICK, mainCloseBtnClick);
function mainCloseBtnClick(evt:MouseEvent):void {
mainCloseBtn.visible = false;
loader.unload();
M1L1Btn1.visible = false;
M1L1Btn1.stop();
}
I'm trying to unload the swfs by clicking on the close button, but the problem is when the swf is loaded i can no longer see the close btn, I've tried to use swap depth but isn't working.
I would really appreciate it if someone could help me!
Thanks

try this:
replace
addChild(loader);
with
addChildAt(loader,0);

Related

ActionScript3 remove child error

I recently have been converting an as2 fla to as3 (new to AS3) and have the entire thing working on export, but I am getting an error when I try to remove previously loaded swf's before a new swf is loaded
ArgumentError: Error #2025: The supplied DisplayObject
must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-11()
I know the error relates to my removeChild code here:
`stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}`
However, I cannot seem to find a suitable rewrite for this code that will work and not have an error. This code IS working, so I'm not sure if it would be worth my time to fix this error, or just leave it. I've already messed with it for a couple days, so at this point it's just frustrating me that I cannot fix it.
The stage mouse click listener is useful in this case because I have a back button not shown in this code that clears the loaded swf's before moving to another scene.
Does anyone see a simple solution for this, or do you think it is unnecessary to pursue since the code does what I require?
ENTIRE CODE:
function launchSWF(vBox, vFile):void {
var swfLoader:Loader = new Loader();
var swfURL:URLRequest = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
}
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
//NEW ERRORS BUT WORKING
stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
}
var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
function fall_bClick(e:MouseEvent):void {
var swfFile:String = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
function face_bClick(e:MouseEvent):void {
var swfFile:String = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
function rott_bClick(e:MouseEvent):void {
var swfFile:String = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
//MORE SWFS...
Any advice anyone has is appreciated
First of all function launchSWF(vBox, vFile):void { isn't closed. You've also got function inside functions which is easy enough for you to solve if you click the lines the curly brackets start and end on to track them.
I can't see anything wrong with the code you said has an error but I'm guessing this isn't all the code. If you using Flash Professisonal you can use permit debugging to show the line the error is on.
EDIT: Please note this hasn't been tested as I'm on my mobile writing out code. That being said this should now work:
var container:MovieClip;
var currentSWF:MovieClip;
var swfFile:String;
var swfLoader:Loader;
var preLoader:Loader;
var swfURL:URLRequest;
init();
function init():void {
preLoader = new Loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
container = new MovieClip();
currentSWF = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
stage.addEventListener(MouseEvent.CLICK, removeSWF);
}
function launchSWF(vBox, vFile):void {
swfLoader = new Loader();
swfURL = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
}
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
}
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
function onProgressHandler(event:ProgressEvent) {
var dataAmountLoaded:Number = (event.bytesLoaded / event.bytesTotal * 100);
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text = int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
function removeSWF (e:MouseEvent):void {
if(vBox.numChildren !=0){
//swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
function fall_bClick(e:MouseEvent):void {
swfFile = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
function face_bClick(e:MouseEvent):void {
swfFile = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
function rott_bClick(e:MouseEvent):void {
swfFile = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
I have this rewritten. I could not get the vBox errors cleared in the original code, and I was getting many other errors with what was posted. The vBox code was seen on a tutorial. I think it was supposed to reference the loader for the preloader and the swf, and vFile was for the actual .swf. The following code preloads multiple swfs and clears them with no errors. I appreciate your help AntBirch. I'm beginning to understand loaders in as3 a little more now.
//LOAD FIRST PIECE ON OPEN (required to removeChild later)
var swfLoader:Loader = new Loader();
var defaultSWF:URLRequest = new URLRequest("load/fall.swf");
swfLoader.load(defaultSWF);
swfLoader.x = 165;
swfLoader.y = 15;
addChild(swfLoader);
//PRELOADER
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
function loadProdComplete(e:Event):void {
trace("swf file loaded");
removeChild(preLoader);
addChild(swfLoader);
}
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
}
//BUTTONS
function btnClick(event:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
addChild(preLoader);
var newSWFRequest:URLRequest = new URLRequest("load/" + event.target.name + ".swf");
swfLoader.load(newSWFRequest);
swfLoader.x = 165;
swfLoader.y = 15;;
addChild(swfLoader);
}
// BUTTON LISTENERS
fall.addEventListener(MouseEvent.CLICK, btnClick);
face.addEventListener(MouseEvent.CLICK, btnClick);
rott.addEventListener(MouseEvent.CLICK, btnClick);
angel.addEventListener(MouseEvent.CLICK, btnClick);
ratts.addEventListener(MouseEvent.CLICK, btnClick);
metal.addEventListener(MouseEvent.CLICK, btnClick);
//etc...
//BACK BUTTON
BB3.addEventListener(MouseEvent.CLICK, BB3Click);
function BB3Click(e:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
this.gotoAndPlay(1 ,"Scene 2")
}

AS3 - my sound player seems to work well but the sound is not there

I've been doing a soundplayer for my assignment.Came across some issues. No compile error neither runtime error is appearing and my sound player functions well but I can't hear the song playing.
var mySound:Sound = new Sound ();
var myURL:URLRequest = new URLRequest ("playList.xml");
var mySoundChannel:SoundChannel = new SoundChannel;
var playing:Boolean = true;
var resumeTime:Number = 0;
var loadedXML:XML;
var mySongs:XMLList;
var myTotal:Number;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(myURL);
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML (e:Event):void {
loadedXML = XML(e.target.data);
mySongs = loadedXML.SONG;
myTotal = mySongs.length();
}
btnPlayPause.buttonMode =true;
btnStop.buttonMode =true;
btnPlayPause.addEventListener(MouseEvent.CLICK, playSound);
btnStop.addEventListener(MouseEvent.CLICK, stopSound);
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
function stopSound (f:MouseEvent):void {
mySoundChannel.stop();
}
Help is appreciated very much.
It seems like you trying to play sound only if it already playing, while stoping sound if it is NOT playing:
function playSound(m:MouseEvent){
if(playing==true){
btnPlayPause.gotoAndStop("lbPause");
mySound.load(myURL);
mySoundChannel = mySound.play(resumeTime);
playing = false;
} else {
btnPlayPause.gotoAndStop("lbPlay");
resumeTime = mySoundChannel.position;
mySoundChannel.stop();
playing = true;
}
}
I see this code like this:
function playSound(){
if(playing){
mySound.play();
} else {
mySoundChannel.stop();
}
}
Maybe this is the root of problem

Movie clips not on the scene automatically playing, when the scene loads

I'm developing an app for android using flash and adobe air for android.
In flash i have a set of Scenes which can be navigated in the following way.
in the lower scenes (morning, noon and evening scenes), i have one movie clip every 5 frames per scene, which have audio embedded in them. on the first frame of the movie clips i have, i call the stop(); function. and at the end of the clip an event is dispatched ( dispatchEvent(new Event ("MEANING_TEXT_COMPLETE")); ), so that code from the main timeline could act accordingly.
Here is the code on my main timeline
import flash.events.MouseEvent;
MovieClip(this.root).stop();
btn_home_parent.addEventListener(MouseEvent.CLICK, goto_dh_morn);
mc_sound.addEventListener(MouseEvent.CLICK, control_sound_morn);
btn_next_morn.addEventListener(MouseEvent.CLICK, goto_next_morn);
btn_prev_morn.addEventListener(MouseEvent.CLICK, goto_prev_morn);
//load all sound files
var mn_1:Mn1 = new Mn1();
var mn_2:Mn2 = new Mn2();
var mn_3:Mn3 = new Mn3();
var mn_4:Mn4 = new Mn4();
var mn_5:Mn5 = new Mn5();
var mn_6:Mn6 = new Mn6();
var mn_7:Mn7 = new Mn7();
var mn_8:Mn8 = new Mn8_();
var mn_9:Mn9 = new Mn9();
var mn_10:Mn10 = new Mn10();
var mn_11:Mn11 = new Mn11();
var mn_12:Mn12 = new Mn12();
var mn_13:Mn13 = new Mn13();
var mn_14:Mn14 = new Mn14();
var frameCount_morn:int = 1;
var lang1Init_morn:Boolean = false;
var lang1:Object;
var ar:Object;
playSound_morn();
function playSound_morn()
{
var currentSound:Sound = new Sound();
channel.stop();
trace("play sound");
currentSound = this["mn_" + frameCount_morn];
channel = currentSound.play();
channel.addEventListener(Event.SOUND_COMPLETE, soundComplete_morn);
trans.volume = 1;
channel.soundTransform = trans;
}
function soundComplete_morn(e:Event):void
{
if (showlang1Text)
{
trace("ar Sound Complete");
ar = getChildByName("arText" + (currentFrame - 1));
ar.visible = false;
trace("ar child acquired");
lang1 = getChildByName("lang1Text" + (currentFrame - 1));
lang1Init_morn = true;
lang1.visible = true;
lang1.play();
lang1.addEventListener("MEANING_TEXT_COMPLETE", translationComplete_morn);
}
}
function translationComplete_morn(e:Event)
{
trace("translate Complete");
ar= getChildByName("arText" + (currentFrame - 1));
ar.visible = true;
lang1 = getChildByName("lang1Text" + (currentFrame - 1));
lang1.visible = false;
}
function goto_next_morn(e:MouseEvent)
{
if (MovieClip(this.root).currentFrame <= 61)
{
var next_frame = MovieClip(this.root).currentFrame + 5;
frameCount_morn = frameCount_morn + 1;
channel.stop();
if (lang1Init_morn){lang1.stop();lang1Init_morn = false;}
MovieClip(this.root).gotoAndStop(next_frame);
playSound_morn();
}
trace("Current Frame: " + currentFrame);
}
function goto_prev_morn(e:MouseEvent)
{
if (MovieClip(this.root).currentFrame > 0)
{
var prev_frame = MovieClip(this.root).currentFrame - 5;
frameCount_morn = frameCount_morn - 1;
channel.stop();
if (lang1Init_morn){lang1.stop();lang1Init_morn = false;}
MovieClip(this.root).gotoAndStop(prev_frame);
playSound_morn();
}
trace("Current Frame: " + currentFrame);
}
function control_sound_morn(e:MouseEvent)
{
control_sound(e);
if (!showlang1Text) {
mc_sound.gotoAndStop(5);
if (lang1Init_morn){
lang1.stop();
lang1Init_morn = false;
ar.visible = true;
lang1.visible = false;
}
}
else {
mc_sound.gotoAndStop(1)
}
}
function goto_dh_morn(e:MouseEvent) {
if (lang1Init_morn){lang1.stop();lang1Init_morn = false;}
channel.stop();
goto_dh(e);
}
the movie clip object is lang1
when i load the scene "Morning Scene" it works perfectly well, as it should. however when i navigate to the "noon scene" or "evening scene", audio from all the movie clips that were put on the "morning scene" automatically starts to play. The funny thing is that those movie clips are not even added to the "noon scene" and "evening scene". Any idea on why this happens or how to fix it.
thanks
as I got no answers. the way I fixed it was to compile each scene to a separate swf file and then load/unload them when needed.

unload external swf (child) from the main screen when it has reached last frame?

I have an external swf loading into a main frame (the URL request), and when the swf reaches it's final frame I need it to UNLOAD itself. I need to do this without any code on the CHILD swf, as this is for an iOS application. Can anyone help?
//start button
start_button_aboriginal.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_3);
import fl.display.ProLoader;
var fl_ProLoader_3:ProLoader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad_3:Boolean = true;
function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void
{
if(fl_ToLoad_3)
{
fl_ProLoader_3 = new ProLoader();
fl_ProLoader_3.load(new URLRequest("myths/myth_aboriginal.swf"));
addChild(fl_ProLoader_3);
fl_ProLoader_3.x = 114;
fl_ProLoader_3.y = 41;
}
else
{
fl_ProLoader_3.unload();
removeChild(fl_ProLoader_3);
fl_ProLoader_3 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_3 = !fl_ToLoad_3;
//here, I want to UNLOAD the external SWF when it is finished playing.
var totFrames:Number=childMC.totalFrames;
var curFrame:Number;
childMC.addEventListener(Event.ENTER_FRAME, remove);
function remove(evt:Event):void {
curFrame=childMC.currentFrame;
if (totFrames==curFrame) {
removeChild(childMC);
}
}
You need to declare childMC in a global scope and the assign the loader content.
and dont declare functions inside of functions!!
something like this NOT TESTET
import fl.display.Loader; // impoerts belong at the top
var fl_ProLoader_3:ProLoader; // then your global vars
var childMC:MovieClip; // instatiate childMC with global scope
start_button_aboriginal.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_3,false,0,false); // listener with weak refference
var fl_ToLoad_3:Boolean = true;
function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void
{
if(fl_ToLoad_3)
{
fl_ProLoader_3 = new Loader();
var url:URLRequest = new URLRequest("myths/myth_aboriginal.swf");
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null); // IOS needs this
fl_ProLoader_3.load(url, loaderContext);
fl_ProLoader_3.addEventListener(Event.COMPLETE, loadCompleteHandler,false,0,false);
}
else
{
if(childMC){
removeChild(childMC);
childMC.unloadAndStop();
childMC = null;
}
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_3 = !fl_ToLoad_3;
}
function loadCompleteHandler(evt:Event):void
{
childMC = evt.target.content as MovieClip;
childMC.addEventListener(Event.ENTER_FRAME, remove);
addChild(childMC);
childMC.x = 114;
childMC.y = 41;
}
function remove(evt:Event):void {
var totFrames:Number=childMC.totalFrames;
var curFrame:Number =childMC.currentFrame;;
if (totFrames==curFrame) {
childMC.removeEventListener(Event.ENTER_FRAME, remove);
removeChild(childMC);
childMC.unloadAndStop();
childMC = null;
}
}

action script 3.0 .I had tried a program that should move the dragger as well as the content movieclip,

In this code i need a progressbar to progress through while the imported .swf file is playing, meanwhile i should able to drag the dragger in the progress bar (i.e the rate of movement of dragger should sync with rate of .swf file playing). I got Argument error: #2109 Frame label 459.99 not found in scene1.
var loader:Loader = new Loader();
playBtn.visible = true;
pauseBtn.visible = false;
btn_00.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_01.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_02.addEventListener(MouseEvent.CLICK, fileLoaded);
function fileLoaded(evt:MouseEvent):void
{
var fileName:String = evt.currentTarget.name;
var fileNumber:String = fileName.split("_")[1];
var urlPath:String = "assets/file_" + fileNumber + ".swf";
loader.load(new URLRequest(urlPath));
addChild(loader);
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
function swfLoaded(event:Event):void
{
addEventListener(Event.ENTER_FRAME, trackPlayback);
}
function trackPlayback(event:Event):void
{
var perPlayed:Number = MovieClip(loader.content).currentFrame / MovieClip(loader.content).totalFrames;
progressbar.drag.x = (progressbar.bar.width - progressbar.drag.width) * perPlayed;
}
progressbar.drag.buttonMode = true;
var dragClicked:Boolean = false;
var xpos:Number = progressbar.bar.x * progressbar.drag.width;
progressbar.drag.addEventListener(MouseEvent.MOUSE_DOWN,dragMouseDown);
function dragMouseDown(evt:MouseEvent):void
{
trace(" inside mouse down ");
dragClicked = true;
progressbar.drag.startDrag(false,new Rectangle(xpos,0,progressbar.width-progressbar.drag.width,0));
}
progressbar.drag.addEventListener(MouseEvent.MOUSE_UP,dragMouseUp);
function dragMouseUp(evt:MouseEvent):void
{
dragClicked = false;
progressbar.drag.stopDrag();
var cnt:Number = (progressbar.drag.x/(progressbar.width-progressbar.drag.width))*MovieClip(loader.content).totalFrames;
MovieClip(loader.content).gotoAndPlay(cnt);
}
Pls solve my issue.
Thanks in advance.
To access a specific frame, you need to provide an integer value where at the moment you are using a floating-point value.
A simple fix would be to cast the Number to an int:
MovieClip(loader.content).gotoAndPlay(int(cnt));