Java enum

Started by
15 comments, last by pinacolada 16 years, 2 months ago
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.
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?
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.
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.

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.
Instead of inheriting from an interface, you should probably use static imports.

Greggles
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?

A common convention is to avoid globals like the pest.
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 ...
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?

This topic is closed to new replies.

Advertisement