AS3 Load swf As Movieclip - actionscript-3

I need to load in an external swf and be able to use it as a Movieclip in FlashDevelop i.e I need to be able to go to specific keyframes, start and stop it playing etc. Some simple working sample code would be hugely appreciated as I cannot find any satisfactory tutorials through Google.
EDIT
I now have this code
package
{
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.utils.getQualifiedClassName;
public class Main extends MovieClip
{
var animatedBox:MovieClip = new MovieClip();
var ldr:Loader = new Loader();
var frames:int = 0;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onload);
ldr.load(new URLRequest("../lib/test.swf"));
}
function onload(e:Event)
{
if ( !e.target )
return;
if( e.target.content is MovieClip )
{
animatedBox = e.target.content as MovieClip;
animatedBox.gotoAndPlay("Start");
}
else
{
trace( getQualifiedClassName( e.target.content ) );
}
}
}
}
After I try to run it I get [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference.
Any ideas?

import flash.utils.getQualifiedClassName;
var mc: MovieClip;
var ldr: Loader = new Loader();
ldr.contentLoaderInfo.addEventListener( Event.COMPLETE, onLoad );
ldr.load( new URLRequest("your.swf") );
function onLoad( e:Event ):void
{
if( !e.target )
return;
trace( getQualifiedClassName( e.target.content ) );
/* if you get: flash.display::AVM1Movie
it means you are trying to load an AS1 or AS2 SWF
into AS3 SWF. They both need to be AS3 */
mc = e.target.content as MovieClip;
mc.gotoAndPlay( 2 );
// or mc.gotoAndPlay( 'yourLabel' );
}

Related

ActionScript 3.0 import swf

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

Remove Child of Root actionscript3

I've created 4 instances of Notes and I have them moving to the right until their x value is greater than 100. Once they're there, how do I remove them? I ran a trace statement and confirmed that the parent of these instances is root (root1 to be exact). If I type
root.removeChild(this);
I get an error saying "call to a possibly undefined method removeChild"
If I type
removeChild(this);
I get an error saying "The supplied DisplayObject must be a child of the caller". Full code is posted below. The last line before the }'s at the end is the problem line. Thanks so much for the help!
package
{
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.utils.getDefinitionByName;
import flash.utils.Timer;
import flash.events.TimerEvent;
[Frame(factoryClass="Preloader")]
public class Main extends Sprite
{
private var speed:int = 8;
[Embed(source="../lib/Dodgethis.jpg")]
public var Notes:Class;
public var numnotes:Number;
public var timer:Timer = new Timer(500, 1)
public var rootContainer:DisplayObjectContainer = DisplayObjectContainer (root);
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
timer.start();
timer.addEventListener(TimerEvent.TIMER, testevent);
}
private function testevent(e:Event = null):void {
trace("testevent has run");
appear();
}
private function appear() {
var arr1:Array = new Array;
numnotes = 4;
for (var i = 0; i < numnotes; i++)
{
trace (i);
var nbm:Bitmap = new Notes;
stage.addChild(nbm);
nbm.y = i * 50;
arr1.push(nbm);
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
}
private function loop (e:Event):void {
this.x += speed;
trace(this.x) ;
if (this.x > 100) {
removeEventListener(Event.ENTER_FRAME, loop);
trace ("Event listener was removed");
//removeChild(this);
//rootContainer.removeChild (nbm);
/*trace(this.contains)
trace(this.name)
trace(this)*/
trace(this.parent.name); //root
removeChild(this);
}
}
}
}
Try using this in the loop function
e.target.parent.removeChild(e.target);
//or
stage.removeChild(e.target);
You're adding the notes to stage. So you need to remove them from stage.
stage.removeChild( note );
You can only remove a child from its parent, not from any other container. So calling removeChild on a different container will always fail

Dragging a movieclip as3

I have a Movieclip which is a child of another movieclip. I use startDrag() and stopDrag() with first (parent) Movieclip but the nested one doesnt move. Why?
relevant code on stage:
var main:rt = new rt(); // rt being a class in my library, which extends MovieClip object.
addChild(main);
stage.addEventListener(MouseEvent.MOUSE_DOWN, stage_mousedownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, stage_mouseupHandler);
function stage_mousedownHandler(event_object:Event) {
main.startDrag();
}
function stage_mouseupHandler(event_object:Event) {
main.stopDrag();
}
rt's constructor code:
public function rt() {
var bmp_bar:Bitmap;
var br_male:Bar_male; // Bar_male is a Bitmap in my library. (AS Linkage)
bmp_bar = new Bitmap(br_male);
this.addChild(bmp_bar);
}
Made this simple program to test and it works as expected in Flash Develop, there is a main sprite and a child bitmap when I mouse down any where on stage the main sprite is dragged and the child bitmap is moved.
So I am guessing there is something going on in your workflow in flash professional and linkage. Make sure the mouse event handlers are triggered put some breakpoints and debug.
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class Test extends Sprite
{
private var sp:Sprite = new Sprite();
public function Test()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
var bmpData:BitmapData = new BitmapData(100, 100,false,0x000000);
bmpData.fillRect(new Rectangle(0, 0, 100, 100), 0xff0000);
var bmp:Bitmap = new Bitmap(bmpData);
sp.addChild(bmp);
addChild(sp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseUp(e:MouseEvent):void
{
sp.stopDrag();
}
private function onMouseDown(e:MouseEvent):void
{
sp.startDrag();
}
}
}

AS3 MovieClip not playing consistently

So at the beginning when my SWF loads up it also loads up a sequence of animated clips like so:
var loader:Loader = new Loader();
loader.load(new URLRequest("clips/clip4.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, clip4Loaded);
And my clipLoaded function is:
private function clip4Loaded(e:Event):void {
clip4 = e.target.content as MovieClip;
}
The clip4 file being loaded in has a stop() at the first frame. Later in the game (clip4 is the "outro") I use:
clip4.gotoAndPlay(0);
clip4.addFrameScript(clip4.totalFrames - 1, clip4End);
However, the clip only seems to play about 25% of the time and all the other clips which I load the exact same way play fine. The only difference is that those clips get played fairly soon after they load which leads me to believe clip4 is being autoreleased at some point but I really have no clue.
strange - i've got a timeline-animated swf with stop(); in the first frame and the following code:
package {
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
/**
*
* #author www0z0k
*/
[SWF(width='400', height='300', frameRate='30')]
public class NewClass extends Sprite {
private const URL:String = 'res/1.swf';
private var spr:MovieClip;
public function NewClass():void {
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.INIT, onloaded);
ldr.load(new URLRequest(URL));
}
private function onloaded(e:Event):void {
spr = e.target.content as MovieClip;
addChild(spr);
spr.addFrameScript(spr.totalFrames - 1, function():void { x = x == 0 ? 100 : 0; } );
spr.addEventListener(MouseEvent.CLICK, onclick);
}
private function onclick(e:MouseEvent):void {
spr.gotoAndPlay(0);
}
}
}
and it works exactly as it's written.
could you please upload your clip4.swf anywhere (or test this code with it yourself)?

Access a function inside a loaded .swf file?

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.