In ActionScript3, I am trying to access the properties of the caller object from a composite.
public class Robot {
...
private var controlPanel:ControlPanel;
...
public function Robot() {
...
cPanel = new ControlPanel();
...
}
}
My ControlPanel needs to access properties from Robot instance, but I don’t think I can pass this when calling the ControlPanel…
public class ControlPanel{
...
public function ControlPanel() {
//How can I refer back to robot properties ?
//
}
}
I believe I am in the case of composition as a Robot has a ControlPanel. I am thinking of using events, but there are many properties I need to access.
What would be the best way to solve this?
You can always just let
ControlPanelstore a reference to its ownRobotobject, like so:And then, when creating the control panel:
Alternatively, you could create an even system of sorts, and then let the control panel dispatch them. You could create your own
ControlPanelEventclass, and then let the Robot itself handle the results. For example, let’s say you change a property calledfooin the control panel. You could dispatch it like this:Then you’d receive it like this:
However, that’s wordy and unnecessary. You could also use ActionScript’s array access notation in the event handler, which would be a simple one-liner:
Still, that’s not entirely secure, since you might not want the control panel to be able to update all of a robot’s properties. Instead, you could maintain a simple map of configurable properties that the robot can have, and update that instead:
There you go. Of course, you could always just store the configuration map in the
ControlPanelitself, and let theRobotpull from that, but if you absolutely need it as a property of the robot, here are a few solutions.