Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 10-24-2009, 01:57 PM   #1
Registered User
Join Date: Mar 2009
Location: Sweden
Posts: 29
Default Accessing methods of dynamically created objects?

Alright, so I'm creating objects dynamically, i.e.

ActionScript Code:
for(var i:int=0; i<5; i++) {                 var table:Table = new Table(Math.random()*stage.stageWidth,Math.random()*stage.stageHeight,Math.random()*100);                 addChild(table);                 table.addEventListener(MouseEvent.CLICK, onClick);             }

Now, the Table class has a method called getMemento. How would I go ahead accessing that method? e.target.getMemento() doesn't work And I can't come up with any solution...

ActionScript Code:
private function onClick(e:MouseEvent):void {             _nextMementos = new Array();             _previousMementos.push(e.target.getMemento());             (e.target as MovieClip).setXPosition(Math.random() * stage.stageWidth);             (e.target as MovieClip).setYPosition(Math.random() * stage.stageWidth);         }
jennikins is offline   Reply With Quote
Old 10-24-2009, 02:45 PM   #2
Holosuit User
wvxvw's Avatar
Join Date: Oct 2006
Location: Tel Aviv
Posts: 4,301
Send a message via ICQ to wvxvw

ActionScript Code:
Probably the target of the even isn't Table, it may be some button inside the table or something like that.
MovieClip has no method setXPosition, it has property x. Why do you try to
* call method that will rpobably not exist on the event.target
* not cast the target to the expected type?
The .NET open source editor for Flash and web developers
*This would be my contribution to the project*
couchsurfing if you need it
wvxvw is offline   Reply With Quote
Old 10-24-2009, 03:02 PM   #3
Registered User
Join Date: Mar 2009
Location: Sweden
Posts: 29

I realize that, but the only object type that the event listener is assigned to is Table, so I was looking for help with the casting. Table(e.target) didn't do the trick and (e.target as Table) didn't work any better. So I was looking for help with that.

setXPosition is a method within the Table class, and I'm trying to access it, implementing a undo/redo pattern (memento pattern).
jennikins is offline   Reply With Quote
Old 10-24-2009, 03:03 PM   #4
Super Moderator
ASWC's Avatar
Join Date: Dec 2007
Location: Greenville, SC
Posts: 6,528

ActionScript Code:
This might work but I agree you should try to simply strong type the currentTarget by casting to table.
currentTarget is the object you register the event with. target is the object receiving the input which as wvxvw pointed out might not be the same object.
Super Duper!
ASWC is offline   Reply With Quote
Old 10-24-2009, 03:10 PM   #5
Senior Member
Join Date: Feb 2009
Posts: 360

set table.mouseChidren = false, and then e.target will be your table.

currentTarget should also get the container object, but turning off mouseChildren is generally good practice.
shawnblais is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 08:32 AM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.