The Controller is being created in the constructor, and the registerKeys is being called there too. However, the event listeners are being added in the initialize function.
Thanks Dave. That was my workaround. Would event handlers make sense for controller? They do work for keyboard/mouse.
Btw, I might have ran into a bug. If I instantiate an array in the constructor the contents get lost:
//this.cameraList = new Array("Level-01", "TestBellHuey");
If I instantiate it during the initialize call it is persistent. Is that expected behavior? If so, is there a list of do’s and don’ts for the constructor? Or am I doing something wrong?
Nothing should be overwritten in the constructor unless you have declared this.cameraList as a script attribute. In which case, yes, it will be overwritten with contents from the editor.
The only “don’t” for the constructor is that the hierarchy isn’t present when the constructor is called. So you can’t access the hierarchy from context.root (e.g. use context.root.findByName) in the constructor. You have to wait until initialize() to access the hierarchy.
I’m not super happy with the controller at all it could do with some TLC. Events might make sense in the controller, but if you are just assigning multiple keys to an action. It might make more sense to use the “keydown” event and watch for the key codes in there.
Yes, I initially wanted to have a list of entity names accessible from the development window but learned arrays of strings, or arrays of entity’s wasn’t possible. So I just hardcoded the list and the rest is history. . .
After removing the attribute, the array instantiation in the constructor worked as expected.
Not accessing the hierarchy during the constructor makes sense. Thanks for pointing that out. I’m sure I’ll forget by tomorrow though
For the record, I’m just playing with reparenting the camera to different objects in the scene. I’m using wasPressed but getting numerous calls (frame, by frame). wasPressed and isPressed should be different, yes? If I press the key and let go, should that match a wasPressed?