PDA

View Full Version : Dynamic sprite name


optikaa
07-27-2009, 09:24 PM
I'm trying to understand how to get to grips with AS3 using this code from a tutorial, what I don't understand is how you affect or reference a particular sprite, the last line that I have added doesn't do anything.

var i:Number=0;
var n:Number=7;
while( i < n )
{
// Create the Sprite
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill( 0 );
sprite.graphics.drawRect( 0, 0, 50, 50 );
sprite.graphics.endFill();
sprite.x = Math.random() * 500;
sprite.y = Math.random() * 300;

sprite.buttonMode = true;
sprite.mouseChildren = false;

// Give the Sprite a name
sprite.name = "sprite_" + i; // "sprite_0" "sprite_1" ...

// Get a reference to an event listener
var type:String = "MouseDown";
var listener:Function = this[ "on" + type ]; // "onMouseDown"

// Add the listener to the sprite.
sprite.addEventListener( MouseEvent.MOUSE_DOWN, listener );

// Add the sprite to the display list.
addChild( sprite );

// Increase the loop counter.
i ++;
}

function onMouseDown( e:MouseEvent ):void
{
var sprite:Sprite = Sprite( e.target );
trace( sprite.name ); // "sprite_0" "sprite_1" .
sprite_1.width=400;
}

shawnblais
07-27-2009, 09:41 PM
I'm trying to understand how to get to grips with AS3 using this code from a tutorial, what I don't understand is how you affect or reference a particular sprite, the last line that I have added doesn't do anything.

var i:Number=0;
var n:Number=7;
while( i < n )
{
// Create the Sprite
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill( 0 );
sprite.graphics.drawRect( 0, 0, 50, 50 );
sprite.graphics.endFill();
sprite.x = Math.random() * 500;
sprite.y = Math.random() * 300;

sprite.buttonMode = true;
sprite.mouseChildren = false;

// Give the Sprite a name
sprite.name = "sprite_" + i; // "sprite_0" "sprite_1" ...

// Get a reference to an event listener
var type:String = "MouseDown";
var listener:Function = this[ "on" + type ]; // "onMouseDown"

// Add the listener to the sprite.
sprite.addEventListener( MouseEvent.MOUSE_DOWN, listener );

// Add the sprite to the display list.
addChild( sprite );

// Increase the loop counter.
i ++;
}

function onMouseDown( e:MouseEvent ):void
{
var sprite:Sprite = Sprite( e.target );
trace( sprite.name ); // "sprite_0" "sprite_1" .
sprite_1.width=400;
}

You can access the target directly... e.target.width = 400;

Usually it's common to store your instanciated objects in an array, so you can easily reference them later using an index number. Whe using classes this tends to be alot cleaner and easier than messing with the .name property.

Basically you're storing a direct reference to the object and can target it no matter where it exists in the display list, if you use .name then you're stuck using the getChildByName function, and you have to be targeting the proper displayObject for it to work.

UncleNinja
07-27-2009, 09:51 PM
Hello there.
Your while loop would be better as a for loop. It works ver similar to a for loop, so you might as well use one. :)
Change this:
while( i < n )
{
to this:
for(i=0; i<n; i++)
{
Also, remove this:
// Increase the loop counter.
i ++;the for loop does it for you! :)

One problem with your code is that you create a new sprite every time that function is fired. Sure, garbage collection may come later and clean it up, but it isn't too efficent. :)
You can change your trace to just:
trace(e.target.name);and not make that Sprite.
maybe this will help you. :)

EDIT:
You can access the target directly... e.target.width = 400;
Yes, if you are wanting to do that, it is a good idea.

optikaa
07-27-2009, 10:01 PM
You can access the target directly... e.target.width = 400;

Usually it's common to store your instanciated objects in an array, so you can easily reference them later using an index number. Whe using classes this tends to be alot cleaner and easier than messing with the .name property.

Basically you're storing a direct reference to the object and can target it no matter where it exists in the display list, if you use .name then you're stuck using the getChildByName function, and you have to be targeting the proper displayObject for it to work.

That does sound a lot better and I have read this somewhere else but can't really figure out how this should be written.

Is something like this right?


var sprite:Sprite = new Sprite();

myArray[0]=sprite;

myArray[0].width=400;

shawnblais
07-27-2009, 10:20 PM
That does sound a lot better and I have read this somewhere else but can't really figure out how this should be written.

Is something like this right?


var sprite:Sprite = new Sprite();

myArray[0]=sprite;

myArray[0].width=400;

..basically, but more like:

myArray.push(sprite);
or
myArray[i]=sprite;

And this works:
myArray[0].width=400;

but in your case you can just manipulate the target directly within the event handler.

optikaa
07-27-2009, 11:11 PM
..basically, but more like:

myArray.push(sprite);
or
myArray[i]=sprite;

And this works:
myArray[0].width=400;

but in your case you can just manipulate the target directly within the event handler.

That's great thanks, all coming together now.

Here is the script with both methods working

var i:Number=0;
var n:Number=7;
var squaresArray:Array = new Array();
while( i < n )
{
// Create the Sprite
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill( 0 );
sprite.graphics.drawRect( 0, 0, 50, 50 );
sprite.graphics.endFill();
sprite.x = Math.random() * 500;
sprite.y = Math.random() * 300;

sprite.buttonMode = true;
sprite.mouseChildren = false;

// Give the Sprite a name
sprite.name = "sprite_" + i; // "sprite_0" "sprite_1" ...



// Add the listener to the sprite.
sprite.addEventListener( MouseEvent.MOUSE_DOWN, Squash );

// Add the sprite to the display list.
addChild( sprite );

squaresArray.push(sprite);


// Increase the loop counter.
i ++;
}

function Squash( e:MouseEvent ):void
{

e.target.width=10;
squaresArray[0].height=10;
}