0

OK for sake of argument and simplicity this code here has a rectangle sprite/texture that shoots(cuz it's a gun) upwards. And an enemy rectangle/sprite/texture the spawns downwards. Then the player detects if it hits a enemy. When the player hits an enemy I get an out of bounds exception -1

package com.TheGame.Pack;

import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class GameScreen implements Screen {
final MasterClass game;

Texture FleetTexture;
Texture PlayerTexture;
Texture ShootingTexture;





OrthographicCamera camera;

Rectangle Player;


Array<Rectangle> Emma;
Array<Rectangle> Shooting;

long EmmaSpawnTime;
long ShootingTime;
public static int EmmaKilled = 0;

public GameScreen(final MasterClass gam) {
    this.game = gam;

    // load the images for the droplet and the Player, 64x64 pixels each
    FleetTexture = new Texture(Gdx.files.internal("cirA.png")); //Enemies
    PlayerTexture = new Texture(Gdx.files.internal("BoxA.png"));
    ShootingTexture = new Texture(Gdx.files.internal("gun.png"));


    // load the drop sound effect and the rain background "music"
//  dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
//  rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
//  rainMusic.setLooping(true);

    // create the camera and the SpriteBatch
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 800, 480);

    // create a Rectangle to logically represent the Player
    Player = new Rectangle();
    Player.x = 800 / 2 - 64 / 2; // center the Player horizontally
    Player.y = 20; // bottom left corner of the Player is 20 pixels above
    // the bottom screen edge
    Player.width = 40;
    Player.height = 30;

    // create the Emma array and spawn the first EmmaInArray
    Emma = new Array<Rectangle>();
    Shooting = new Array<Rectangle>();
    spawnEmma();

}

private void spawnEmma() {
    Rectangle EmmaInArray = new Rectangle();
    EmmaInArray.x = MathUtils.random(0, 800 - 64);
    EmmaInArray.y = 480;
    EmmaInArray.width = 40;
    EmmaInArray.height = 30;
    Emma.add(EmmaInArray);
    EmmaSpawnTime = TimeUtils.nanoTime();
}

private void spawnShooting(){
    Rectangle ShootingInArray = new Rectangle();
    ShootingInArray.x = Player.x;
    ShootingInArray.y = Player.y;
    ShootingInArray.width = 40;
    ShootingInArray.height = 30;
    Shooting.add(ShootingInArray);
    ShootingTime = TimeUtils.nanoTime();


} 

@Override
public void render(float delta) {
    // clear the screen with a dark blue color. The
    // arguments to glClearColor are the red, green
    // blue and alpha component in the range [0,1]
    // of the color to be used to clear the screen.
    Gdx.gl.glClearColor(0, 0, 0.2f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

     // tell the camera to update its matrices.
     camera.update();

    // tell the SpriteBatch to render in the
    // coordinate system specified by the camera.
    game.batch.setProjectionMatrix(camera.combined);

    // begin a new batch and draw the Player and
    // all drops
    game.batch.begin();
    game.font.draw(game.batch, "Drops Collected: " + EmmaKilled, 0, 480);
    game.batch.draw(PlayerTexture, Player.x, Player.y, Gdx.graphics.getWidth() / 20,

     Gdx.graphics.getHeight()/ 20);


    for (Rectangle EmmaInArray : Emma) {
        game.batch.draw(FleetTexture, EmmaInArray.x, EmmaInArray.y);
    }

    for(Rectangle ShootingInArray : Shooting){
        game.batch.draw(ShootingTexture, ShootingInArray.x, ShootingInArray.y);
        ShootingInArray.y +=10;
    }  


    game.batch.end();

    // process user input
    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);
        Player.x = touchPos.x - 64 / 2;
    }
    if (Gdx.input.isKeyPressed(Keys.LEFT))
        Player.x -= 400 * Gdx.graphics.getDeltaTime();
    if (Gdx.input.isKeyPressed(Keys.RIGHT))
        Player.x += 400 * Gdx.graphics.getDeltaTime();

    // make sure the Player stays within the screen bounds
    if (Player.x < 0)
        Player.x = 0;
    if (Player.x > 800 - 64)
        Player.x = 800 - 64;

    // check if we need to create a new EmmaInArray
    if (TimeUtils.nanoTime() - EmmaSpawnTime > 100000000){
        spawnEmma();
    }

    if(TimeUtils.nanoTime() - ShootingTime > 100000000){
        spawnShooting();
    } 
    // move the Emma, remove any that are beneath the bottom edge of
    // the screen or that hit the Player. In the later case we play back
    // a sound effect as well.
    Iterator<Rectangle> EmmaIterator = Emma.iterator();


    while (EmmaIterator.hasNext()) {
        Rectangle EmmaInArray = EmmaIterator.next();
        EmmaInArray.y -= 200 * Gdx.graphics.getDeltaTime();

        if (EmmaInArray.y + 64 < 0){
            EmmaIterator.remove();
        }


        Iterator<Rectangle> ShootingIterator = Shooting.iterator();
        while(ShootingIterator.hasNext()){

            Rectangle ShootingInArray = ShootingIterator.next();
        //  ShootingInArray.y += 200 * Gdx.graphics.getDeltaTime();

            if(ShootingInArray.y > 480){
                ShootingIterator.remove();
            }

            if(EmmaInArray.overlaps(ShootingInArray)){

                ShootingIterator.remove();
                EmmaIterator.remove();
            }   
            if (Player.overlaps(EmmaInArray)) {
                EmmaKilled++;
                game.setScreen(game.HS);
            //  dropSound.play();

if I comment out EmmaIterator.remove(); it runs fine with it uncommented it crashes upon hit.

Why does this crash is this not the proper way to do this? Do I need to somehow detect hit's at the same time? How can the array be at negative 1 when clearly there are still enemies on the screen?

                EmmaIterator.remove();
            }
        } 

Though this is not the way I will have things setup this code still should run with no issues. I encounter the same problem when instead of detecting the player enemies collisions I have 2 guns checking for collisions. This seems like a big problem to me which is why I'd say I'm just doing it wrong but documentation is light so I come here.

    }
}

@Override
public void resize(int width, int height) {
}

@Override
public void show() {
    // start the playback of the background music
    // when the screen is shown
    //rainMusic.play();
}

@Override
public void hide() {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {
    FleetTexture.dispose();
    PlayerTexture.dispose();
    ShootingTexture.dispose();
//  dropSound.dispose();
//  rainMusic.dispose();
}

}

0

It's unlikely, unless multithreading is being used, that anything will happen at the same time exactly. You have a number of probable typos in your code, but one will break it:

Rectangle var1_holder = iter.next();

That same reference to iter is in both the first block, which should use it, and the second block, which should use iter1. You should consider using matching variable names, like

Iterator<Rectangle> iter3 = var3.iterator();

if you must have numbers as the only distinguishing feature.

|improve this answer|||||
  • 1
    The reason for this is that iter1.next() must be called before iter1.remove(), and iter1.next() is never being called in the second block. – notostraca Sep 4 '14 at 0:11
0

notostraca is right. But to make it more clear i will show u an example with for loops which i use for collisions. it can't make any harm and i hope it will make it more clear for u

int v2 = var2.size();
for (int i = 0; i < v2; i++) {
    if (object.getBounds().overlaps(var2.get(i).getBounds())) {
        var2.remove(i);  
        v2--;
        //in this line u might use break; if u know that just one object 
        //from var2 array can hit at a time
    }
}
|improve this answer|||||
  • After the line: EmmaIterator.remove(); use: System.out.println(); and trace literator variables in line on which compiler gives u errors. I will check this better when i get home, coz i cant scrol down in my phone... – raco Sep 11 '14 at 8:36
  • Just spits out 284.0, 336.73978, 40.0, 30.0 I have no idea what's wrong – user3929948 Sep 13 '14 at 2:14

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.