0
Я использую libGDX для создания игры, такой как кривая лихорадка, но для Android.
На самом деле я работаю над столкновением, но все, что я пробовал, не будет работать; Я использую методы overlaps
класса Rectangle
.
Он всегда возвращает false
.Метод перекрытий не работает
Вот код, который проверяет, есть ли столкновение:
public void collision() {
if(head.getAlive()) {
for (TailSnake tail : getTail()) {
if (tail.lifeTime > 2)
if (head.bounds.overlaps(tail.bounds))
head.dead();
}
for (Object wallObj : getWall()) {
Wall wall = (Wall) wallObj;
if (head.bounds.overlaps(wall.bounds))
head.dead();
}
}
}
код The TailSnake в:
package com.me.mygdxgame;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class TailSnake {
static final float SIZE = 0.1f;
Vector2 position = new Vector2();
Rectangle bounds = new Rectangle();
float lifeTime = 0;
public TailSnake(Vector2 position) {
this.position = position;
this.bounds.height = SIZE;
this.bounds.width = SIZE;
}
public Rectangle getBounds() {
return bounds;
}
public Vector2 getPosition() {
return position;
}
public void update(float delta) {
lifeTime += delta;
}
}
И, наконец, код HeadSnake:
package com.me.mygdxgame;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class HeadSnake extends TailSnake{
static final float SPEED = 1f;
static final double ROTATE_SPEED = 200;
Rectangle bounds = new Rectangle();
Vector2 velocity = new Vector2();
boolean alive = true;
public HeadSnake(Vector2 position) {
super(position);
this.velocity.x = 0;
this.velocity.y = SPEED;
}
public Vector2 getVelocity() {
return velocity;
}
public boolean getAlive() {
return alive;
}
public void update(float delta) {
if (alive)
position.add(velocity.cpy().scl(delta));
}
public void dead() {
this.alive = false;
}
}
Спасибо, много. Я понятия не имею, почему я об этом не думал. – Poroing
@ user2984573 Я рад помочь. :) –