PDA

View Full Version : Inheritance and Function Identifiers



Dan0
August 7th, 2007, 08:39 AM
Hi again :to:,
I know I ask too many questions but its fun typing all of this to people I don't know. Ok so here goes;
I have all my button instances created from 1, generic class. I can easily get the class to register a MOUSE_DOWN event listener when clicked. However, I want the container of the buttons to register/process the clicks and Im not sure how to do this;
- either parse a link to each button of the function I want them to trigger and parse a unique identifier. But I do not know how to tell the button which function to trigger eg.


//Container Class
var myButton = new ButtonCreate("text",width,<function identifier>, <unique ID>);
// ButtonCreate Class
function onClickMe(e:Event){
<function identifier>(<unique ID>);
}

- OR - I use Inheritance so each different group of buttons inherit a different function when clicked, but I do not know where to start with this, let alone how to it efficiently.

Thanks for the awsem help, :moustache
Dan

senocular
August 7th, 2007, 08:43 AM
MOUSE_DOWN events propagate to parent instances. So if your buttons are clicked, their parent container will also receive the MOUSE_DOWN event and the event.target of that event will reference the button instance clicked.

Dan0
August 7th, 2007, 05:50 PM
Thanks I guess that has cleared things up a bit. However, I have a text field inside my button. Using this;

if (e.target is ButtonCreate)it works if I click in the graphic area of my ButtonCreate or in the shape instance inside it. But when I click on the text it registers the e.target type as a TextField. This is very annoying. Is there anyway to check if e.target is anything WITHIN a ButtonCreate instance?
*sigh* noobs are no good at anything nowa' days

Thanks for the help,
Dan

-edit-
further more, if it is returning the textfield as the even target then trying to call the id variable that I created on the ButtonCreate instances will not work, which is obviously needed to recognise which damn button was clicked.
-edit 2-
My solution;

if (e.target is ButtonCreate || e.target.parent is ButtonCreate)
:D thanks for the help forum peeps!

Dan0
August 7th, 2007, 06:16 PM
Oh yeh, is there somthing like a "is child of" function you can use? Because it would be nice for a function to return;

true - if it is inside a certain instance display wise / if it is the instance
false - if it is not inside the instance / it is not the instance

I would have guessed that it exists but I don't know if it does...

senocular
August 7th, 2007, 06:57 PM
see:
http://www.kirupa.com/forum/showthread.php?p=1948060#post1948060

Dan0
August 8th, 2007, 07:22 AM
Thanks sen, here is my final code;


//buttonClick on MOUSE_DOWN
// buttonsA, an array with identifiers to all buttons
function buttonClick(e:Event) {
var a=null;
for(var j=0;j<buttonsA.length;j++){
(buttonsA[j].contains(e.target))?a=buttonsA[j]:null;
}
if (a!=null) {
for (var i =0; i<buttonsA.length; i++) {
if (buttonsA[i]==a) {
buttonsA[i].selectColor(0xFF3300);
} else {
buttonsA[i].selectColor(0);
}
}
}
}I don't know if anyone can suggest any improvements?
Thanks for all the help sen & co,
Dan:toad: