Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

« Return

ActionScript 3

Registration point

When you click on an object where a listener is listening for that click, one piece of information sent in that click event is "currentTarget", which tells the code which object was clicked. So in this example, clicking on one of the two large squares results in the called function adding the small square to the display list of the currentTarget, the square that you clicked. Using addChild to add a shape to a new display list automatically removes the shape from whatever display list it had been in -- you don't have to first use removeChild in this example.

Download the FLA file

var a1:Sprite = new Sprite();
var a2:Sprite = new Sprite();
var a3:Sprite = new Sprite();;,0,100,100);
a1.x = 200;
a1.y = 70;
a1.buttonMode = true;
a2.x = 320;
a2.y = 70;
a2.buttonMode = true;
addChild(a2);;,45,10,10); // offset is 45,45 relative to parent, not stage

a1.addEventListener(MouseEvent.CLICK, changeSpriteParent);
a2.addEventListener(MouseEvent.CLICK, changeSpriteParent);
function changeSpriteParent(myEvent:MouseEvent)
	myEvent.currentTarget.addChild(a3); // currentTarget is whatever was clicked	

myButton1_btn.addEventListener(MouseEvent.CLICK, moveSprite1);
function moveSprite1(myEvent:MouseEvent)
	a1.x += 10;

myButton2_btn.addEventListener(MouseEvent.CLICK, moveSprite2);
function moveSprite2(myEvent:MouseEvent)
	a2.x += 10;

myButton3_btn.addEventListener(MouseEvent.CLICK, moveSprite3);
function moveSprite3(myEvent:MouseEvent)
	a3.x += 10;

« Return

©2012 Kroll Design    781.910.3694
Last modified: 12/31/1969 7:00 PM