PDA

View Full Version : objects putting functions on another objects


Incrue
08-27-2006, 09:04 PM
I have a class, i want it to put some methods and functions inside other objects, but its not working
It goes like this:

package{
public dynamic class asbr {

public function initialize(obj):void{
obj.listeners = new Array();
obj.addListe = function(objt:Object):void {
obj.removeListe(objt);
obj.listeners.push(objt);
}
obj.removeListe = function(objt:Object):void {
for(var i:int = 0; i < listeners.length; i++) {
if(listeners[i] == objt) {
listeners.splice(i, 1);
}//end if
}//end for
}//end removeListe
////
obj.broadcastMessage=function(ev:String):void {
for(var i:int = 0; i < listeners.length; i++) {
listeners[i][ev]();
}
}

}
}
}

In flash it goes:

var ac:asbr = new asbr();
var ec:Object = new Object;
ac.initialize(ec);

var subject = new Object();
ec.addListe(subject);
ec.removeListe(subject);

Yes, i am trying to do a asBroadcaster class in as3.Simple mind?Yes, i am , thank you very much, now how do i make this thing work?Thank very vitamins.

Incrue
08-28-2006, 12:25 AM
Now, what the heck is that; when i go


var ac:asbr = new asbr();
var ei:Object = new Object;
ac.initialize(ei);
trace(ei.listeners);


He didnt say anything about listeners being undefined, but this:

var ac:asbr = new asbr();
var ei:Object = new Object;
ac.initialize(ei);
trace(ei.listeners);
var uc = new Object();
ei.addListe(uc);

Outputs : Variable listeners is not defined.
I know this may sound stupid so noone wants to answer but im still learning

Incrue
08-28-2006, 10:43 AM
Thank you so much for the help
Anyway, in case someone cares, i think the problem is that in AS2 this works, but in AS3,it doesnt


var objA = new Object();
var objB = new Object();

objB.doThis = function(){
trace("objB doing this");
}

objA.obName = "objB";
objA.funcName = "doThis";
objA[obName][funcName]();

Fall_X
08-28-2006, 12:04 PM
Thank you so much for the help
Anyway, in case someone cares, i think the problem is that in AS2 this works, but in AS3,it doesnt


var objA = new Object();
var objB = new Object();

objB.doThis = function(){
trace("objB doing this");
}

objA.obName = "objB";
objA.funcName = "doThis";
objA[obName][funcName]();


What are you talking about? This wouldn't work in AS2 either. It wouldn't give you an error because AS2 isn't strict, but it wouldn't work either.

First : objA[obName] is invalid. There is no variable called obName. So you probably mean objA['obName'] or objA.obName
Secondly, objA.obName would return the STRING objB. That's a string, not a reference to objB. So you can't call any functions from it. But since funcName isn't a variable (it is a property of objA, but you're not using it as such), it wouldn't work anyway.

Incrue
08-28-2006, 01:31 PM
What are you talking about? This wouldn't work in AS2 either.


Secondly, objA.obName would return the STRING objB. That's a string, not a reference to objB. So you can't call any functions from it.

Cant call any functions or objects using strings as references?So paste this in f8, smart guy:

var objA = new Object();
var objB = new Object();

objB.doThis = function(){
trace("objB doing this");
}

var obName =new String("objB");
objA.funcName =new String("doThis");
_root[obName][objA.funcName]();


Besides, before start talking about things you dont know, it would be good if you try to learn a little, all Keys and all references in any objects are Strings.The only exeption are dicitonarys
http://www.kirupa.com/forum/showthread.php?p=1886034#post1886034

Fall_X
08-28-2006, 02:21 PM
Cant call any functions or objects using strings as references?So paste this in f8, smart guy:

Your code for F8 is different. Off course you can do what you want to do, but not the way you did it. Try reading what I said, it explained exactly what was wrong with it.

before start talking about things you dont know, it would be good if you try to learn a littleRight. Sure. Whatever. I have enough real life experience and have followed enough university degree courses about OO and programming in general, so I don't need someone to tell me that I don't know about anything it, especially not after I was trying to help.

all Keys and all references in any objects are Strings.The only exeption are dicitonarys
That has absolutely no relevance to your problem. Let me explain it once more :


objA[obName]

THIS RETURNS A STRING. Now, off course, if you would use it as a key on an object, it would make sense. But you didn't do that, you just took that string and tried to access a property called [funcName] on it. funcName is undefined in the current scope (objA.funcName on the other hand, is defined). Plus, if it was defined, the string you used it on doesn't have a function with that name (unless it contained one of the String class's methods).

Point is : your code was wrong. I told you what is wrong with it. I wasn't trying to be a smart guy, I was trying to HELP, after you became annoyed that nobody was helping.

Just check out your topic on flashkit and you'll see how it's done.

Incrue
08-28-2006, 02:31 PM
THIS RETURNS A STRING. Now, off course, if you would use it as a key on an object, it would make sense.[quote]
Of course, now that i tell you


But you didn't do that, you just took that string and tried to access a property called [funcName]
YOU ASSUME i was trying to do this, when what i was trying to do HAD NOTHING TO DO WITH THIS
Genious, I KNOW funcName is a String, I MADE it a String who will return "doThis" WHAT YOU DIDNT NOTICE, GENIOUS was that i want WHAT IS HAS RETURNED to acess the function doThis
And thats what i did
Thank you very much for absolutely nothing.You now is on my ignore List so...until never.

Fall_X
08-28-2006, 02:37 PM
Could someone point out to this guy that he is missing the point entirely?

Of course, now that i tell you
Like that wasn't obvious from the start :rolleyes:
I KNOW funcName is a String, I MADE it a StringThat's not what I was talking about. Read my posts carefully.
YOU ASSUME i was trying to do this, when what i was trying to do HAD NOTHING TO DO WITH THISI'm not assuming anything, I'm telling you what your code is doing. If what you were trying to do has nothing to do with that, then why were you coding it like that?
Thank you very much for absolutely nothing.You now is on my ignore List so...until never.
How childish.

Tink
08-29-2006, 12:42 PM
This post is now closed means you can't play nicely.