PDA

View Full Version : ERROR #1009 -> Event.ADDED_TO_STAGE?



neonheart
February 25th, 2010, 10:30 AM
Hey guys,
I'm currently trying to load a gallery.swf into my mainmovie.swf. But when I try this I get this ERROR #1009:

TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.
at Handle()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at Gallery()
TypeError: Error #1009: Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.
at Gallery()


The gallery.swf consists of 3 classes:

The gallery.as:

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;

public class Gallery extends MovieClip
{
private var imagesToShow : Number = 4;
public var spaceBetween : Number = 20;
public var defaultWidth : Number = 195;
private var backend : String = 'backend.php';

private var xmlLoader:URLLoader = new URLLoader();
private var xdata : XML;
public var images : Array = new Array();
private var orgImgId : Number;
private var skipTo : Number = 0;
private var loaded : Boolean = false;

public var imagesClip : MovieClip = new MovieClip();
public var stageWidth : Number;

public function Gallery() : void
{
stageWidth = stage.stageWidth;
theHandle.addEventListener( "sliding", slide );

imagesClip.y = 180;
addChild( imagesClip );

xmlLoader.load( new URLRequest( backend + "?" + new Date().valueOf() ) );
xmlLoader.addEventListener( Event.COMPLETE, loadImages );

}

private function loadImages( _e : Event ) : void
{
xdata = new XML( _e.target.data );
var i : Number = 0;
for each( var img:XML in xdata.img )
{
images[i] = new Img( img, this );
images[i].x = 200 * i;
images[i].id = i;
i++;
}
goTo( 0 );
}

private function slide( _e : Event ) : void
{
var percent : Number = ( theHandle.goToX - slider.x ) / ( slider.width - theHandle.width );
var imgId : Number = Math.round(percent * ( images.length - 1 ));

goTo( imgId );
}

private function goTo( imgId : Number ) : void
{
var direction : Number;
if ( orgImgId != imgId )
{
if ( imgId > orgImgId ) direction = 1;
else direction = -1;

for ( var i : Number = - Math.floor(imagesToShow/2); i <= Math.floor(imagesToShow/2); i++ )
{
if( imgId + i < images.length && imgId + i >= 0 ) images[imgId + i].makeActive( i, direction );
}

for( var j : Number = 0; j < imagesClip.numChildren; j++ )
{
var tile : Img = imagesClip.getChildAt(j) as Img;
if ( tile.id < imgId - Math.floor(imagesToShow/2) || tile.id > imgId + Math.floor(imagesToShow/2) ) tile.deActive( direction );
}
countTxt.text = imgId + 1 + "/" + images.length;
}
orgImgId = imgId;
}
}
}

The handle.as:

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;

public class Handle extends MovieClip
{
public var goToX : Number = x;
private var slider : MovieClip = MovieClip(parent).slider;
private var mousePos : Number = 0;

public function Handle() : void
{
buttonMode = true;
addEventListener( MouseEvent.MOUSE_DOWN, moveHandle );
stage.addEventListener( MouseEvent.MOUSE_UP, stopHandle );
}

private function moveHandle( _e : MouseEvent ) : void
{
mousePos = mouseX;
stage.addEventListener( MouseEvent.MOUSE_MOVE, followHandle );
}

private function stopHandle( _e : MouseEvent ) : void
{
stage.removeEventListener( MouseEvent.MOUSE_MOVE, followHandle );
}

private function followHandle( _e : MouseEvent ) : void
{
var newPos : Number = stage.mouseX - mousePos;
var orgX : Number = x;
if ( newPos < slider.x )
goToX = slider.x;
else if ( newPos > (slider.x + slider.width) - width )
goToX = (slider.x + slider.width) - width;
else
goToX = newPos;
x = goToX;
if( goToX != orgX ) dispatchEvent( new Event( "sliding", true ) );
}

}

}

The img.as:

package
{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.text.TextField;
import flash.events.Event;
import flash.net.URLRequest;
import flash.events.*;
import fl.transitions.easing.*;
import flash.text.TextFormat;
import gs.TweenLite;
import fl.motion.easing.*;
import flash.text.TextFieldAutoSize;


public class Img extends MovieClip
{
public var active : Boolean = false;
public var id : Number;
private var src : String;
public var imageLoader:Loader = new Loader();
private var orgWidth:Number = 0;
private var orgHeight:Number = 0;
private var main : Gallery;
private var deactivating : Boolean = false;

public function Img( load : String, m : Gallery ) : void
{
orgWidth = width;
orgHeight = height;
main = m;
src = load;
alpha = 0;
}

public function loadImage() : void
{
if ( numChildren < 3 )
{
imageLoader.load( new URLRequest( "img/" + src ) );
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, displayImage );
}
}

private function displayImage( _e : Event ) : void
{
if ( main.images[id + 1] != null && !main.images[id + 1].parent ) main.images[id + 1].loadImage();
Bitmap( imageLoader.content ).smoothing = true;
imageLoader.x = main.spaceBetween/2 - ( orgWidth /2 );
imageLoader.y = main.spaceBetween/2 -( orgHeight /2 );
addChild( imageLoader );

var descr : TextField = new TextField();
descr.text = src;
descr.y = ( orgHeight /2 ) - 27;
descr.x = 10 - ( orgWidth /2 );
descr.autoSize = TextFieldAutoSize.LEFT;
descr.embedFonts = true;

var format : TextFormat = new TextFormat("cFont", 15, 0);
format.color = 0x5B4846;
descr.setTextFormat( format );

addChild( descr );
}

public function makeActive( position : Number, direction : Number ) : void
{
deactivating = false;
if ( parent == null )
{
x = ( direction == 1 ? main.stageWidth + main.defaultWidth * 2 : - main.defaultWidth * 2 );
alpha = 0;
main.imagesClip.addChild( this );
}
visible = true;
if ( numChildren < 3 ) loadImage();
parent.setChildIndex(this, ( parent.numChildren-1 ) - Math.abs( position ) );

var extra : Number = Math.round( position * ( main.defaultWidth + main.spaceBetween ) );
var newX : Number = ( Math.round( ( main.stageWidth / 2 ) /* - ( main.defaultWidth / 2 )*/ ) + extra );
flyTo( newX, false, (position == 0 ? 1.2 : 1 ) );
}

public function deActive( direction : Number ) : void
{
if ( ! deactivating )
{
active = false;
var moveTo : Number = ( direction != 1 ? main.stageWidth + main.defaultWidth * 2 : parent.x - main.defaultWidth * 2 );
flyTo( moveTo, true );
deactivating = true;
}
}

private function flyTo( newX : Number, removeAfter : Boolean, scale : Number = 1 ) : void
{
var tweeningOptions : Object = new Object;
tweeningOptions.x = newX;

if ( removeAfter )
{
tweeningOptions.ease = Linear.easeIn;
tweeningOptions.alpha = 0;
tweeningOptions.scaleX = tweeningOptions.scaleY = 0.3;
tweeningOptions.visible = false;
}
else
{
tweeningOptions.scaleX = tweeningOptions.scaleY = scale;
tweeningOptions.rotation = (Math.random() * 20) - 10;
tweeningOptions.ease = Back.easeOut;
tweeningOptions.alpha = 1;
}
TweenLite.to ( this, 0.4, tweeningOptions );
}

private function motionEnd () : void
{
visible = false;
}
}
}

Therefore I read through some threads and found Event.ADDED_TO_STAGE as a solution.. But I'm pretty new to this and don't realy know how to do this..
Could you help me out?

Thank you!

Breen
February 25th, 2010, 10:48 AM
Can you confirm, this gallery.swf works standalone?

neonheart
February 25th, 2010, 10:50 AM
Can you confirm, this gallery.swf works standalone?
Yes, it does.

Breen
February 25th, 2010, 11:14 AM
Hey NeonHeart,

Quick find, a tutorial that explains how to use the ADDED_TO_STAGE event, and gives you an example how to integrate it into a simple codebase:

http://www.flepstudio.org/forum/tutorials/775-event-added_to_stage.html

Give that a try first!

neonheart
February 25th, 2010, 11:53 AM
Hey NeonHeart,

Quick find, a tutorial that explains how to use the ADDED_TO_STAGE event, and gives you an example how to integrate it into a simple codebase:

http://www.flepstudio.org/forum/tutorials/775-event-added_to_stage.html

Give that a try first!
So, for example:

In the Error Code it says something about Handle().
Therefore I have to put this Event.ADDED_TO_STAGE in there, like:


package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;

public class Handle extends MovieClip
{
public var goToX : Number = x;
private var slider : MovieClip = MovieClip(parent).slider;
private var mousePos : Number = 0;

public function Handle() : void
{
addEventListener(Event.ADDED_TO_STAGE, added); //ADDED THIS ONE
buttonMode = true;
addEventListener( MouseEvent.MOUSE_DOWN, moveHandle );
stage.addEventListener( MouseEvent.MOUSE_UP, stopHandle );
}

private function added(e:Event)void{ // AND THIS
dispatchEvent(new Event("handleLoaded"));
}

private function moveHandle( _e : MouseEvent ) : void
{
mousePos = mouseX;
stage.addEventListener( MouseEvent.MOUSE_MOVE, followHandle );
}

private function stopHandle( _e : MouseEvent ) : void
{
stage.removeEventListener( MouseEvent.MOUSE_MOVE, followHandle );
}

private function followHandle( _e : MouseEvent ) : void
{
var newPos : Number = stage.mouseX - mousePos;
var orgX : Number = x;
if ( newPos < slider.x )
goToX = slider.x;
else if ( newPos > (slider.x + slider.width) - width )
goToX = (slider.x + slider.width) - width;
else
goToX = newPos;
x = goToX;
if( goToX != orgX ) dispatchEvent( new Event( "sliding", true ) );
}

}

}

Breen
February 25th, 2010, 12:03 PM
Almost. Maybe try it with a more simple piece of code,

Anywho, consider:

Currently, you are calling stage.addEventListener( MouseEvent.MOUSE_UP, stopHandle ); immediatly, after the object is created. However, because the object has not yet been added to a displaylist (this is required for Movieclips to show up on the stage), there is no stage to add the mouse_up listener to...

So what you want to do, is execute the code that is currently in the constructor, only when your class has been added to the stage (in the ADDED_TO_STAGE event handler, which in your current code is private function added(e:Event)void)

Does that make sense?

neonheart
February 25th, 2010, 12:48 PM
How about this?

Gallery.as

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.*;

public class Gallery extends MovieClip
{
private var imagesToShow : Number = 4;
public var spaceBetween : Number = 20;
public var defaultWidth : Number = 195;
private var backend : String = 'backend.php';

private var xmlLoader:URLLoader = new URLLoader();
private var xdata : XML;
public var images : Array = new Array();
private var orgImgId : Number;
private var skipTo : Number = 0;
private var loaded : Boolean = false;

public var imagesClip : MovieClip = new MovieClip();
public var stageWidth : Number;

public function Gallery() : void
{
addEventListener( Event.ADDED_TO_STAGE, init2 );

}

public function init2() : void
{
removeEventListener( Event.ADDED_TO_STAGE, init2 );
stageWidth = stage.stageWidth;
theHandle.addEventListener( "sliding", slide );

imagesClip.y = 180;
addChild( imagesClip );

xmlLoader.load( new URLRequest( backend + "?" + new Date().valueOf() ) );
xmlLoader.addEventListener( Event.COMPLETE, loadImages );
}

private function loadImages( _e : Event ) : void
{
xdata = new XML( _e.target.data );
var i : Number = 0;
for each( var img:XML in xdata.img )
{
images[i] = new Img( img, this );
images[i].x = 200 * i;
images[i].id = i;
i++;
}
goTo( 0 );
}

private function slide( _e : Event ) : void
{
var percent : Number = ( theHandle.goToX - slider.x ) / ( slider.width - theHandle.width );
var imgId : Number = Math.round(percent * ( images.length - 1 ));

goTo( imgId );
}

private function goTo( imgId : Number ) : void
{
var direction : Number;
if ( orgImgId != imgId )
{
if ( imgId > orgImgId ) direction = 1;
else direction = -1;

for ( var i : Number = - Math.floor(imagesToShow/2); i <= Math.floor(imagesToShow/2); i++ )
{
if( imgId + i < images.length && imgId + i >= 0 ) images[imgId + i].makeActive( i, direction );
}

for( var j : Number = 0; j < imagesClip.numChildren; j++ )
{
var tile : Img = imagesClip.getChildAt(j) as Img;
if ( tile.id < imgId - Math.floor(imagesToShow/2) || tile.id > imgId + Math.floor(imagesToShow/2) ) tile.deActive( direction );
}
countTxt.text = imgId + 1 + "/" + images.length;
}
orgImgId = imgId;
}
}
}

Handle.as

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;

public class Handle extends MovieClip
{
public var goToX : Number = x;
private var slider : MovieClip = MovieClip(parent).slider;
private var mousePos : Number = 0;

public function Handle() : void
{
addEventListener( Event.ADDED_TO_STAGE, init );

}

public function init() : void
{
removeEventListener( Event.ADDED_TO_STAGE, init );
buttonMode = true;
addEventListener( MouseEvent.MOUSE_DOWN, moveHandle );
stage.addEventListener( MouseEvent.MOUSE_UP, stopHandle );
}

private function moveHandle( _e : MouseEvent ) : void
{
mousePos = mouseX;
stage.addEventListener( MouseEvent.MOUSE_MOVE, followHandle );
}

private function stopHandle( _e : MouseEvent ) : void
{
stage.removeEventListener( MouseEvent.MOUSE_MOVE, followHandle );
}

private function followHandle( _e : MouseEvent ) : void
{
var newPos : Number = stage.mouseX - mousePos;
var orgX : Number = x;
if ( newPos < slider.x )
goToX = slider.x;
else if ( newPos > (slider.x + slider.width) - width )
goToX = (slider.x + slider.width) - width;
else
goToX = newPos;
x = goToX;
if( goToX != orgX ) dispatchEvent( new Event( "sliding", true ) );
}

}

}

neonheart
February 26th, 2010, 04:26 PM
Okay, it worked. Thank for the help.

But now there's another problem:

Wehen I try loading gallery.swf into a mc-container the slider won't work anymore. Therefore I tried loeding the .swf without a conatainer..
But for some reason I can't position it.

That's the code:

var my_Loader:Loader = new Loader();
my_Loader.x = 400;
my_Loader.y = 155;

//This creates a new instance of the URLRequest object that contains the
//path to the external SWF.
var my_url:URLRequest=new URLRequest("gallery.swf");

//These listeners detect when the file has finished loading, and if the
//correct file is loaded.

my_Loader.contentLoaderInfo.addEventListener(Event .COMPLETE, finishLoading);
my_Loader.contentLoaderInfo.addEventListener(IOErr orEvent.IO_ERROR, errorHandler);


//The load method then loads the SWF file into the loader object.
my_Loader.load(my_url);


//This function adds the external SWF to the stage.
function finishLoading(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}

//This function displays a message if the file is not found.
function errorHandler(errorEvent:Event):void {
trace("file missing");
}