views:

16

answers:

1

I created a canvas in a function, and in his function I have a CLICK eventlistener. On the click I want to manipulate what's inside the canvas.

Will referencing the canvas using the Dictionary class work?

A: 

You don't need a dictionary. Assuming that you added click listener using something like:

canvas.addEventListener(MouseEvent.CLICK, clickHandler);

you can access the canvas inside the clickHandler method using the event.currentTarget property.

private function clickHandler(event:MouseEvent):void
{
  //currentTarget is typed as object - cast it to canvas
  var canvas:Canvas = Canvas(event.currentTarget);
  //now do whatever you want with canvas
  canvas.setStyle("backgroundColor", 0xffff00);
}
Amarghosh
what if the eventlistener is a button inside the canvas? how would i access the children of the canvas? Would i need to use the dictionary in this case?
Adam
@Adam you mean a button that is a child of canvas? You can get button from the `event.currentTarget` and then canvas from `button.parent` - but I'd rather declare `canvas` as an instance variable of the class rather than use a dictionary for this. That's what instance variables are meant for.
Amarghosh