Gå til innhold

Noen små problemer med JGame og kollisjoner


Anbefalte innlegg

Jeg skjønner ikke hva jeg kan ha gjort feil, jeg har 3 JGObject'er Player, Enemy og Bullet. Jeg vil at når Bullet treffer Enemy så skal Enemy og Bullet bli borte, men problemet er da at hit() blir aldri registrert på Enemy.

 

Litt kode:

Enemy:

 

 

public class Enemy extends JGObject {
	/** Constructor. */
	Enemy (double x, double y) {
		// Initialise game object by calling an appropriate constructor
		// in the JGObject class.
		super(
			"Enemy",// name by which the object is known
			true,//true means add a unique ID number after the object name.
			     //If we don't do this, this object will replace any object
			     //with the same name.
			x,  // X position
			y, // Y position
			2, // the object's collision ID (used to determine which classes
			   // of objects should collide with each other)
			null // name of sprite or animation to use (null is none)
		);
		// Give the object an initial speed in a random direction.
		xspeed = 0;//random(-2,2);
		yspeed = 3;//random(-2,2);
	}

	/** Update the object. This method is called by moveObjects. */
	public void move() {

	}

	/** Draw the object. */
	public void paint() {
		// Draw a yellow ball
		setColor(JGColor.blue);
		drawOval(x,y,16,16,true,true);
	}

       public void KeyDown(KeyEvent evt) {

       }

       @Override
       public void hit_bg(int id) {
           System.out.println("Got hit!");
       }

       @Override
       public void hit(JGObject obj) {
           System.out.println("Got hit!");
           remove();
           obj.remove();
       }
}

 

 

Bullet:

 

 

public class Bullet extends JGObject {

	/** Constructor. */
	Bullet (double x, double y) {
		// Initialise game object by calling an appropriate constructor
		// in the JGObject class.
		super(
			"bullet",// name by which the object is known
			true,//true means add a unique ID number after the object name.
			     //If we don't do this, this object will replace any object
			     //with the same name.
			x,  // X position
			y, // Y position
			3, // the object's collision ID (used to determine which classes
			   // of objects should collide with each other)
			null // name of sprite or animation to use (null is none)
		);
		// Give the object an initial speed in a random direction.
		xspeed = 0;//random(-2,2);
		yspeed = -5;//random(-2,2);
	}

	/** Update the object. This method is called by moveObjects. */
	public void move() {

	}

	/** Draw the object. */
	public void paint() {
		// Draw a yellow ball
		setColor(JGColor.red);
		drawOval(x,y,6,6,true,true);
	}

       @Override
       public void hit(JGObject obj) {
           System.out.println("Got hit!");
       }

       @Override
       public void hit_bg(int id) {
           System.out.println("Got hit!");
       }

}

 

 

Noe av hovedkoden:

 

 

 
public void doFrame() {
       checkCollision(3, 2);

       ...
}

 

 

Lenke til kommentar
Videoannonse
Annonse

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...