我正在制作一个小型的2d滚动游戏,由于某种原因我的碰撞检测不起作用。我无法弄明白我已经尝试修复它几天但似乎没有任何效果。你能帮助我吗!提前致谢!!!!
public class GameScreen extends Screen {
enum GameState {
Ready, Running, Paused, GameOver
}
static GameState state = GameState.Ready;
// Variable Setup
// You would create game objects here.
public Paint paint, paint2;
public static int gameHeight = AndroidGame.height;
public static int gameWidth = AndroidGame.width;
public static int byX = SampleGame.x;
public static int byY = SampleGame.y;
public static int x = 60;
public static int y = AndroidGame.height - 80;
public static int surface = AndroidGame.height - 80;
public static int numberSq = 25;
public static int[] values;
public double counter2 = 4;
public int counterStep;
public Image person;
public static Image[] walk = new Image[9];
public static Image object;
private Animation anim;
public static int up = 0;
public static int right = 0;
public long jumpTime = 200;
public static Rect[] valuesSq;
public static Rect[] valuesSq2;
public static Rect[] valuesSq3;
public static Rect rectOne;
public static Rect rectTwo;
public int colide = 0;
public static boolean boolResult;
private Collision collision;
public GameScreen(Game game) {
super(game);
rectOne = new Rect(x, y, 27, 75);
valuesSq = new Rect[numberSq];
valuesSq2 = new Rect[numberSq];
valuesSq3 = new Rect[numberSq];
values = new int[numberSq];
values[0] = 300;
for (int i = 1; i < numberSq; i++) {
values[i] = (values[i - 1] + 200);
}
object = Assets.object;
walk[1] = Assets.walk[1];
walk[2] = Assets.walk[2];
walk[3] = Assets.walk[3];
walk[4] = Assets.walk[4];
walk[5] = Assets.walk[5];
walk[6] = Assets.walk[6];
walk[7] = Assets.walk[7];
walk[8] = Assets.walk[8];
anim = new Animation();
anim.addFrame(walk[1], 50);
anim.addFrame(walk[2], 50);
anim.addFrame(walk[3], 50);
anim.addFrame(walk[4], 50);
anim.addFrame(walk[5], 50);
anim.addFrame(walk[6], 50);
anim.addFrame(walk[7], 50);
anim.addFrame(walk[8], 50);
person = anim.getImage();
paint = new Paint();
paint.setTextSize(30);
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint2 = new Paint();
paint2.setTextSize(30);
paint2.setTextAlign(Paint.Align.CENTER);
paint2.setAntiAlias(true);
paint2.setColor(Color.WHITE);
}
@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
if (state == GameState.Ready)
updateReady(touchEvents);
if (state == GameState.Running)
updateRunning(touchEvents, deltaTime);
if (state == GameState.Paused)
updatePaused(touchEvents);
if (state == GameState.GameOver)
updateGameOver(touchEvents);
}
private void updateReady(List<TouchEvent> touchEvents) {
// This example starts with a "Ready" screen.
// When the user touches the screen, the game begins.
// state now becomes GameState.Running.
// Now the updateRunning() method will be called!
if (touchEvents.size() > 0) {
state = GameState.Running;
}
}
private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
// This is identical to the update() method from our Unit 2/3 game.
// x++;
right = 1;
if (right == 1) {
for (int i = 0; i < numberSq; i++) {
values[i] -= 4;
}
}
// 1. All touch input is handled here:
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_DOWN) {
if (event.x < 640) {
// Move left.
}
else if (event.x > 640) {
// Move right.
}
}
if (event.type == TouchEvent.TOUCH_UP && y == surface) {
up = 1;
new Thread(new thread()).start();
}
else if (event.x > 640) {
// Stop moving right. }
}
}
if (y < surface) {
y += 6;
}
if (y > surface) {
y = surface;
}
if (up == 1) {
// Stop moving left.
counter2 = 4;
counter2 += .1;
y = y + (int) ((Math.sin(counter2) + Math.cos(counter2)) * 14.2);
if (counter2 >= 10) {
counter2 = 4;
}
}
person = anim.getImage();
animate();
}
private void updatePaused(List<TouchEvent> touchEvents) {
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_UP) {
}
}
}
private void updateGameOver(List<TouchEvent> touchEvents) {
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type == TouchEvent.TOUCH_UP) {
if (event.x > 300 && event.x < 980 && event.y > 100
&& event.y < 500) {
nullify();
game.setScreen(new MainMenuScreen(game));
return;
}
}
}
}
@Override
public void paint(float deltaTime) {
Graphics g = game.getGraphics();
// g.drawImage(Assets.background, 0, 0);
for (int i = 0; i < numberSq; i++) {
valuesSq[0 + i] = new Rect(values[0 + i] - 6, surface - 35, 1,
110);
valuesSq2[0 + i] = new Rect(values[0 + i] - 6, 0, 1, 110);
if (Rect.intersects(rectOne,valuesSq[0 + i])
|| Rect.intersects(rectOne,valuesSq2[0 + i])) {
state=GameState.GameOver;
} else {
}
}
g.drawRect(0, 0, gameWidth, gameHeight, Color.CYAN);
g.drawRect(x, y, 27, 75, Color.BLACK);
g.drawImage(person, x, y);
for (int i = 0; i < numberSq; i++) {
g.drawImage(object, values[0 + i] - 6, surface - 35);
g.drawImage(object, values[0 + i] - 6, 0);
}
g.drawString("" + boolResult, 320, 480, paint);
// Secondly, draw the UI above the game elements.
if (state == GameState.Ready)
drawReadyUI();
if (state == GameState.Running)
drawRunningUI();
if (state == GameState.Paused)
drawPausedUI();
if (state == GameState.GameOver)
drawGameOverUI();
}
private void nullify() {
paint = null;
System.gc();
}
private void drawReadyUI() {
Graphics g = game.getGraphics();
g.drawARGB(155, 0, 0, 0);
g.drawString("Tap each side of the screen to move in that direction.",
320, 480, paint);
}
private void drawRunningUI() {
Graphics g = game.getGraphics();
}
private void drawPausedUI() {
Graphics g = game.getGraphics();
g.drawARGB(155, 0, 0, 0);
g.drawString("Resume", 400, 165, paint2);
g.drawString("Menu", 400, 360, paint2);
}
private void drawGameOverUI() {
Graphics g = game.getGraphics();
g.drawRect(0, 0, 320, 480, Color.BLACK);
g.drawString("GAME OVER.", 50, 50, paint);
}
@Override
public void pause() {
if (state == GameState.Running)
state = GameState.Paused;
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
@Override
public void backButton() {
pause();
}
public void animate() {
anim.update(10);
}
public class thread implements Runnable {
@Override
public void run() {
try {
Thread.sleep(jumpTime);
up = 0;
} catch (Exception e) {
e.printStackTrace();
new Thread(this).start();
System.exit(0);
}
}
}
}
答案 0 :(得分:1)
Rect
的参数不是Rect(x, y, width, height)
,而是Rect(left, top, right, bottom)
,因此出于您的目的,您可能希望使用Rect(x, y, x + width, y - height)
。