Jump to content

  • Log In with Google      Sign In   
  • Create Account

PsychotikRabbit

Member Since 11 Dec 2012
Offline Last Active Dec 11 2014 09:38 AM

Posts I've Made

In Topic: Is this memory leakage?

10 July 2014 - 07:48 AM

Yes you are right you need to call close.

Another approach:
 

public class InputManager {
   private final Scanner scanner;

   public InputManager() {
      scanner = new Scanner(System.in);
   }

   //This should be called when you don't want to read input anymore.
   public void shutdown() {
      scanner.close();
   }

   public String menu(){
      String menuoption = scanner.next();
 
      //Do some stuff...

      return menuoption;
   }
 
   public void info(){
      String info = scanner.next();

      //Do some stuff... 
   }
}

You could then use your class like this:
 

public static void main(String[] args) {
   InputManager inputManager = new InputManager();

   //Do whatever you want with the user input.
   String option = inputManager.menu();
   System.out.println("Option: " + option);

   //Before exiting the application you free resources
   inputManager.shutdown();
}

In Topic: Is this memory leakage?

10 July 2014 - 06:46 AM

Best practice would be to re-use the same scanner instance. If the menu() and info() method must remain static then you can do something like this:
 

private static final Scanner scanner = new Scanner(System.in);

//You should specify either if the method is public or private. 
private static String menu(){
   String menuoption = scanner.next();
 
   //Do some stuff...

   return menuoption;
}
 


//You should specify either if the method is public or private.
private static void info(){
   String info = scanner.next();

   //Do some stuff... 
}

In Topic: Check if objects collide in 2d, without specifying which objects?

22 April 2013 - 01:40 PM

I see, it's just that I read somewhere that (and I semi-quote it) "If you need to use instanceof, it means your code is bad/needs improvement" and many agreed on what that person said. I dunno though how true it is and I don't remember if it was in game-development or general programing (as these are different in my opinion).

What do you think?



I don't think it's bad to use instanceof for this part of the code. Storing a string for the name would be perfect if it would be used as an unique Identifiant. Though,
all your paddles will be name "paddle" and all your walls will be named "wall". I think you must adjust your code with the functionnalities that you are looking for. Therefore, as you don't need 1000 different entities and you have to implement collision detection differently on each of your entity, it is a good way to do it.

In Topic: Check if objects collide in 2d, without specifying which objects?

22 April 2013 - 10:53 AM

It's not a bad way, but you can improve it. Instead of adding an entityName, when you check collision you can verify the type of the obstacle instance:

 

//If there is an obstacle
if(obstacle != null) {
   if(obstacle instanceof Paddle) {
      this.invGoingLeft();
   }
	        
   if(obstacle instanceof Wall) {
      this.invGoingDown();
   }
}

 


Also in the second snippet of code your posted you have two if
 

if(obstacle.entityName == "Wall")

 

and
 

if(obstacle.entityName != "Wall") 

 


Consider using "else" instead of the second condition.
Also if you don't want to get lost in your code and follow standards go read about Indentation at  http://en.wikipedia.org/wiki/Indent_style .

Is this a good way or would this be considered a bad way to check what it collides with?
 
I get that in more advanced games where there are more objects and factors etc this would probably be a bad way.



It is a simple game, I think this collision detection is good for a pong game, though if you start making bigger projects, you might consider more advanced techniques which will fits your needs. As for now it's very good to start somewhere less complicated to get more experience.

In Topic: Check if objects collide in 2d, without specifying which objects?

22 April 2013 - 06:21 AM

Since this is a ball specific collision implementation, you could add in the ball's update method some code to manage it.

 

//ball update method
public void update() {
    //Check collision
    Entity obstacle = CollisionDetection.collidesWith(this);

    //If there is an obstacle
    if(obstacle != null) {
        if(obstacle.getX() < getX()) {
            //Bouce Right;
        } else {
            //Bounce Left;
        }
        
        if(obstacle.getY() < getY() {
            //Bounce Bottom
        } else {
            //Bounce Top
        }
    }
}

 


Of course you can adapt this code to your needs.


PARTNERS