Monday 1 December 2008

Bridge Part 2 - init() start() stop()

Here's the init() function, called by the browser or applet viewer to inform this applet that it has been loaded into the system.

public void init()
{
// Establish the path (URL) of the applet itself
codebasestr = getCodeBase().toString();
// Clear the area into which Save data will be copied
saveString = new String("");
// Clear the Save flag
saveFlag = false;
// Clear the item that indicates a Sprite is being held by the player
isHolding = null;
// Set up a class instance for the scene data
bridgedata = new BridgeData(this);
// Set up class instances for display stuff
dirtyrectset = new DirtyRectSet();
displaymanager = new DisplayManager(this);
// No music right now
songFlag = false;
// Set the initial scene
currscene = changeScene(bridgedata.startup); // returns scene name
thisscene = bridgedata.fetchCurrentScene(); // returns scene class instance
// Initialise the loop timer
timerRunning = false;
ticks=0;
// Set Mouse interrupt functions
addMouseMotionListener(this);
addMouseListener(this);
// No inventory entry now
invFlag = false;
}

The start() function, called by the browser or applet viewer to inform this applet that it should start its execution. start() and stop() may be called repeatedly during the applet's existence as the page it is on drops out of focus etc. All we do here is to start the timer if it is stopped, and to create a new timer if there isn't one already. This means we can halt operation and restart, effectively freezing the game time. I'm not sure we should call timer.start() unless we have just created it.


public void start()
{
if (!timerRunning)
{
ticks++;
timewas = timenow = System.currentTimeMillis();
timerRunning = true;
if (timer == null)
{
timer = new Ticktimer();
}
}
timer.start();
}

The stop() function, called by the browser or applet viewer to inform this applet that it should stop its execution. It is called when the Web page that contains this applet has been replaced by another page, and also just before the applet is to be destroyed. Apparently it isn't safe to stop() a timer, so we just flag it as suspended. I think we ought to check this flag in the tick() function or in the ticktimer, which we don't currently do, otherwise ticks will just keep going ad infinitum even if we wanted the timer to freeze.

public void stop()
{
timerRunning = false;
}

No comments: