Hello all,
Here is a specific problem I am facing most of the time; let me try to explain with an example.
Say I create a shape, and I want to move it with a ENTER_FRAME event, like this:
public function makeShape(){
var myShape=new Shape();
addChild(myShape);
myShape.addEventListener(Event.ENTER_FRAME,moveMyShape);
}
public function moveMyShape (event:Event) {
event.currentTarget.x+=1;
It is cool up to here.
But what if I want to pass a variable with my shape to the event function. Say my shape has a no or a name or anything like a data; so I rewrite the code;
public function makeShape(){
var myShape=new Shape();
myShape.thisShapesNo=50;
addChild(myShape);
myShape.addEventListener(Event.ENTER_FRAME,moveMyShape);
}
public function moveMyShape (event:Event) {
event.currentTarget.x+=1;
trace(event.currentTarget.thisShapesNo)
And now I get the error code,
"Cannot create property thisShapesNo on flash.shape… "
OK, I understand that, but it is not fair. Anything you create with a code can have a property you want to dub or add on it, and you should call it anytime you like.
I learned that there are 2 ways to get around it, but both of them are extra work for the code, and I don’t like to spend that precious memory for a cheap error like this.
First one is to add the shape as a child to a movieclip, I use this most of the time and goes like this:
public function makeShape(){
var myShape=new Shape();
var myClip=new MovieClip();
myClip.addChild(myShape);
myClip.thisShapesNo=50;
myClip.addEventListener(Event.ENTER_FRAME,moveMyShape);
}
public function moveMyShape (event:Event) {
event.currentTarget.x+=1;
trace(event.currentTarget.thisShapesNo)
See the extra workload… And the other way (and I’ve never tried) is to write your own event function where you can pass variables… I guess something like this:
myShape.addEventListener(Event.ENTER_FRAME, moveMyShape, 50 );
So it seems like this is another extra workload for the code.
And so here is my question: How can I add a you-name-it property to any thing I’ve created in AS3?
Regards.