Target variable found in .actionscript file from FLA? - actionscript-3

I am trying to stop the sound channel below from playing when a movieclip is visible in a flash file, but the variable is located in the .as file which loads in the FLA. How can I target that variable? Right now I am getting "undefiend" for the properties channel and song.
fla:
if(myMC == visible){
channel.stop();
}
else(channel = song.play());
.as file:
public var channel:SoundChannel;
public function Main(param:MovieClip) {
function showXML(e:Event):void {
websiteXML = new XML(e.target.data);
MovieClip(root).gotoAndPlay(3);
//trace(websiteXML);
popupFunction();
var urlStr:String = websiteXML.settings.mp3Url.#srcUrl;
var isStreaming:Boolean = websiteXML.settings.mp3Url.#streaming;
var song:Sound = new Sound();
song.load(new URLRequest(urlStr));
setTimeout(function():void {
channel = song.play(0, 999);
channel.soundTransform = new SoundTransform(1);
},1000);
}
}

Related

I am looking for a simple solution to loading a new/different mp3 sound file upon clicking a thumbnail using my existing code below

This seems simple enough, I just can't crack it though.
currently, in the below code. its a UILoader with a series of thumbnail mc's horizontally across the bottom of the FLA. when the thumbs are clicked and new SWF loads in the UI.
I am simply looking for the error of my ways, hopefully by simply added to this code and a possible explanation. I need to figure out how to play/stop on exit and new mp3 sound file.
UILoader
With movieClips used for thumbnail loads
var imagesXML:XML;
var xmlLoader: URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("lessons/images/Images5.xml"));
/////////////////////////////////////////////////////////////
//////////// thumbnail loader /////////////////////////
function xmlLoaded(evt:Event):void
{
imagesXML = new XML(xmlloader.data);
var thumbLoader:UILoader;
for(var i:uint = 0; i < imagesXML.image.length(); i++)
{
thumbLoader UILoader(getChildByName("thumb" + 1));
thumbLoader.load(new URLRequest("lessons/images/thumbs/" + imagesXML.image[i].#file));
thumbLoader.buttonmode = true;
thumbLoader.addEventListener(MouseEvent.CLICK, thumbClicked);
var fullPath:String = "lessons/images/file-1.swf";
mainLoader.load(new URLRequest(fullpath));
}
}
/////////////////////////////////////////////////////////////
//////////////// load Images /////////////////////////////
function thumbClicked(evt:MouseEvent):void
{
var thumbName:String = evt.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "lessons/images/" + imagesXML. image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
}
you can try to use this library https://github.com/treefortress/SoundAS
or try to google "Sound manager for as3" and I sure you will find library you like
Declare sound object outside of functions so it is available to all functions
var mySound:Sound;
var myChannel:SoundChannel;
function thumbClicked(evt:MouseEvent):void
{
var thumbName:String = evt.currentTarget.name;
var thumbIndex:uint = uint(thumbName.substr(5));
var fullPath:String = "lessons/images/" + imagesXML. image[thumbIndex].#file;
mainLoader.load(new URLRequest(fullPath));
//stop any already playing sound
if ( myChannel != null) { myChannel.stop(); }
//load an MP3 file
mySound = new Sound(); mychannel = new SoundChannel();
mySound.load(new URLRequest("myAudio.mp3"));
mySound.play();
}

Unloading an swf file and loading another swf file in as3

I want to unload a previously loaded swf file(welcome.swf) and load another swf file(topics.swf) when a user clicks a button that is located inside the first loaded swf file(i.e the button, is nested inside the welcome swf file that is to be unloaded).I have tried all manner of codes but none seems to be working and Im at my wits end.Kindly someone assist.Thanks in advance.Below is the code in my main file and the external swf that is to be unloaded.
Main file
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
box.addChild(loader);
External swf file
var loader = new Loader();`
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loader.unloadAndStop();
loader=null;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
loader.x = 0;
loader.y = 0;
addChild(loader);
}
I think you should load topics.swf at Main Loader.
So you should access Main Loader from welcome.swf.
Doesn't this code work?
External swf file
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
var target: Object = this;
// Search the Main Loader from child swf.
while (target.parent){
target = target.parent;
if (target is Loader){
var loader:Loader = target as Loader;
var SWFRequest:URLRequest = new URLRequest("Topics.swf");
loader.load(SWFRequest);
break;
}
}
}
I would probably move all the loading/unloading responsibility in a main file, with inner swf just letting the main know about some events inside through loaderInfo.sharedEvents just like this:
main file:
var box:Sprite = new Sprite;
box.x = 0;
box.y = 0;
addChild(box);
var loader = new Loader();
var swfURLReq:URLRequest = new URLRequest("welcome.swf");
loader.load(swfURLReq);
function topicsRequestedHandler(event:Event):void {
loader.contentLoaderInfo.sharedEvents.removeEventListener(
"loadTopics", topicsRequestedHandler);
loader.unloadAndStop();
//load "Topics.swf" here
}
loader.contentLoaderInfo.sharedEvents.addEventListener(
"loadTopics", topicsRequestedHandler);
box.addChild(loader);
and in external file:
button_1.addEventListener(MouseEvent.CLICK, button4);
function button4(event:MouseEvent) {
loaderInfo.sharedEvents.dispatchEvent(new Event("loadTopics"));
}
(not tested but should work)
So using this scheme for all the loaded files, you will keep loading/unloading management in one place.

loadermax to get qualified class name of the swf

The typical as3 code would be like this
private function load():void {
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
if(Security.sandboxType == Security.REMOTE) {
loaderContext.securityDomain = SecurityDomain.currentDomain;
}
loader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
loader.load(new URLRequest("capture.swf"), loaderContext);
}
private function handleInit(event:Event){
var className:String = getQualifiedClassName(loader.content);
var classRef:Class = loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
var captureModule = new classRef();
addChild(captureModule as DisplayObject);
}
now while using greensock's loadermax, how can I access the qualified class name, it's reference, create an object myself and add to display.
loaderMax.append(new SWFLoader("capture.swf", {name:"capture"}));
loaderMax.append(new SWFLoader("filter.swf", {name:"filter"}));
loaderMax.load();
the loadComplete function
function completeHandler(event:LoaderEvent): void {
trace(event.target + " is complete");
var capture = loaderMax.getContent("capture");
trace(getQualifiedClassName(capture)); //want to reach the custom class of the loaded sf
}
turns out that the loaded swf has a property loaderInfo.loader that you can access to use the loader.
var loader = loaderMax.getContent("capture").rawContent.loaderInfo.loader;
this.data["capture"] = loader.contentLoaderInfo.applicationDomain.getDefinition(getQualifiedClassName(loaderMax.getContent("capture").rawContent)) as Class;

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

Actionscript - load and play another sound file

i am playing a sound file and i want onclick to start playing another file.
You can check the funcyion PlayAnother() in the following example:
private var TheSound:Sound = new Sound();
private var mySoundChannel:SoundChannel = new SoundChannel();
private function PlaySound(e:MouseEvent):void
{
TheSound.load(new URLRequest("../lib/File1.MP3"));
mySoundChannel = TheSound.play();
}
private function PlayAnother(e:MouseEvent):void
{
mySoundChannel.stop();
TheSound.load(new URLRequest("../lib/File2.MP3"));
}
public function Test1():void
{
var Viewer:Shape = new Shape();
Viewer.graphics.lineStyle(0, 0x000000);
Viewer.graphics.beginFill(0x000000);
Viewer.graphics.drawRect(0, 0, 1, 10);
Viewer.graphics.endFill();
Viewer.width = 30;
Viewer.x = 10;
var Viewer1:Shape = new Shape();
Viewer1.graphics.lineStyle(0, 0x000000);
Viewer1.graphics.beginFill(0x000000);
Viewer1.graphics.drawRect(0, 0, 1, 10);
Viewer1.graphics.endFill();
Viewer1.width = 30;
Viewer1.x = 50;
var tileSpot:Sprite = new Sprite();
var tileSpot1:Sprite = new Sprite();
tileSpot.addChild(Viewer)
tileSpot1.addChild(Viewer1)
addChild(tileSpot);
addChild(tileSpot1);
tileSpot.addEventListener(MouseEvent.CLICK, PlaySound);
tileSpot1.addEventListener(MouseEvent.CLICK, PlayAnother);
}
but i'm getting the error (Functions called in incorrect sequence, or earlier call was unsuccessful).
can any one help please.
Flash is complaining because you're loading a new file into a Sound object which already has data. (If you look at the docs for Sound.load() here, it says "Once load() is called on a Sound object, you can't later load a different sound file into that Sound object").
You just need to instantiate a new Sound before loading File2 and run play() again:
private function PlayAnother(e:MouseEvent):void
{
mySoundChannel.stop();
TheSound = new Sound();
TheSound.load(new URLRequest("../lib/File2.MP3"));
mySoundChannel = TheSound.play();
}