Groups | Blog | Home
all groups > flash actionscript > august 2004 >

flash actionscript : Key.addListener(object:Object) Type casting ???


russMiner
8/22/2004 6:33:30 PM
I am aware of all of the code out there that says to make a key listener do
this:

var myListener:Object = new Object();
myListener.onKeyDown = function () {
trace ("You pressed a key.");
}
myListener.onKeyUp = function () {
trace ("You released a key.");
}
Key.addListener(myListener);



But what I am trying to do is have a class that already exists define the
click handlers, and then just have another executing class invoke the Key
object to add my pre-defined class as a listener. Example:


class com.smallfrystudios.za.PlayerController extends AbstractController{

public function PlayerController(cm:Observable){
super(cm);
}

public function playerKeyDown():Void{
trace(Key.getCode());
}
public function playerKeyUp():Void{
trace(Key.getCode());
}
}



Then in another class I try this:


//Assume that there was an instance of the controller passed in as a variable
"c"

//First I tried this:
Key.addListener(c);


//Then I tried this:
var caster = new Object(c);
Key.addListener(c);





Either way I always get a
Type mismatch. Key.addListener(c);

Now, all the documentation says that addListener accepts an Object. So what
gives?

Ideas?

russMiner
8/22/2004 6:42:36 PM
Hmm.. after some more messing.

In the controller class.. I am able to do:

Key.addListener(this);



And it responds correctly. But what if I still wanted to do the usage I described above?
AddThis Social Bookmark Button