Reputation: 814
I'm trying to make a small game, and this requires using several "loops". In my case, the user is first presented with two fields and a "Connect" button. Once he clicks the button, the fields and buttons are removed, and the game moves to another screen which is containing a single "disconnect" button, that'll take him to the first screen again. This is my naive implementation of it (I simplified it a lot) :
package Application {
import flash.display.Sprite;
import flash.events.Event;
import fl.controls.TextArea;
import fl.controls.Button;
import flash.events.MouseEvent;
public class Main extends Sprite {
var IPField:TextArea;
var PortField:TextArea;
var disconnectButton:Button;
var nickField:TextArea;
var passField:TextArea;
var loginButton:Button;
var network:NetworkHandler;
public function Main() {
IPField = new TextArea();
PortField = new TextArea();
disconnectButton = new Button();
// Snipped : positioning the Buttons and TextAreas
network = new NetworkHandler(this.stage, IPField, PortField);
network.addEventListener("connection_opened", onConnect);
network.addEventListener("connection_error", onError);
disconnectButton.addEventListener(MouseEvent.CLICK, network.doConnect);
addChild(IPField);
addChild(PortField);
addChild(disconnectButton);
}
private function onConnect(e:Event):void {
removeChild(IPField);
removeChild(PortField);
removeChild(disconnectButton);
disconnectButton.removeEventListener(MouseEvent.CLICK, network.doConnect);
network.removeEventListener("connection_opened", onConnect);
network.removeEventListener("connection_error", onError);
network.addEventListener("connection_closed", returnToConnect);
network.addEventListener("connection_error", returnToConnect);
disconnectButton.addEventListener(MouseEvent.CLICK, network.doDisconnect);
addChild(disconnectButton);
}
private function returnToConnect(e:Event):void {
loginButton.removeEventListener(MouseEvent.CLICK,network.doConnect);
removeChild(disconnectButton);
network.removeEventListener("connection_closed", returnToConnect);
network.removeEventListener("connection_error", returnToConnect);
disconnectButton.addEventListener(MouseEvent.CLICK, network.doConnect);
network.addEventListener("connection_opened", onConnect);
network.addEventListener("connection_error", onError);
addChild(IPField);
addChild(PortField);
addChild(disconnectButton);
}
private function onError(e:Event):void {
// Error management
}
}
}
For the record, NetworkManager is a class I designed, and that basically manages the connection, dispatching events whenever the connection is established/closed/dropped.
So, I have two questions :
Isn't the way I used to implement this recursive? Meaning, if someone where to press Connect, disconnect, connect, etc., would it eventually lead to a stack overflow? (Assuming that there are more buttons on the page, things taking a lot of memory, etc)
What is the best way to implement this? In the end, there will be several more "loops" in the game, so i'm not sure whether this is a good solution or not. What's commonly used by game devs?
Upvotes: 0
Views: 48
Reputation: 18747
No, this is not recursive, because you are using events and your listeners do not make calls to each other. And yes, this is normal model to use event listeners. You may even retain event listeners in place, because they will not trigger if the object is not on the stage. The only thing I'd change is grouping, that is, use container Sprite
objects to place a set of nested scenery in a single addChild()
with a restriction if necessary, so that you won't get two different scenes on your screen at once.
PS: Use TextField
instead of TextArea
, if you are planning them to receive one-line input from user. Set the text field type to input
and watch the show.
Upvotes: 1