i've little bit trouble with imported swf. Problem is that imported swf has movie clip which exported as movie clip class. Code is here:
Imported swf:
class myBooks(){
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
and HOLDER flash file:
var txt:String = "Project/book2.swf";
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest(txt);
myLoader.load(url);
addChild(myLoader);
when i try to run , it shows me error like this:
TypeError: Error #1009 cannot access a property or method of a null object reference at actions::myBooks()
Try the following
class myBooks()
{
public function myBooks():void
{
if(stage)
{
init(null);
}
else
{
addEventListener( Event.ADDED_TO_STAGE,init)
}
}
private function init( e:Event = null )
{
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
}
}
Hope that help
Related
Using a code snippet in as3 flash, but struggling for it to actually work:
I would like to unload the child SWF (which is a video) and go back into the parent SWF (a video selection page). Tried so many different ways and need a quick and easy solution. Thanks.
Below does not seem to work...
exit_btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
import fl.display.ProLoader;
var fl_ProLoader:ProLoader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;
function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
fl_ProLoader.unload();
removeChild(fl_ProLoader);
fl_ProLoader = null;
}
I use this code it's tested
import flash.filesystem.*;
import flash.events.MouseEvent;
import flash.net.*;
import flash.events.*;
var _file: File;
_file = File.documentsDirectory.resolvePath("./Directory to swf file/mySwf.swf");
if (_file.exists) {
trace("SWF loading ...........");
displayingSWF(_file.nativePath)
} else {
trace("the file doesn't exit");
}
//////////////// DIsplay the SWF story file //////////////////
var mLoader: Loader ;
function displayingSWF(lnk) {
var inFileStream: FileStream = new FileStream();
inFileStream.open(_file, FileMode.READ);
var swfBytes: ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
mLoader = new Loader();
var loaderContext: LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoadComplete);
mLoader.loadBytes(swfBytes, loaderContext);
}
function onSwfLoadComplete(e: Event): void {
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoadComplete);
addChild(mLoader);
}
exit_btn.addEventListener(MouseEvent.CLICK, dimising);
function dimising(e: MouseEvent): void {
mLoader.unloadAndStop();
removeChild(mLoader);
}
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;
I would like to load external child swf into the parent swf (under same directory).
I did try, the child loaded but the class in it didn't work.
1) How can i load the class of child swf?
2) How can i unload the class of child swf?(becoz there could be many ext. swf with different class)
thx
main.fla
function startLoad(){
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("game1.swf");
var mLoaderContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest, mLoaderContext);
}
function onCompleteHandler(loadEvent:Event){
var keyManager:Class = ApplicationDomain.currentDomain.getDefinition("net.keithhair.KeyManager") as Class;
addChild(loadEvent.currentTarget.content);
}
game1.fla
import net.keithhair.KeyManager;
keyManager=new KeyManager(stage);
keyManager.addKey(["a"], doSomething);
function doSomething():void {
//do something
}
Result:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at net.keithhair::KeyManager/removeListeners()
at net.keithhair::KeyManager/addListeners()
at net.keithhair::KeyManager()
at game1_fla::MainTimeline/frame1()
function Constructor(){
if (stage){
onAddedToStage();
} else {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
function onAddedToStage(evt:Event=null):void {
var keyManager:KeyManager;
trace("here!")
keyManager=new KeyManager(stage);
keyManager.addKey(["a"], doSomething
}
Constructor();
The above code solve all my problem.
thank you very much destinier
& senocular
Working on this Flash AS3 application and I am keep getting this error when I try to make an imgLoader clickable.
The imgLoader is a dynamic loader which will load an image from XML file and its created using ActionScript.
This is the full error I get:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at apptest_fla::MainTimeline/frame1()[apptest_fla.MainTimeline::frame1:65]
at runtime::ContentPlayer/loadInitialContent()
at runtime::ContentPlayer/playRawContent()
at runtime::ContentPlayer/playContent()
at runtime::AppRunner/run()
at ADLAppEntry/run()
at global/runtime::ADLEntry()
and this is the code for making the imgLoader clickable:
imgLoader.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
anyone knows why this is happening?
EDIT
This is my entire code:
stop();
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.filters.DropShadowFilter;
var xmlLoader11:URLLoader;
var xml11:XML;
var uRequest11 = new URLRequest("my.xml");
xmlLoader11 = new URLLoader(uRequest11);
xmlLoader11.addEventListener(Event.COMPLETE, onXMLLoad11);
var imgLoader11:Loader;
var nameLoader11:Loader;
var myString11:String = 'loading';
function onXMLLoad11(e:Event) {
xml11 = new XML(e.target.data);
imgLoader11 = new Loader();
imgLoader11.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded11);
imgLoader11.load(new URLRequest(xml11.Data.Image.text()[0]));
Nametxt11.text = "" + xml11.Data.Name.text()[0];
}
function onImgLoaded11(e:Event) {
addChild(imgLoader11);
imgLoader11.height = 300;
imgLoader11.width = 300;
var bitmapContent11:Bitmap = Bitmap( e.target.content );
bitmapContent11.smoothing = true;
addChild( bitmapContent11 );
bitmapContent11.height = 150;
bitmapContent11.width = 150;
bitmapContent11.y = 65;
bitmapContent11.x = 85;
}
imgLoader11.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
Does it break when this is called:
imgLoader.addEventListener(MouseEvent.CLICK, doSomething);
or when this is called:
nextFrame()
In the first case, imgLoader is null. In the second case, something you're trying to acess fields or methods of right after nextFrame() is called is null.
EDIT:
Try moving this:
imgLoader11.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
}
to the bottom of onXMLLoad11().
function onXMLLoad11(e:Event) {
xml11 = new XML(e.target.data);
imgLoader11 = new Loader();
imgLoader11.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoaded11);
imgLoader11.load(new URLRequest(xml11.Data.Image.text()[0]));
Nametxt11.text = "" + xml11.Data.Name.text()[0];
imgLoader11.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(event:MouseEvent){
nextFrame()
}
}
Is there a way to call a function inside a loaded SWF file?
Basically, I have a .swf file (A) that loads another .swf file (B)...I would just like to treat B as if it was any other instance added to my class .swf "A"...
Have to recast "Loader" with the name of your .swf file class:
Loaded .swf class:
package src {
import flash.display.MovieClip;
public class LoadedSWF extends MovieClip {
public function LoadedSWF() {
}
public function helloWorld():void
{
trace("hello world from loaded swf!");
}
}
}
Main class:
package src {
import flash.display.Loader;
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.events.Event;
public class Main extends MovieClip {
private var loader:Loader;
public function Main() {
loadSWF("LoadedSWF.swf")
}
private function loadSWF(url:String):void {
var urlRequest:URLRequest = new URLRequest(url);
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded, false, 0, true);
loader.load(urlRequest);
addChild(loader);
}
private function onLoaded(e:Event):void {
var target:LoadedSWF = e.currentTarget.loader.content as LoadedSWF;
trace(target);
target.helloWorld();
addChild(target);
}
}
}
There are two cases i.e
Child swf(B) calls function of Parent swf(A) or
Parent swf(A) calls function of loaded swf(B)
Firstly, in both cases, You must make sure that loaded swf(B) has been loaded and added to Loader swf(A) using Event.COMPLETE. Then communication between two swf is possible. The Loaded swf is just like any other child.
Here is the sample code for case 2
var mLoader:Loader = new Loader();
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.load(new URLRequest("B.swf"));
public function onCompleteHandler(evt:Event)
{
var embedSWF:MovieClip = MovieClip(evt.target.content);
addChild(embedSWF);
embedSWF.function_OF_B();
}
embedSWF.function_OF_B() statement will call the function of child swf B i.e function_OF_B()
In Adobe Flex, you can use the flash.display.Loader class to load another SWF, and add an event listener to it.
This example is taken from the Adobe Documentation:
var url:String = "http://www.helpexamples.com/flash/images/image2.jpg";
var urlRequest:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.load(urlRequest);
addChild(loader);
function loader_complete(evt:Event):void {
var target_mc:Loader = evt.currentTarget.loader as Loader;
target_mc.x = (stage.stageWidth - target_mc.width) / 2;
target_mc.y = (stage.stageHeight - target_mc.height) / 2;
}
Because contentLoaderInfo is a subclass of EventDispatcher, you can also dispatch events to the loaded SWF.
This is basically like calling a function from the SWF, just a little more complicated.