Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

package com.edu4java.android.killthemall; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.view.

SurfaceHolder; import android.view.SurfaceView;

public class GameView extends SurfaceView { private Bitmap bmp; private SurfaceHolder holder; private GameLoopThread gameLoopThread; private Sprite sprite;

public GameView(Context context) { super(context); gameLoopThread = new GameLoopThread(this); holder = getHolder(); holder.addCallback(new SurfaceHolder.Callback() {

@Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; gameLoopThread.setRunning(false); while (retry) { try {

gameLoopThread.join(); retry = false; } catch (InterruptedException e) { } } }

@Override public void surfaceCreated(SurfaceHolder holder) { gameLoopThread.setRunning(true); gameLoopThread.start(); }

@Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.bad1); sprite = new Sprite(this,bmp); }

@Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.BLACK); sprite.onDraw(canvas); }

import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Rect;

public class Sprite { private static final int BMP_ROWS = 4; private static final int BMP_COLUMNS = 3; private int x = 0; private int y = 0; private int xSpeed = 5; private GameView gameView; private Bitmap bmp; private int currentFrame = 0; private int width; private int height;

public Sprite(GameView gameView, Bitmap bmp) { this.gameView = gameView; this.bmp = bmp; this.width = bmp.getWidth() / BMP_COLUMNS; this.height = bmp.getHeight() / BMP_ROWS; }

private void update() { if (x > gameView.getWidth() - width - xSpeed) { xSpeed = -5; }

if (x + xSpeed < 0) { xSpeed = 5; } x = x + xSpeed; currentFrame = ++currentFrame % BMP_COLUMNS; }

public void onDraw(Canvas canvas) { update(); int srcX = currentFrame * width; int srcY = 1 * height; Rect src = new Rect(srcX, srcY, srcX + width, srcY + height); Rect dst = new Rect(x, y, x + width, y + height); canvas.drawBitmap(bmp, src, dst, null); } }=====================++++++++++++++++==========================

public class SampleActivity extends SimpleBaseGameActivity { protected static final int CAMERA_WIDTH = 800; protected static final int CAMERA_HEIGHT = 480; private ITextureRegion Box1, Box2;; private Stack Stack1, Stack2; private Sprite small_box,big_box; //private final Sprite small_box = new Sprite[1]; public EngineOptions onCreateEngineOptions() { final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); } public void onCreateResources() { try { ITexture small_box = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() { public InputStream open() throws IOException {

return getAssets().open("gfx/small_box.png"); } }); ITexture big_box = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() { public InputStream open() throws IOException { return getAssets().open("gfx/big_box.png"); } }); small_box.load(); big_box.load(); this.Box1 = TextureRegionFactory.extractFromTexture(small_box); this.Box2 = TextureRegionFactory.extractFromTexture(big_box); this.Stack1 = new Stack(); this.Stack2 = new Stack(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Scene onCreateScene() { final Scene scene = new Scene(); scene.setBackground(new Background(1, 1, 1)); for( int i=0;i<2;i++){ small_box = new Sprite(i*300, 100, Box1, this.getVertexBufferObjectManager()) { public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { if (((Sprite) this.getmStack().peek()).getHeight() != this .getHeight()) return false; this.setPosition(pSceneTouchEvent.getX() this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2); if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){ checkForCollisionsWithBoxes(); } return true; } private Stack getmStack() { // TODO Auto-generated method stub return Stack2; } }; big_box = new Sprite(i*300, 200, Box2, this.getVertexBufferObjectManager()) { public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) { if (((Sprite) this.getmStack().peek()).getHeight() != this .getHeight()) return false;

this.setPosition(pSceneTouchEvent.getX() this.getWidth() / 2, pSceneTouchEvent.getY() this.getHeight() / 2); if (pSceneTouchEvent.getAction() == TouchEvent.ACTION_UP){ checkForCollisionsWithBoxes(); } return true; } private Stack getmStack() { // TODO Auto-generated method stub return Stack1; } }; scene.attachChild(small_box); scene.attachChild(big_box); this.Stack1.add(big_box); this.Stack2.add(small_box); scene.registerTouchArea(small_box); scene.registerTouchArea(big_box); scene.setTouchAreaBindingOnActionDownEnabled(true); } return scene; } private void checkForCollisionsWithBoxes() { if ((big_box.collidesWith(small_box) && (Stack1.size() == 0 || small_box.getHeight() < ((Sprite) Stack1.peek()).getHeight()))) { small_box.setPosition(100+big_box.getWidth()/2, 50+ big_box.getHeight()); big_box.setPosition(300+small_box.getWidth()/2, 250+small_box.getHeight()/2); } } }

You might also like