So I have this problem that the Rectangle.overlaps isn't working for some reason or I don't know. All this does is it draws a jet (MyActor) and a bullet Actor and on touch it makes the bullet move towards the blue enemy Actor and i want it to System.out.println("touched"); if bullet hits the enemy.
I'll just paste the whole code.
public class MyGdxGame implements ApplicationListener{
private Texture texture;
private Texture bulletTexture;
private Texture enemyTexture;
private MyActor myActor;
private BulletActor bulletActor;
private EnemyActor enemyActor;
private Stage stage;
private BackGroundActor backGroundActor;
private Rectangle bulletRectangle;
private Rectangle enemyRectangle;
float X=500,Y =600;
float bulletX = X, bulletY = Y+43;
float enemyX =0, enemyY = 615;
boolean started;
int touched = 0;
public class EnemyActor extends Actor{
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(enemyTexture,enemyX,enemyY);
}
@Override
public void act(float delta) {
if(bulletRectangle.overlaps(enemyRectangle)){
System.out.println("Overlaps");
}
}
}
public class BackGroundActor extends Actor{
public BackGroundActor() {
setBounds(0,0,1280,720);
addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
started = true;
touched++;
System.out.println(touched);
return true;
}
});
}
}
public class MyActor extends Actor{
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture,X,Y);
}
}
public class BulletActor extends Actor{
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(bulletTexture,bulletX,bulletY);
}
@Override
public void act(float delta) {
if(started) {
bulletX -= 3;
}
}
}
@Override
public void create() {
texture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\0001.png");
bulletTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\bullet.png");
enemyTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\fuel.png");
bulletRectangle = new Rectangle(bulletX,bulletY,bulletTexture.getWidth(),bulletTexture.getHeight());
enemyRectangle = new Rectangle(enemyX,enemyY,enemyTexture.getWidth(),enemyTexture.getHeight());
stage = new Stage();
myActor = new MyActor();
enemyActor = new EnemyActor();
bulletActor = new BulletActor();
backGroundActor = new BackGroundActor();
backGroundActor.setTouchable(Touchable.enabled);
stage.addActor(bulletActor);
stage.addActor(myActor);
stage.addActor(backGroundActor);
stage.addActor(enemyActor);
Gdx.input.setInputProcessor(stage);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
if(enemyRectangle.overlaps(bulletRectangle)){
System.out.println("HIT I SAY");
}
}
@Override
public void resize ( int width, int height){
}
@Override
public void pause () {
}
@Override
public void resume () {
}
}
The issue here is that the bulletRectangle
is not updated with the new position of the bullet, bulletX
is not directly linked to bulletRectangle
.
You could do
if(started) {
bulletX -= 3;
bulletRectangle.setX(bulletX);
}
You might want also to update the other rectangle with the proper position of the enemy
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments