/** Do the actual drawing stuff */ private void doDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); bkg.draw(canvas); sprCoin.draw(canvas); pipePlant.draw(canvas); sprPipe.draw(canvas); sprBlock.draw(canvas); mario.draw(canvas); }
/** Invoke while the screen is touched */ public void doTouchEvent(MotionEvent event) { // handle the event here // if there is something to animate // then wake up if (event.getAction() == 0) { this.wait = false; mario.jump(); synchronized (this) { notify(); } } }
/** * Update the animation, sprites or whatever. If there is nothing to animate set the wait * attribute of the thread to true */ private void updatePhysics() { // if nothing was updated : // this.wait = true; // this.wait = !mario.wasUpdated(); // if(mSensor.isUpdated()) { float[] sensorData = mSensor.getAccel(); mTheta = Math.toDegrees(Math.atan2(sensorData[1], sensorData[0])); if (mTheta < 89) { if (mario.getState() != Mario.states.JumpLeft && mario.getState() != Mario.states.JumpRight) { mario.setState(Mario.states.RunLeft); } } else if (mTheta > 91) { if (mario.getState() != Mario.states.JumpLeft && mario.getState() != Mario.states.JumpRight) { mario.setState(Mario.states.RunRight); } } else { if (mario.getState() == Mario.states.RunLeft) { mario.setState(Mario.states.StandLeft); } else if (mario.getState() == Mario.states.RunRight) { mario.setState(Mario.states.StandRight); } } }
@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) { } } } } }
public void onOffsetsChanged( float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { bkg.setBkgShift(xPixelOffset); mario.setxOffset(0 /*_xPixelOffset - xPixelOffset*/); sprCoin.setBkgShift(xPixelOffset); sprPipe.setBkgShift(xPixelOffset); sprBlock.setBkgShift(xPixelOffset); pipePlant.setBkgShift(xPixelOffset); _xPixelOffset = xPixelOffset; /*for(int i=0;i<numSprites;i++) { spriteList[i].setFgShift(xPixelOffset); }*/ }
public LiveWallpaperPainting(SurfaceHolder surfaceHolder, Context context) { // keep a reference of the context and the surface // the context is needed if you want to inflate // some resources from your livewallpaper .apk this.surfaceHolder = surfaceHolder; this.context = context; // don't animate until surface is created and displayed this.wait = true; mario = new Mario(64, 578, context); // spriteList = new AnimatedSprite[16]; // numSprites = 0; sprCoin = new AnimatedSprite(); sprPipe = new AnimatedSprite(); sprBlock = new AnimatedSprite(); pipePlant = new AnimatedSprite(); bkg = new AnimatedSprite(); sprCoin.setID(1); sprBlock.setID(2); sprPipe.Initialize( BitmapFactory.decodeResource(context.getResources(), R.drawable.pipe), 72, 48, 1, false, -1.0f, AnimatedSprite.objects.Pipe); sprCoin.Initialize( BitmapFactory.decodeResource(context.getResources(), R.drawable.coin), 24, 20, 4, true, 10.0f, AnimatedSprite.objects.Coin); sprBlock.Initialize( BitmapFactory.decodeResource(context.getResources(), R.drawable.animblock), 24, 24, 4, true, 10.0f, AnimatedSprite.objects.CoinBlock); pipePlant.Initialize( BitmapFactory.decodeResource(context.getResources(), R.drawable.pipeplant), 48, 40, 4, true, 10.0f, AnimatedSprite.objects.Plant); bkg.Initialize( BitmapFactory.decodeResource(context.getResources(), R.drawable.wallpaper_bkg), 1515, 642, 1, false, -1.0f, AnimatedSprite.objects.Bkg); sprPipe.setSolid(true); sprBlock.setSolid(true); // mario.addCollisionObj(sprPipe); mario.addCollisionObj(sprCoin); mario.addCollisionObj(sprBlock); mario.addCollisionObj(sprPipe); mSensor = new SensorActivity(context); }