2
\$\begingroup\$

I am trying to create random bullets but it is not working for some reason. also how can I make a delay so the bullets come every 30 seconds or 1 minute? Also the onTouch method does not work and it is not taking the bullet away?

Shall I put the array in the GameRender class?

bullet class

public class Bullet extends Sprite {

public static final float BULLET_HOMING = 6000; 
public static final float BULLET_SPEED = 300; 
private Vector2 velocity;
private float lifetime;
private Rectangle bul;

public Bullet(float x, float y) {
 velocity = new Vector2(0, 0);
     setPosition(x, y);
AssetLoader.bullet1.flip(true, false);
AssetLoader.bullet2.flip(true, false);
setSize(AssetLoader.bullet1.getWidth(), AssetLoader.bullet1.getHeight());
bul = new Rectangle();
}

public void update(float delta) {
 float targetX = GameWorld.getBall().getX();
 float targetY = GameWorld.getBall().getY();
 float dx = targetX - getX();
 float dy = targetY - getY();

 float distToTarget = (float) Math.sqrt(dx * dx + dy * dy); 
 dx /= distToTarget;
     dy /= distToTarget;
 dx *= BULLET_HOMING;
 dy *= BULLET_HOMING;
 velocity.x += dx * delta;
 velocity.y += dy * delta;

 float vMag = (float) Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
 velocity.x /= vMag;
 velocity.y /= vMag;
 velocity.x *= BULLET_SPEED;
 velocity.y *= BULLET_SPEED;

 bul.set(getX(), getY(), getOriginX(), getOriginY());

 Vector2 v = velocity.cpy().scl(delta);
 setPosition(getX() + v.x, getY() + v.y);
 setOriginCenter(); 
 setRotation(velocity.angle());
 }

public Rectangle getBounds() {
return bul;
}
public Rectangle getBounds1() {
return this.getBoundingRectangle();
}
}

game world class

public class GameWorld {

public static Ball ball;
private Bullet bullet1;
    private ScrollHandler scroller;
    private Array<Bullet> bullets = new Array<Bullet>();

    public GameWorld() {
    ball = new Ball(280, 273, 32, 32);
    bullet = new Bullet(-300, 200);
    scroller = new ScrollHandler(0);
    bullets.add(new Bullet(bullet.getX(), bullet.getY()));

    bullets = new Array<Bullet>();

    Bullet bullet = null;
    float bulletX = 0.0f;
    float bulletY = 0.0f;
    for (int i=0; i < 10; i++) {
       bulletX = MathUtils.random(-10, 10);
       bulletY = MathUtils.random(-10, 10);
       bullet = new Bullet(bulletX, bulletY);

       bullets.add(bullet); 
    }
}

public void update(float delta) {
    ball.update(delta);
    bullet.update(delta);
    scroller.update(delta);
}

public static Ball getBall() {
    return ball;
}

public ScrollHandler getScroller() {
    return scroller;
}

public Bullet getBullet1() { 
    return bullet1;
}
}

This is my game render class

public class GameRenderer {
private Bullet bullet;
 Ball ball;

 public GameRenderer(GameWorld world) {
 myWorld = world;
 cam = new OrthographicCamera();
 cam.setToOrtho(true, 480, 320);

 batcher = new SpriteBatch();
 // Attach batcher to camera
 batcher.setProjectionMatrix(cam.combined);

 shapeRenderer = new ShapeRenderer();
 shapeRenderer.setProjectionMatrix(cam.combined);

 // Call helper methods to initialize instance variables
 initGameObjects();
 initAssets();
 }

 private void initGameObjects() {
 ball = GameWorld.getBall();
 bullet = myWorld.getBullet();
 scroller = myWorld.getScroller();
 }

 private void initAssets() {
 ballAnimation = AssetLoader.ballAnimation;
 bulletAnimation = AssetLoader.bulletAnimation;
 }

 public void render(float runTime) {

 Gdx.gl.glClearColor(0, 0, 0, 1);
 Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

 batcher.begin();

 batcher.disableBlending();


 batcher.enableBlending();


 batcher.draw(AssetLoader.ballAnimation.getKeyFrame(runTime), ball.getX(),     ball.getY(), ball.getWidth(), ball.getHeight());

    batcher.draw(AssetLoader.bulletAnimation.getKeyFrame(runTime), bullet.getX(), bullet.getY(), bullet.getOriginX(), bullet.getOriginY(), bullet.getWidth(), bullet.getHeight(), 1.0f, 1.0f, bullet.getRotation());

 // End SpriteBatch
batcher.end();
 }
 }

I also tried this and it is not working, I used this in the GameRender class

Array<Bullet> enemies=new Array<Bullet>();

//in the constructor of the class
enemies.add(new Bullet(bullet.getX(), bullet.getY()));

 // this throws an exception for some reason??? this is in the render method
for(int i=0; i<bullet.size; i++)
   bullet.get(i).draw(batcher);

//this I am using in any method that will allow me from the constructor to update to render
for(int i=0; i<bullet.size; i++)
bullet.get(i).update(delta);

this is not taking the bullet out

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
for(int i=0; i<bullet.size; i++)
 if(bullet.get(i).getBounds().contains(screenX,screenY))
     bullet.removeIndex(i--);
return false;
}

Thanks for the help anyone.

\$\endgroup\$
  • \$\begingroup\$ is there anyone to help? \$\endgroup\$ – johnny-b Jun 10 '14 at 19:04
  • \$\begingroup\$ Can you post your Bullet class? \$\endgroup\$ – Cristina Jun 11 '14 at 7:50
  • \$\begingroup\$ hi Cristina i added all what i can if you need any more info please let me know. thank you @Cristina \$\endgroup\$ – johnny-b Jun 11 '14 at 12:00
  • \$\begingroup\$ What are you trying to do? \$\endgroup\$ – wes Jun 11 '14 at 12:08
  • \$\begingroup\$ i am trying to make a array of enemies randomly come from outside the screen to the target. and using onTouch() method to remove them. and with a delay so they dont come all at once. \$\endgroup\$ – johnny-b Jun 11 '14 at 12:26
1
\$\begingroup\$

Please try the following, went through it again and I think this is the issue:

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
   for(int i=0; i<bullet.size; i++)
   {
      if(bullet.get(i).getBounds().contains(screenX,screenY))
         bullet.removeIndex(i);
         //with bullet.removeIndex(i--) - you are removing the previous index 
         //from the array indifferent if it's bounding box touched the screen position from onTouch.
   }
   return false;
}
\$\endgroup\$
  • \$\begingroup\$ hi Cristina it is still not working. where is the best place to render the array? would it be better to change all of it and put the array in the GameRenderer class and create the 10 random bullets? thanks \$\endgroup\$ – johnny-b Jun 11 '14 at 14:51
  • \$\begingroup\$ all the array code shall i put it to the GameRenderer class from all the other classes? also i made a void onClick in the render class and initialized it in the InputHandler class which has the onTouch() method \$\endgroup\$ – johnny-b Jun 11 '14 at 15:00
  • \$\begingroup\$ I think there is a lot of confusion what are you doing and where and you should first clean up your game logic before going further. Always go update and then render and you should keep them separated, the Rendering system does not need to know anything about game objects, all that it's concerned about is rendering. \$\endgroup\$ – Cristina Jun 11 '14 at 15:02
  • \$\begingroup\$ is there anyway i can send you the whole code all of the classes, thank you @Cristina \$\endgroup\$ – johnny-b Jun 11 '14 at 15:19
  • \$\begingroup\$ This is a nice exercise if you are new to programming because you can do a lot of simple stuff. 1) Cleaning up your code, separating the update logic versus rendering. 2) Debugging. See if after you call removeIndex you still have the exact same number of objects inside the array. \$\endgroup\$ – Cristina Jun 11 '14 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.