Re-use removed object from ArrayList?

edited March 2017 in Questions about Code

I just got a little familiar with using ArrayLists in my jump n run game, but I am not yet sure about how it all works. So, I have a collision detection that removes objects after collision with the player. Now I want to generate levels and I have made some kind of "portal" that the player has to collide with after hitting a certain score. It works fine, the player touches the portal, gets "teleported" to the next level and the game speed increases. The portal is removed based on the same principle, as I did before when the player collects the bonusses, which then disappear. However, I need to re-use the portal to get into the next level after hitting another score.. I guess the portal is then gone, thats why it doesnt work..

Main Program ArrayListteleport=new ArrayList(); boolean goToNextLevel; int level=1; int score=0; int hits=0;

void setup()
{
  size(600, 300);
  startGame();
}

void draw()
{          
 if (gameStart) {
    getItGoing();
  }
}

void startGame()
{
   gameStart = false;

  //portal
  teleport=new ArrayList<Portal>();
  teleport.add(new Portal(width/2, height/2, 50, 100));
  teleport.add(new Portal(width/2, height/2, 50, 100));

  //player; initial position
  player=new Player(50, height-70, 30, 40, height-70);      
}

void getItGoing() //start game after shift key is pressed
{
  if (millis()<startOfGame+durationOfGame) { 
//if game is still running and a certain Score is hit, boolean goToNextLevel turns true and Portal is displayed
    for (int i=0; i<teleport.size(); i++) {
      if (goToNextLevel==true) {
        Portal portal = (Portal) teleport.get(i);
        portal.displayPortal();
      }
    }
  }
}

boolean beamPlayer(float x, float y, float w, float h)
{  
  for (int i=0; i<teleport.size(); i++)
  {
    if (teleport.get(i).teleportationDone(x, y, w, h)) {
      teleport.remove(i);
      return true;
    }
  }
  return false;
}

Then the Portal class

    class Portal {

      int portalX = 0;
      int portalY = 0;
      int portalWidth =0;
      int portalHeight= 0;

      Portal (int pX, int pY, int pW, int pH) {

        portalX = pX;
        portalY = pY;
        portalWidth = pW;
        portalHeight = pH;
      }

      void displayPortal() {

        fill(0,0,0,100);
        ellipse(portalX, portalY, portalWidth, portalHeight);
      }


     boolean teleportationDone(float x, float y, float w, float h)
      {
        if ((x+w>=portalX-portalWidth/2) && (x<(portalWidth/2+portalX)) &&
            ((y+h) >= portalY-portalHeight/2) && (y < (portalY-portalHeight/2)))
        {
          println("teleported to next level");
          level+=1;
          return true;
        }
        return false;
      }
    }

and the Player class

class Player {
    boolean teleported = false;
    float playerX, playerY, playerWidth, playerHeight;

 void drawPlayer()
  {
    fill(255);
    rect(playerX, playerY, playerWidth, playerHeight);
  }

  void update()
  {
    teleported = false; //initial state: not collecting carrots
    if (beamPlayer(playerX, playerY, playerWidth, playerHeight))
    {
      //if(!collected) player.score++;
      teleported=true;
      //break not needed here, cause im not using a for loop to break out from like with carrots/platforms
    }
}
Tagged:

Answers

Sign In or Register to comment.