Jump to content
  • Advertisement
Sign in to follow this  
subxaero33

Game lag on first level and rest is smooth?

This topic is 3613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys can anyone spot why my game has smooth graphics after the first level? I think it may be an initialization thing but I'm not seeing it. I need fresh eyes! :P Heres the class thats giving problems: package a3; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Iterator; import java.util.Observable; import java.util.Random; import javax.swing.JOptionPane; import javax.swing.Timer; /** * This class encapsulates the gameworld rules/logic and gamestate data. * @author Paul V Johnson III * */ public class GameWorld extends Observable implements ActionListener{ private GameCollection gameObjects; //Game state variables private double minScore; private int lives; private int level; private int ownedSquares; private int tickets; private int clock; private int elapsed; private Timer time; /** * @return the minScore */ public double getMinScore() { return minScore; } /** * @return the level */ public int getLevel() { return level; } /** * Constructor Gameworld() initializes the game level. */ public GameWorld(){ level = 0; resetGame(++level); time = new Timer(20,this); } /*@SuppressWarnings("unchecked") public GameCollection getSelected(){ GameObject n; GameCollection selectedObjects = new GameCollection(); Iterator itr = gameObjects.iterator(); while (itr.hasNext()){ if ((n = (GameObject) itr.next()) instanceof ISelectable){ selectedObjects.add(n); } } return selectedObjects; }*/ /** * This method is to start the game timer. */ public void startClock(){ time.start(); } /** * This method is to stop the game timer. */ public void stopClock(){ time.stop(); } /** * This method returns the number of monsterballs calculated by level * @return the quantity of monsterballs per level. */ private int gameMonsters() { return 3+((level-1)*1); } /** * This method calculates the dynamic timeticket time by level. * @return the dynamic tickettime */ private int levelTicketTime(){ return 30-((level-1)*5); } /** * Getter showClock to read the clock game state data. * @return clock */ public int getClock(){ return clock; } /** * This method dynamically updates the positions of all gameObjects that are movable. * @param i */ @SuppressWarnings("unchecked") private void updatePositions(int i){ Iterator itr = gameObjects.iterator(); GameObject n; while(itr.hasNext()){ n = (GameObject) itr.next(); n.move(i); } setChanged(); notifyObservers(); } /** * This method decrements the lives value * Alerts the user that an action has been registered. * The car is reset to it's starting position. */ @SuppressWarnings("unchecked") public void bam(){ lives--; Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).reset(); break; } } if(isGameOver()) gameOver(); setChanged(); notifyObservers(); } /** * When the game is over the user is prompted if they would like to play again. * If so, the game is reset to level 1. */ public void gameOver(){ int n = JOptionPane.showConfirmDialog(null, "Would you like to try again?", "GAME OVER", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) resetGame(1); else System.exit(1); } /** * This method determines if the game is over * @return true if the game is over * @return false if the game is still in play */ private boolean isGameOver() { return lives <= 0; } /** * This method resets the game in the event that the game is over and wants to be replayed * or when the player has advanced to the next level. * @param lvl */ private void resetGame(int lvl){ ownedSquares = 396; level = lvl; minScore = levelMinScore(); clock = gameTime(); if (level ==1) lives = 3; //else lives is carried on to the next level tickets = 0; gameObjects = new GameCollection(); setFields(); Car.getTheCar().reset(); gameObjects.add(Car.getTheCar()); //create the monster balls. index 1-startBalls for (int k = 0; k<gameMonsters(); k++) addMonsterBall(); //create the time ticket. index startBalls + 1 addTimeTicket(); elapsed = 0; //setChanged(); //notifyObservers(); } /** * This method is to add a monsterball to the gameworld gameobject collection */ public void addMonsterBall(){ gameObjects.add(new MonsterBall()); setChanged(); notifyObservers(); } private void setFields(){ for (int k = 0; k<98; k++){//left and right FieldSquares border = new FieldSquares(0,(double)k*5+10); border.setOwned(); gameObjects.add(border); border = new FieldSquares(495,(double)k*5+10); border.setOwned(); gameObjects.add(border); } for (int j = 0; j<100; j++){//top and bottom FieldSquares border = new FieldSquares((double)j*5,500); border.setOwned(); gameObjects.add(border); border = new FieldSquares((double)j*5,5); border.setOwned(); gameObjects.add(border); } Random gen = new Random(); for(int i = 0; i < 250; i++){ gameObjects.add(new FieldSquares((double)gen.nextInt(480)+10,(double)gen.nextInt(480)+10)); } } /** * This method returns the value of the minimum score for the game to be one by level. * Each level the minimum score increases by .05 * At level 1, the minimum score is .5 * @return the value of the level's minimum score */ private double levelMinScore() { return .5 +((level-1)*.05); } /** * This method decrements the clock * if time is up the car is reset and lives is decremented. * positions of all movable objects are updated by calling updatePositions() */ @SuppressWarnings("unchecked") public void tock(){ elapsed += 1; if (elapsed == 50){ clock--; elapsed = 0; } if (clock == 0){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).reset(); } } lives--; if(isGameOver()) gameOver(); clock = gameTime(); } updatePositions(time.getDelay()); checkCollisions(); } /** * */ @SuppressWarnings("unchecked") private void checkCollisions(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ ICollider obj = (ICollider) itr.next(); Iterator itr2 = gameObjects.iterator(); while(itr2.hasNext()){ ICollider otherObj = (ICollider) itr2.next(); if ((otherObj != obj) && (otherObj.getClass() != obj.getClass()) ){ if(obj.collidesWith(otherObj)) obj.handleCollision(otherObj,this); } } } setChanged(); notifyObservers(); } /** * This method returns the time value for the given level. * @return calculated game time value */ private int gameTime() { return 30-((level-1)*5); } /** * This method adds the value num to the number of ownedsquares * Also if the added squares results in a win, the appropriate message and action is taken. * @param num */ public void addOwned(int num){ ownedSquares += num; if(checkWon()){ resetGame(++level); if (gameTime()-5 == 0){ JOptionPane.showMessageDialog(null, "Last Level! Boy, you are good", "YOU WON", JOptionPane.INFORMATION_MESSAGE); } else if(gameTime()-5 > 0) JOptionPane.showMessageDialog(null, "Congrats! You have advanced to the next level!", "YOU WON", JOptionPane.INFORMATION_MESSAGE); if (gameTime()==0 && !isGameOver()){ if(JOptionPane.showConfirmDialog(null, "You beat the game! Play again?", "YOU BEAT THE GAME!", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ resetGame(1); } else System.exit(1); } } else{ //Game still in play setChanged(); notifyObservers(); } } /** * This method calculates the percentage of owned squares * @return the percentage of owned squares */ public double calcOwned(){ return (double)(ownedSquares/850.0); } /** * This method adds the time within the TimeTicket to the game clock. */ @SuppressWarnings("unchecked") private void addTime(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof TimeTickets){ clock += ((TimeTickets) n).getTime(); break; } } setChanged(); notifyObservers(); } /** * This method is a getter for the class field tickets * @return tickets */ private int getTickets() { return tickets; } /** * This method adds 1 unit to the current speed of the car. */ @SuppressWarnings("unchecked") public void acellerating(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).setSpeed(5); setChanged(); } } notifyObservers(); } /** * This method decreases the car's speed by 1, but not lower than 0 */ @SuppressWarnings("unchecked") public void braking(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ if(((Car) n).getSpeed() > 0){ ((Car) n).setSpeed(-1); setChanged(); } else JOptionPane.showMessageDialog(null, "Acelleration is already 0. Cannot slow down when stopped.", "Can't do that", JOptionPane.WARNING_MESSAGE); } } notifyObservers(); } /** * This method changes the direction of the car to head North */ @SuppressWarnings("unchecked") public void headNorth(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goNorth(); setChanged(); } } notifyObservers(); } /** * This method changes the direction of the car to head East */ @SuppressWarnings("unchecked") public void headEast(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goEast(); setChanged(); } } notifyObservers(); } /** * This method changes the direction of the car to head South */ @SuppressWarnings("unchecked") public void headSouth(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goSouth(); setChanged(); } } notifyObservers(); } /** * This method changes the direction of the car to head West */ @SuppressWarnings("unchecked") public void headWest(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goWest(); setChanged(); } } notifyObservers(); } /** * This method returns a boolean value based off if the percentage of squares is met */ public boolean checkWon() { return calcOwned() >= minScore; } /** * This getter returns the value of the lives game state data * @return lives */ public int getLives() { return this.lives; } /** * This method ensures that there are TimeTickets before adding the TimeTicket * time and removing it from the gameObject Vector. */ @SuppressWarnings("unchecked") public void checkTickets() { if(getTickets()>0){ addTime(); Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof TimeTickets){ gameObjects.remove(n); setChanged(); break; } } //Removing TimeTicket from world. tickets--; notifyObservers(); } else JOptionPane.showMessageDialog(null, "You do not have any TimeTickets.", "Can't do that", JOptionPane.WARNING_MESSAGE); } public GameCollection getGameObjects() { return this.gameObjects; } @Override public void actionPerformed(ActionEvent e) { TickCommand.getTickCommand(this).actionPerformed(e); } public void addTimeTicket() { gameObjects.add(new TimeTickets(levelTicketTime())); tickets++; setChanged(); notifyObservers(); } }

Share this post


Link to post
Share on other sites
Advertisement
Code is a bit too unstructured to really evaluate properly, especially since your problem is a bit vague and could have many sources. If it was an initialization issue, why does the problem persist for the whole of the first level?

In any case, you need to narrow down the cause of the slowdown. You can do this in a number of ways:
1. Profiling, using a tool like HPROF (or something a bit more user-friendly).
2. You could just measure the time things take by checking the time before they execute, and the time after (System.currentTimeMillis() should have a sufficient resolution for your problem.
3. You could think about the differences between the problematic state (level 1), and the non-problematic state (other levels). And you could rule out the possibility of initialization issues by ensuring that everthing is initialized (probably best done by loading and playing the level without completing it), and then determining whether the problem still exists.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!