jackmaverick1 -4 Junior Poster

I've made a little game (not really, just a bit of a start), but the collision detection is a little screwy. It works within 2 pixels for the bottom three sides. However, on the top, there is a buffer of about 20 pixels that it counts as part of the entity.

I've posted the most relevant parts:

//Player.java
package game;

public class Player extends Entity {

	public Player(int x, int y) {
		super(100, x, y, 50, 100, "player.png", null);
	}

	public void move(Entity ref) {
		int speed = 5;
		if (Game.up && !Game.down && getY() > 0) {
			setY(getY() - speed);
		}
		if (Game.down && !Game.up && getY() < 670) {
			setY(getY() + speed);
		}
		if (Game.left && !Game.right && getX() > 0) {
			setX(getX() - speed);
		}
		if (Game.right && !Game.left && getX() < 750) {
			setX(getX() + speed);
		}
	}
}
//Entity.java
package game;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;

public class Entity {
	public int type;

	Rectangle rect = new Rectangle();
	private int x, y, width, height;
	Image img;
	final Image orig;
	protected int health;
	final protected int maximumHealth;
	boolean drawMe = true;

	public Entity(int maxHealth, int x, int y, int wid, int hei,
			String pathToImage, String[] extras) {
		maximumHealth = maxHealth;
		health = maxHealth;
		setX(x);
		setY(y);
		setWidth(wid);
		setHeight(hei);
		img = new ImageIcon(pathToImage).getImage();
		orig = img;
		rect = new Rectangle(x, y, wid, hei);
	}

	// draws the current image
	public void draw(Graphics g) {
		if (drawMe)
			g.drawImage(img, x, y, null);
	}

	// Switch to the image that is specified -1 = reset
	public void switchImage(int index) {
		if (!(index > 0 && index < extras.length))
			return;
		if (index == -1) {
			img = orig;
			return;
		}
		try {
			img = extras[index];
		} catch (ArrayIndexOutOfBoundsException e) {

		}
	}

	public boolean checkWith(Entity other) {
		return rect.intersects(other.rect);
	}

	public void OnCollision(Entity e) {
		
	}

	public void setX(int x) {
		this.x = x;
		rect.x = x;
	}

	public int getX() {
		return x;
	}

	public void setY(int y) {
		this.y = y;
		rect.y = y;
	}

	public int getY() {
		return y;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getWidth() {
		return width;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getHeight() {
		return height;
	}

	Image extras[];
	//
	public void move(Entity ref) {
	}

	public void setType(int i) {
		this.type = i;
	}

	public int getType() {
		return type;
	}
}
//SMB_Entity.java
package game;

public class SMB_Entity extends Gift {

	public SMB_Entity(int x, int y) {
		super(100, x, y, 48, 48, "enemy.png", null, 1);
		// TODO Auto-generated constructor stub
	}
}

Some of the classes are very simplistic, as they are just extensions of other classes and only provide more appropriate constructors for a given class.
A few notes:

  • Gift is a sub class of Entity
  • Enemy is a sub-class of Entity
  • Player is a sub-class of Entity

P.S. Could you comment on my image switching idea? It's an animation tool. It's the best way I could think to implement it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.