0

簡単なゲームだと思っていたものを作ろうとして遊んでいます。私は誰かのチュートリアルに従ってきましたが、今はそれを微調整して、やりたいことをやらせようとしています。しかし、私にとってはうまくいく答えが見つかりません。彼らはすべて、コンパイルを停止するエラーを出します。コードの行は

droid = new Droid(BitmapFactory.decodeResource(getResources(),
    R.drawable.droid_1), 50, 50);

2番目の「50」を任意のハードコードされた数字に変更でき、移動しますが、画面サイズに関係なく、常に画面の下部に作成したいと考えています. だから基本的に私は 50 を (スクリーンの高さ - ビットマップの高さ) にしたい ここに私が取り組んでいるコードのページがあります.

import net.obviam.droidz.model.Droid;
import net.obviam.droidz.model.components.Speed;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;


/**
 * This is the main surface that handles the ontouch events and draws
 * the image to the screen.
*/
public class MainGamePanel extends SurfaceView implements
    SurfaceHolder.Callback {

private static final String TAG = MainGamePanel.class.getSimpleName();

private MainThread thread;
private Droid droid;

public MainGamePanel(Context context) {
    super(context);
    // adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    // create droid and load bitmap
      droid = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.droid_1), 50, 50);

    // create the game loop thread
    thread = new MainThread(getHolder(), this);

    // make the GamePanel focusable so it can handle events
    setFocusable(true);


}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // at this point the surface is created and
    // we can safely start the game loop
    thread.setRunning(true);
    thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d(TAG, "Surface is being destroyed");
    // tell the thread to shut down and wait for it to finish
    // this is a clean shutdown
    boolean retry = true;
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // try again shutting down the thread
        }
    }
    Log.d(TAG, "Thread was shut down cleanly");
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        // delegating event handling to the droid
        droid.handleActionDown((int)event.getX(), (int)event.getY());

        // check if in the lower part of the screen we exit
        if (event.getX() > getWidth() /2) {
            thread.setRunning(false);
            ((Activity)getContext()).finish();

        } else {

            droid.setX((int)event.getX());
            droid.setY((int)event.getY());

        }

    } if (event.getAction() == MotionEvent.ACTION_MOVE) {
        // the gestures
        if (droid.isTouched()) {
            // the droid was picked up and is being dragged
            droid.setX((int)event.getX());
            droid.setY((int)event.getY());
        }
    } if (event.getAction() == MotionEvent.ACTION_UP) {
        // touch was released
        if (droid.isTouched()) {
            droid.setTouched(false);
        }
    }


    return true;
}

public void render(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    droid.draw(canvas);
}

/**
 * This is the game update method. It iterates through all the objects
 * and calls their update method if they have one or calls specific
 * engine's update method.
 */
public void update() {
    // check collision with right wall if heading right
    if (droid.getSpeed().getxDirection() == Speed.DIRECTION_RIGHT
            && droid.getX() + droid.getBitmap().getWidth() / 2 >= getWidth()) {
        droid.getSpeed().toggleXDirection();
    }
    // check collision with left wall if heading left
    if (droid.getSpeed().getxDirection() == Speed.DIRECTION_LEFT
            && droid.getX() - droid.getBitmap().getWidth() / 2 <= 0) {
        droid.getSpeed().toggleXDirection();
    }
    // check collision with bottom wall if heading down
    if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN
            && droid.getY() + droid.getBitmap().getHeight() / 2 + 2 >= getHeight()) {
        droid.getSpeed().toggleYDirection();
    }
    // check collision with top wall if heading up
    if (droid.getSpeed().getyDirection() == Speed.DIRECTION_UP
            && droid.getY() - droid.getBitmap().getHeight() / 2 - 2 <= 0) {
        droid.getSpeed().toggleYDirection();
    }
    // Update the lone droid
    droid.update();
}

}

4

1 に答える 1

0

このようなものが必要ですか?使用している API のバージョンがわからないので、初期の API のコード例を示します。

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

私が理解したことを願っています、頑張ってください。

于 2012-05-10T00:22:46.920 に答える