2D Framework updates (GameTimer & Auto-Repeat - Example Vid from Android Game)

These are actually pretty nice (and very useful) updates.

  1. AnimLayer now has a pause & resume method, allowing you to only pause sections of your game associated with the AnimLayer’s update loop.

  2. AnimManager can now manage GameTimer(s) for you.

  3. GameTimer is now abstract and calls onComplete(float time); when the timer finishes.

  4. GameTimers can now be set to auto-repeat.

Example usage:
[java]
GameTimer attack = new GameTimer(interval) {
@Override
public void onComplete(float time) {
// Executed each time the timer completes
if (dirIndex == 9) dir = false;
else if (dirIndex == 0) dir = true;
weapon.getEmitter().setFixedDirection(dirs[dirIndex]);
weapon.getEmitter().emitNumParticles(1);
if (dir) dirIndex++;
else dirIndex–;
}
};
attack.setAutoRestart(true);

// Somewhere in you code… add the timer
screen.getAnimManager().addGameTimer(attack);

// Somewhere else in your code… remove the cycling timer
screen.getAnimManager().removeGameTimer(attack);
[/java]

EDIT: Also note that a timer that is not set to auto-repeat will be executed once and then removed/unflagged as managed.

EDIT 2: I guess it is also worth mentioning that GameTimer can be used for anything… they don’t have to be associated with the library components. They are a great way of keeping your update loop from looking like a CENSORING THE F BOMB… SORRY bowl of spaghetti.

That’s all for now.

1 Like

oops… ignore this :wink:

Another small update for GameTimer that allows you to set up a series of commands at different intervals with the use of setNextDuration calls from onComplete.

Scenario:
You have a series of commands/animations that you need to run at different intervals. For instance, you want a ship to release smaller attacker ships. The port needs to:

  1. Open (duration .5f)
  2. Release the series of attacker ships (duration 5f)
  3. Close (duration .5f)
  4. Reset for next use (turn off auto-repeat to flush the timer from the queue)

All this can be easily accomplished with a single timer now:

[java]
GameTimer releaseTimer;
float animDuration = .5f;
float openDuration = 5f;

// Methods to perform said actions
private void openPort(boolean open) {
// Open the port, run associated animations, etc
if (open) {
// open the port, etc
} else {
// close the port, etc
}
}

private void releaseAttackShips() {
// Release the ships, stay open and vulnerable for a period of time
}

// Defining the timer to execute the series of commands and then flush from the queue once complete
public void initTimer() {
releaseTimer = new GameTimer() {
@Override
public void onComplete(float time) {
if (getRunCount() == 1) {
openPort(true);
setNextDuration(animDuration);
} else if (getRunCount() == 2) {
releaseAttackShips();
setNextDuration(openDuration);
} else if (getRunCount() == 3) {
openPort(false);
setNextDuration(animDuration);
} else if (getRunCount() == 4) {
// Disable auto-repeat to flush from the queue
setAutoRestart(false);
}
}
};
}

// In your update loop
public void update(float tpf) {
if (!screen.getAnimManager().hasGameTimer(releaseTimer)) {
// Reset game timer start time to 0
releaseTimer.reset(false);
// Re-enable auto-repeat
releaseTimer.setAutoRestart(true);
// Set the initial duration
releaseTimer.setDuration(FastMath.nextRandomFloat()*5f+1f);
// Reset the run count
releaseTimer.resetRunCount();
// Add the timer
screen.getAnimManager().addGameTimer(releaseTimer);
}
}
[/java]

1 Like

Thought I’d add a vid from the current Android game I’m working on (a version of the Galaga type clone in Android friendly version):

[video]http://youtu.be/aXdv-aONpnk[/video]

This uses the above technique for handling all mothership scripts.

EDIT: Heh… the whole reason I posted this was to mention:

  1. I’ll be adding this to the Play Store soon
  2. I’ll be posting a link to the project so you can pick through it for ideas.
3 Likes

Hey that looks great i will buy the game for my android smartphone or so =). i will try to programm my 2d game too to show some stuff

1 Like
@Snowsun92 said: Hey that looks great i will buy the game for my android smartphone or so =). i will try to programm my 2d game too to show some stuff

I’m adding this as a free app. It’s my testbed for the 2D framework. Once it’s released, I’ll post the project here for others to go through.

1 Like