PDA

View Full Version : Reference X Or Y coordinate by using name property



gphilly77
October 17th, 2008, 02:04 PM
I have a function called addPoint that creates a new sprite instance onmousedown event:

function addPoint(e:MouseEvent):void
{
pointSpriteNameCounter += 1;
pointSprite = new Sprite();
pointSprite.name = "pointSprite" + pointSpriteNameCounter;

pointSprite.graphics.lineStyle(1,0x000000,1,true);
pointSprite.graphics.beginFill(0x99CC00,1);
pointSprite.graphics.drawCircle(0,0,10);
pointSprite.x = mouseX;
pointSprite.y = mouseY;
pointSprite.graphics.endFill();
addChild(pointSprite);

routeContainer.graphics.lineStyle(1, 0, 1);
routeContainer.graphics.moveTo(pointSprite.x, pointSprite.y);
routeContainer.graphics.lineTo(300, 300);
}

The idea is that each time user clicks a new circle is created on stage and for each circle created, a line is created to connect the previous circle to the last one created. I thought that I could do this like so:

routeContainer.graphics.lineStyle(1, 0, 1);
//reference coord of the first sprite created
routeContainer.graphics.moveTo(pointSprite.name.x, pointSprite.name.y);
//reference coord of the second sprite created
routeContainer.graphics.lineTo(pointSprite.name.x, pointSprite.name.y);

But compiler returns an error when I try to reference the X or Y coor by using
pointSprite.name.x
I'm sure there is a much better way to create sprites on mousedown and then connect each sprite to another as they are created.

I was thinking maybe storing each sprite name in an array and then loop through array to get the last and current sprite name and then connecting them that way...

Thanks for the help...

G

sonicdoomx
October 17th, 2008, 02:17 PM
var sprite_arr:Array = new Array();

function addPoint(e:MouseEvent){
var new_sprite:Sprite = new Sprite();
new_sprite.x = mouseX;
new_sprite.y = mouseY;
addChild(new_sprite);

if(sprite_arr.length > 0){
routeContainer.graphics.lineStyle(1, 0, 1);
routeContainer.graphics.moveTo(sprite_arr[sprite_arr.length - 1].x, sprite_arr[sprite_arr.length - 1].y);
routeContainer.graphics.lineTo(300, 300);
}

sprite_arr.push(new_sprite);
}


Using push() on an array inserts an element at the end. You can then use the length -1 to reference the last inserted.

gphilly77
October 17th, 2008, 03:41 PM
Works great, thx for the response.

I changed the last line so each sprite added is connected by lineTo...

routeContainer.graphics.lineTo(sprite_arr[sprite_arr.length-2].x, sprite_arr[sprite_arr.length-2].y);