@Override public void run() { this.run = true; Canvas c = null; while (run) { try { c = this.surfaceHolder.lockCanvas(null); synchronized (this.surfaceHolder) { currentTime = System.currentTimeMillis(); if (currentTime > previousTime + mFPS || true) { previousTime = currentTime; updatePhysics(); mario.update(currentTime); sprCoin.Update(currentTime); sprBlock.Update(currentTime); pipePlant.Update(currentTime); if (pipePlant.getYPos() <= this.pipePlantInitY) { pipePlant.setYVel(1); } if (pipePlant.getYPos() >= this.pipePlantInitY + 70) { pipePlant.setYVel(-1); } doDraw(c); } } } finally { if (c != null) { this.surfaceHolder.unlockCanvasAndPost(c); } } // pause if no need to animate synchronized (this) { if (wait) { try { wait(); } catch (Exception e) { } } } } }
/** Invoke when the surface dimension change */ public void setSurfaceSize(int width, int height) { this.width = width; this.height = height; bkg.setXPos(0); bkg.setYPos(height - 642 - 154); sprCoin.setYPos(500); sprCoin.setXPos(100); sprBlock.setYPos(500); sprBlock.setXPos(600); sprPipe.setXPos(324); sprPipe.setYPos(548); pipePlant.setXPos(328); pipePlant.setYPos(504); pipePlantInitY = pipePlant.getYPos(); synchronized (this) { this.notify(); } }