as3 unload a class when clicking on loaded content - actionscript-3

I loaded a test swf into a class. The swf has its own code that loads a link when clicked. This works fine but I want to UNLOAD the class when the swf is clicked.
How can I unload a class when clicking on the loaded content?
I called the function "meow", a simply random name, as I am testing anyway. :)
Here is what I have that does not work:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.events.MouseEvent;
public class theAd extends MovieClip
{
public function theAd()
{
var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("http://www.massmediamail.com/testing/Manga.swf"));
my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startListener);
function startListener(e:Event):void
{
addChild(my_loader.content);
trace("Content Loaded");
my_loader.content.addEventListener(MouseEvent.CLICK, meow);
}
function meow(e:Event):void
{
//this.parent.parent.removeChild(this);
trace(this.name);
}
}
}
}

Are you having troubles with correct removal of loader.content? You should employ event.target property to get the correct instance.
function meow(e:MouseEvent):void {
var mc:DisplayObject=e.target;
if (!mc) return;
mc.parent.removeChild(mc);
}

Related

Event dispatcher

I have two classes and i tried event dispatch from one class to other class. Here i have used button to invoke the class. But dispatch not working.
class name cusDispatcher.as
package
{
import flash.display.MovieClip;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.MouseEvent;
import Globe;
public class CusDispatcher extends EventDispatcher
{
public function CusDispatcher():void
{
Globe.self.realstage.btn_mc.addEventListener(MouseEvent.CLICK,
doAction);
}
public function doAction(event:MouseEvent):void
{
dispatchEvent(new Event('myevent'));
}
}
}
class name EventDispatcherExample.as
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.EventDispatcher;
import Globe;
public class EventDispatcherExample extends EventDispatcher
{
public function EventDispatcherExample(Mc:MovieClip)
{
Globe.self.realstage = Mc;
var start:CusDispatcher = new CusDispatcher();
Globe.self.realstage.addEventListener('myevent', handler);
trace("one");
}
public function handler(event:Event):void
{
trace("My Event");
}
}
}
In the adobe CC i used this instance to run the class
var Call:EventDispatcherExample = new EventDispatcherExample(this);
Whenever i click the button(btn_mc) in the adobe flash cc it does not show any
trace of dispatch event.So could please analyze and give solution for this one.
Is your other click listener (in the EventDispatcherExample) working?
The problem might be that you have declared your CusDispatcher as a local variable within a function
var start:CusDispatcher = new CusDispatcher();
Once the function is complete these variables are cleaned up. Try to declare it at the beginning of your class as a class variable:
private var start:CusDispatcher;
public function EventDispatcherExample(Mc:MovieClip)...
and then assign your new Dispatcher to it
start = new CusDispatcher();

Swf is not loaded as centered when using TLF text and getting stage null in AIR2.6

When SWF loaded, it is not loads SWF centered with TLF text using & getting stage null. If classic normal text used then it is load centered.
please suggest how to load SWF as centered with TLF text in AIR 2.6. I have written a code as follows.
package {
import flash.display.MovieClip;
import flash.geom.Rectangle;
import flash.display.*;
import flash.events.MouseEvent;
import flash.events.Event;
public class FullScreen extends Sprite {
public function FullScreen() {
this.addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
btnClose.addEventListener(MouseEvent.CLICK, btnCloseFun);
}
private function _onAddedToStage(e:Event):void {
txt.text= "This is text";
var screenBounds:Rectangle = Screen.mainScreen.visibleBounds;
stage.nativeWindow.x = screenBounds.width/2 - stage.stageWidth/2;
stage.nativeWindow.y = screenBounds.height/2 - stage.stageHeight/2;
this.removeEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
}
function btnCloseFun(event:MouseEvent):void {
stage.nativeWindow.close();
}
}
}
Flash file properties is width=800, height = 600

Casting a loaded SWF as an interface

I'm trying to load in a remote SWF and access it's methods and properties, using an interface. (There's a similar question here that got as far as "that's weird!" but didn't resolve it: Loading swf and using it through interface)
My remote SWF looks like this:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.system.Security;
import IMultiplayer;
[SWF(width="238", height="60", frameRate="60", backgroundColor="#FFFFFF")]
public class Main extends Sprite implements IMultiplayer
{
public function init(e:Event):void
{
}
public function TEST():void
{
trace("TEST()");
}
}
}
I then have an interface that looks like this:
package
{
import flash.events.*;
import flash.display.*;
public interface IMultiplayer
{
function init(e:Event):void;
function TEST():void;
}
}
And finally, I've got a loader class that pulls down the SWF and tries to cast it as the same interface that the remote SWF implements. EDIT - apologies for length, was asked to post the full source:
package uk.co.MyDomain
{
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.TimerEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.system.SecurityDomain;
import flash.utils.Timer;
import uk.co.MyDomain.*;
import utils.Console;
public class MultiplayerLoader extends Sprite
{
private var ld:Loader;
private var _environment:String;
public var _mpInstance:IMultiplayer;
private const SANDBOX_SWF:String = "http://static.sandbox.dev.MyDomain.co.uk/smartfoxtest/dev/swf/MP.swf";
public function MultiplayerLoader(environment:String)
{
_environment = environment;
}
public function loadMultiplayer():void
{
ld = new Loader();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
ld.contentLoaderInfo.addEventListener(Event.COMPLETE, multiplayerLoaded);
ld.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onLoadError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOLoadError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.DISK_ERROR, onDiskError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onNetworkError);
ld.contentLoaderInfo.addEventListener(IOErrorEvent.VERIFY_ERROR, onVerifyError);
if(Security.sandboxType == Security.REMOTE)
{
trace('[MP Loader] Loading with context');
ld.load(new URLRequest(SANDBOX_SWF), context);
}
else
{
trace('[MP Loader] Loading with NO context');
ld.load(new URLRequest(SANDBOX_SWF));
}
}
private function onIOLoadError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onDiskError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onNetworkError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onVerifyError(e:IOErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function onLoadError(e:ErrorEvent):void
{
notifyFailure('IOLoadError');
}
private function multiplayerLoaded(e:Event):void
{
var tester:IMultiplayer = e.currentTarget.content as IMultiplayer;
Console.log('Loaded: ' + tester);
dispatchEvent(new MultiplayerEvent(MultiplayerEvent.SWF_LOAD_SUCCESS));
}
private function notifyFailure(reason:String):void
{
var failEvent:MultiplayerEvent = new MultiplayerEvent(MultiplayerEvent.SWF_LOAD_FAILURE);
failEvent.params = {'reason':reason}
dispatchEvent(failEvent);
}
}
}
Now, if I DON'T cast it to use the interface, I can trace it out successfully and call functions (so e.target.content.TEST() will fire). However, as soon as I cast it to the interface, it fails.
Any ideas?
EDIT
OK, I'm getting the same issue with a custom event class that's shared between both applications. Flash errors, saying it cannot convert from one class to the other - even though they're identical they're in different projects, and so I imagine different namespaces. I assumed that loading into the same applicationDomain would fix this, but it hasn't. Is there any other way I can get around this without resorting to a common library/SWC or similar?
I think this might be an issue of the application domain. The loaded SWF resides in its own application domain so it does not share the exact same interface. Try to load the swf into the »current application domain«, using the applicationDomainproperty of the Loader's LoaderInfo Object.
look here
good luck…
EDIT::
it has to be done in the loaders load method
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
var loader:Loader = new Loader();
loader.load(new URLRequest("myButtons.swf"), context);
from here
EDIT 2::
Once I ran into an even more strange error, it was caused by the fact that i created an interface, compiled the »to load files« and »file that loaded«, changed the interface through adding a method and forgot to compile the »to load files« (there were a lot). Perhaps happened something like this…
EDIT 3::
If I remember right than one has to wait for the Event.INIT event of loaded swf's, first after this event the constructor of the loaded swf ran.
EDIT 4::
found this, perhaps you need to do:
var YourInterfaceClass:Class = ApplicationDomain.currentDomain.getDefinition( "YourInterface" ) as Class;
var myInstance:YourINterface = event.target.content as YourInterface;

ActionScript play audio

I am just trying to make a simple .swf file that plays a piece of audio when it loads. This compiles but when I bring it up into the browser nothing happens. I could only find sprite based tutorials so I took a stab that you can extend Sound the same way as you would extend Sprite. The final version is going to be headless and called my Java Script to play audio on Events.
package {
import flash.media.Sound;
import flash.net.URLRequest;
public class typeRight extends Sound {
public function HelloWorld( ) {
load(new URLRequest('./sound.mp3'));
play();
}
}
}
I am NOT working in Flash so please no GUI advice ; )
Rather than subclassing the Sound class, create a document class like this that contains a Sound class in it:
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class SoundPlayer extends Sprite
{
protected var _sound : Sound;
protected var _channel : SoundChannel;
public function SoundPlayer()
{
_sound = new Sound();
_sound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
_sound.addEventListener(IOErrorEvent.IO_ERROR, loadError);
_sound.load(new URLRequest("./sound.mp3"));
}
protected function soundLoadCompleteHandler(evt : Event) : void
{
// Use the _channel object to control sound properties such as pan and volume.
_channel = _sound.play();
}
protected function loadError(evt : IOErrorEvent) : void
{
trace ("ERROR :: " + evt);
// You could try recovering from the error here.
}
}
}

Loading an swf into another swf accessing functions in the child swf

I have an an class a fla called draw.fla . The document class associated with draw is Draw.as The code inside Draw.as is as follows
package
{
import flash.display.MovieClip;
import flash.display.Graphics;
public class Draw extends MovieClip
{
public function drawCircle1(color:Number):void
{
graphics.beginFill(color);
graphics.drawCircle(100,100,40);
graphics.endFill();
}
}
}
The resulting swf is called draw.swf.
I have another fla called test.fla.
The following is the code on the first frame on the main time line.
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;
var swfLoader = new Loader();
var color:Number;
color = Math.round(Math.random()*0xFFFFFF);
var urlReq:URLRequest = new URLRequest("draw.swf");
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, swfLoadError);
swfLoader.load(urlReq);
function swfLoadComplete(evt:Event):void
{
var loader:Loader = Loader(evt.target.loader);
addChild(loader.content);
swfLoader.removeEventListener(Event.COMPLETE, swfLoadComplete);
}
function swfLoadError(evt:IOErrorEvent):void
{
trace("Unable to load swf ");
swfLoader.removeEventListener(IOErrorEvent.IO_ERROR, swfLoadError);
}
I have loaded dwaw.swf into test.swf as you can see. Is there anyway I can access the drawCircle1 function of Draw from the test.fla. Actually I need to pass the value color as an argument while accessing this function.
If I'm not wrong, once the loader is complete you could simply make MovieClip(loader.content).drawCircle1(0xFF0000);.
Loader.content should be that class, and function can be called by name with square bracket syntax:
function swfLoadComplete(evt:Event):void
{
var loader:Loader = Loader(evt.target.loader);
addChild(loader.content);
//trying to call function drawCircle1
loader.content["drawCircle1"](0xFF0000);
swfLoader.removeEventListener(Event.COMPLETE, swfLoadComplete);
}