我正在尝试在Android Studio中与Libgdx进行简单的游戏,玩家左/右移动宇宙飞船,躲避小行星随机进入屏幕。除了矩形在小行星上没有正确定位,或者没有注册重叠以在碰撞后结束游戏时,一切正常。
目前只有当小行星落下时玩家坐在游戏的右手边时,只有碰撞结束游戏,任何其他位置(甚至靠近右手边缘)都不会做任何事情。
非常感谢任何帮助。
我有一个船级:
package com.mygdx.game.Sprites;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.mygdx.game.Game2;
public class Ship {
private static int gravity;
private static int MOVEMENT = 50;
private Vector3 position;
private Vector3 velocity;
private Rectangle bounds;
private Texture ship;
public Ship(int x, int y){
position = new Vector3(x, y, 0);
velocity = new Vector3(0, 0, 0);
ship = new Texture("Ship3.png");
bounds = new Rectangle(x, y, ship.getWidth(), ship.getHeight());
// - 10 maybe for shape not being square
}
public void update(float dt){
velocity.add(gravity, 0, 0);
velocity.scl(dt);
position.add(velocity.x, MOVEMENT*dt, 0);
if (position.x < 0)
position.x = 0;
if(position.x + ship.getWidth() >= Game2.WIDTH/2) {
position.x = Game2.WIDTH/2 - ship.getWidth();
bounds.setPosition(position.x, position.y);
}
}
public Vector3 getPosition() {
return position;
}
public Texture getTexture() {
return ship;
}
public void turnLeft(){
gravity = -100;
}
public void turnRight() {
gravity = 100;
}
public Rectangle getBounds(){
return bounds;
}
public boolean collides(Rectangle asta){
return asta.overlaps(bounds);
}
}
Asta(小行星)的一个类:
> package com.mygdx.game.Sprites;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g3d.particles.influencers.ColorInfluencer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import java.util.Random;
public class Asta {
public static final int ASTA_HEIGHT = 36;
private static final int FLUCTUATION = 150;
private static final int ASTA_GAP = 80;
private Texture leftasta, rightasta;
private Vector2 posLeftAsta, posRightAsta;
private Rectangle boundsRight, boundsLeft;
private Random rand;
public Asta(float y) {
leftasta = new Texture("Rock.png");
rightasta = new Texture("Rock.png");
rand = new Random();
posRightAsta = new Vector2( rand.nextInt(FLUCTUATION) + ASTA_GAP, y);
posLeftAsta = new Vector2( posRightAsta.x - ASTA_GAP -
rightasta.getWidth(), y);
boundsRight = new Rectangle(posRightAsta.x, posRightAsta.y,
rightasta.getWidth(), rightasta.getHeight());
boundsLeft = new Rectangle(posLeftAsta.x, posLeftAsta.y,
leftasta.getWidth(), leftasta.getHeight());
}
public Texture getLeftasta() {
return leftasta;
}
public Texture getRightasta() {
return rightasta;
}
public Vector2 getPosLeftAsta() {
return posLeftAsta;
}
public Vector2 getPosRightAsta() {
return posRightAsta;
}
public void reposition(float y){
posRightAsta.set( rand.nextInt(FLUCTUATION) + ASTA_GAP, y);
posLeftAsta.set( posRightAsta.x - ASTA_GAP - rightasta.getWidth(), y);
boundsRight.setPosition(posRightAsta.x, y);
boundsLeft.setPosition(posLeftAsta.x, y);
}
public boolean collides(Rectangle player){
return player.overlaps(boundsRight) || player.overlaps(boundsLeft);
}
}
和我的PlayState:
package com.mygdx.game.States;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
import com.mygdx.game.Game2;
import com.mygdx.game.Sprites.Asta;
import com.mygdx.game.Sprites.Ship;
public class PlayState extends State {
private static final int ASTA_SPACING = 125;
private static final int ASTA_COUNT = 4;
private Ship ship;
private Texture background;
private Array<Asta> astas;
public PlayState(GameStateManager gsm) {
super(gsm);
ship = new Ship(Game2.WIDTH/4, Game2.HEIGHT/8);
cam.setToOrtho(false, Game2.WIDTH/2, Game2.HEIGHT/2);
// /2
background = new Texture("Background1.png");
astas = new Array<Asta>();
for (int i = 0; i <= ASTA_COUNT; i++){
astas.add(new Asta(i * ASTA_SPACING + Asta.ASTA_HEIGHT));
}
}
@Override
protected void handleInput() {
if (Gdx.input.justTouched())
ship.turnLeft();
{
if (Gdx.input.getX() > Gdx.graphics.getWidth() / 2)
{
ship.turnRight();
}
else
{
ship.turnLeft();
}
}
}
@Override
public void update(float dt) {
handleInput();
ship.update(dt);
cam.position .y = ship.getPosition().y+50 ;
//moves the camera focus around the ship;
for (Asta asta: astas){
if (cam.position.y - (cam.viewportHeight/2) > asta.getPosRightAsta().y + asta.getRightasta().getHeight()){
asta.reposition(asta.getPosRightAsta().y + ((Asta.ASTA_HEIGHT + ASTA_SPACING) * ASTA_COUNT));
}
if (asta.collides(ship.getBounds()))
gsm.set(new MenuState(gsm));
}
cam.update();
}
@Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(background, 0, cam.position.y - (cam.viewportHeight / 2), Game2.WIDTH /2, Game2.HEIGHT /2);
///2
sb.draw(ship.getTexture(), ship.getPosition().x, ship.getPosition().y);
for (Asta asta : astas) {
sb.draw(asta.getRightasta(), asta.getPosRightAsta().x, asta.getPosRightAsta().y);
sb.draw(asta.getLeftasta(), asta.getPosLeftAsta().x, asta.getPosRightAsta().y);}
sb.end();
}
@Override
public void dispose () {
}
}