Jump to content
  • Advertisement
Sign in to follow this  
Shakedown

Java enum

This topic is 3899 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

I don't understand what the problem is here with my use of enum. GAME_CONSTANTS.java
public interface GAME_CONSTANTS {
	
	// Directions Ships travel in
	enum Direction {NORTH, SOUTH, EAST, WEST};

Ship.java
// Movement methods
	public void move(Direction dir){
		switch(dir){
			case NORTH:
				if((coordinate.getY() - speed) >= 0)
					coordinate.moveY(-speed);
				break;
			case SOUTH:
				if((coordinate.getY() + speed + sprite.getHeight()) <= BACKGROUND_HEIGHT)
					coordinate.moveY(speed);
				break;
			case EAST:
				if((coordinate.getX() + speed + sprite.getWidth()) <= BACKGROUND_WIDTH)
					coordinate.moveX(speed);
				break;
			case WEST:
				if((coordinate.getX() - speed) >= 0)
					coordinate.moveX(-speed);
				break;
		}
	}

Game.java
// Read keyboard input
			if(leftPressed) 	playerShip.move(WEST);
			if(rightPressed)	playerShip.move(EAST);
			if(upPressed)		playerShip.move(NORTH);
			if(downPressed)		playerShip.move(SOUTH);

The error reads: WEST cannot be resolved Yes all the classes implement the GAME_CONSTANTS interface.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Shakedown
Yes all the classes implement the GAME_CONSTANTS interface.

That's just horrible. Does saying "A ship is a game constants" make sense to you?

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Quote:
Original post by Shakedown
Yes all the classes implement the GAME_CONSTANTS interface.

That's just horrible. Does saying "A ship is a game constants" make sense to you?


That's common idiom for Java constants due to lack of namespaces. It saves typing while retaining correct values.

It is not a design, just a coding practice.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
That's common idiom for Java constants due to lack of namespaces. It saves typing while retaining correct values.

It's still hideous... at LEAST call the interfact GameConstantsConsumer, or GameObject or something. There should be some good way to organize your classes so that classes implement the interfaces they actually need and get the constants at the same time.

Furthermore, such abuses of OO constructs are even less excusable now, since static imports have been part of the language for some time.

Share this post


Link to post
Share on other sites
Quote:
Original post by smitty1276
Quote:
Original post by Antheus
That's common idiom for Java constants due to lack of namespaces. It saves typing while retaining correct values.

It's still hideous... at LEAST call the interfact GameConstantsConsumer, or GameObject or something. There should be some good way to organize your classes so that classes implement the interfaces they actually need and get the constants at the same time.

Furthermore, such abuses of OO constructs are even less excusable now, since static imports have been part of the language for some time.


Yes, sadly Java community has too much momentum.

Anyway, back to real problem:
if(leftPressed) 	playerShip.move(Direction.WEST);
if(rightPressed) playerShip.move(Direction.EAST);
if(upPressed) playerShip.move(Direction.NORTH);
if(downPressed) playerShip.move(Direction.SOUTH);


Ironically, your problem stems exactly from the above abuse of interfaces to save typing, something which isn't needed since enum keyword was added.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Anyway, back to real problem:

if(leftPressed) playerShip.move(Direction.WEST);
if(rightPressed) playerShip.move(Direction.EAST);
if(upPressed) playerShip.move(Direction.NORTH);
if(downPressed) playerShip.move(Direction.SOUTH);


Thank you. I thought I tried this, but it works fine now. I looked online in several different places for uses of enums, but somehow none of them used them across multiple files.

Quote:
Original post by greggles
Instead of inheriting from an interface, you should probably use static imports.

Greggles


Is this a common convention to access important globals all throughout your program, as opposed to implementing a constants interface?

Share this post


Link to post
Share on other sites
I declare my constants in a static class with its variables public but final. If things get complicated, get-methods can return related stuff that is just a few conversions away. Hence, I can access it from everywhere without passing around anything.

I have never talked to anyone about this, though. There might be reasons against it that I don't know ...

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
A common convention is to avoid globals like the pest.


That's a worthless response. How do you get around using globals?

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!