0

我正在尝试制作我认为简单的游戏。我一直在关注某人的教程,现在我正在尝试调整它并让它做我想做的事情。但是我找不到任何对我有用的答案,他们都提出了一些阻止它编译的错误。代码中的行是

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

我可以将第二个“50”更改为任何硬编码数字,它会移动,但我总是希望它在屏幕底部创建,不受屏幕大小的影响。所以基本上我希望 50 是 (screenheight - bitmapheight) 这是我正在使用的代码页面。

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 回答