android game performance regarding timers

Posted by iQue on Game Development See other posts from Game Development or by iQue
Published on 2012-08-30T15:30:22Z Indexed on 2012/08/30 15:50 UTC
Read the original article Hit count: 292

Filed under:
|
|

Im new to the game-dev world and I have a tendancy to over-simplify my code, and sometimes this costs me alot fo memory.

Im using a custom TimerTask that looks like this:

    public class Task extends TimerTask {

    private MainGamePanel panel;
    public Task(MainGamePanel panel) {
    this.panel=panel;
    }

    /**
    * When the timer executes, this code is run.
    */
    public void run() {
    panel.createEnemies();
    }
    }

this task calls this method from my view:

    public void createEnemies() {
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.female);
    if(enemyCounter < 24){
        enemies.add(new Enemy(bmp, this));
    }
    enemyCounter++;
      }

Since I call this in the onCreate-method instead of in my views contructor (because My enemies need to get width and height of view). Im wondering if this will work when I have multiple levels in game (start a new intent). And if this kind of timer really is the best way to add a delay between the spawning-time of my enemies performance-wise.

adding code for my timer if any1 came here cus they dont understand timers:

    private Timer timer1 = new Timer();
    private long delay1 = 5*1000; // 5 sec delay

    public void surfaceCreated(SurfaceHolder holder) {
    timer1.schedule(new Task(this), 0, delay1); //I call my timer and add the delay
    thread.setRunning(true);
    thread.start();
}

© Game Development or respective owner

Related posts about android

Related posts about Performance