getBounds() i instanceof - brak błędu i brak kolizji obiektów

0

Hej!
Kompletnie nie wiem, czemu program nie wykrywa kolizji obiektów "Monster" i "Bullet". Nie otrzymuję żadnego błędu i cały czas kombinuję gdzie jest błąd w tutorialu: czyżby złe umiejscowienie "checkCollisions();"?

Gra.java:

import java.awt.Canvas;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;

	public class Gra extends Canvas implements Stage,KeyListener {
		
		
		public long usedTime;
		public BufferStrategy strategia;
		private SpriteCache spriteCache;
		private ArrayList actors;
		private Player player;
		
	
		public Gra() {
			spriteCache = new SpriteCache();
			JFrame okno = new JFrame(".: Arkanoid-Invaders :.");
			JPanel panel = (JPanel)okno. getContentPane();
			setBounds(0,0,Stage.SZEROKOSC,Stage.WYSOKOSC);
			panel. setPreferredSize(new Dimension(Stage.SZEROKOSC,Stage.WYSOKOSC));
			panel. setLayout(null);
			panel. add(this);
			okno. setBounds(0,0,Stage.SZEROKOSC,Stage.WYSOKOSC);
			okno. setVisible(true);
			okno. addWindowListener( new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					System. exit(0);
				}
			});
			
			okno. setResizable(false);
			createBufferStrategy(2);
			strategia = getBufferStrategy();
			requestFocus();
			addKeyListener(this);
			
		}
		//przyciski!
		public void keyPressed(KeyEvent e) {
			player. keyPressed(e);
			}
			public void keyReleased(KeyEvent e) {
			player. keyReleased(e);
			}
			public void keyTyped(KeyEvent e) {}
			//przyciski
			
		public void initWorld() {
			actors = new ArrayList();
			for (int i = 0; i < 10; i++){
				Monster m = new Monster(this);
				m. setX( (int)(Math. random()*Stage.SZEROKOSC) );
				m. setY( i*20 );
				m. setVx( (int)(Math. random()*3)+1 );
				actors. add(m);
			}
			
			player = new Player(this);
			player. setX(Stage.SZEROKOSC/2);
			player. setY(Stage.WYSOKOSC - 2*player.getHeight());
			
			
		}
		
		public void paintWorld() {
			Graphics2D g = (Graphics2D)strategia. getDrawGraphics();
			g. setColor(Color.black);
			g. fillRect(0,0, getWidth(), getHeight());
			for (int i = 0; i < actors. size(); i++) {
				Actor m = (Actor)actors. get(i);
				m. paint(g);
			}
					player.paint(g);
			
			g. setColor(Color.white);
			if (usedTime > 0)
				g. drawString(String. valueOf(1000/usedTime)+" fps",0,Stage.WYSOKOSC-550);
			else
				g. drawString("--- fps",0,Stage.WYSOKOSC-550);
			strategia.show();
		}
		
		public void addActor(Actor a) {
			actors.add(a);
		}
		
		public void updateWorld() {
			int i=0;
		while (i < actors.size()) {
			Actor m = (Actor)actors.get(i);
			if (m.isMarkedForRemoval()) {
				actors.remove(i);
			} else {
				m.act();
				i++;
			}
			}
			player.act();
		}
		
		public SpriteCache getSpriteCache() {
			return spriteCache;
		}
		
		//Stwierdzanie KOLIZJI
		public void checkCollisions() {
				Rectangle playerBounds = player. getBounds();
				for (int i = 0; i < actors. size(); i++) {
					Actor a1 = (Actor)actors. get(i);
					Rectangle r1 = a1. getBounds();
					if (r1. intersects(playerBounds)) {
						player. collision(a1);
						a1. collision(player);
					}
					for (int j = i+1; j < actors.size(); j++) {
						Actor a2 = (Actor)actors. get(j);
						Rectangle r2 = a2. getBounds();
						if (r1. intersects(r2)) {
							a1. collision(a2);
							a2. collision(a1);
						}
					}
				}
			}//koniec kolizji
		
		public void game() {
			usedTime=1000;
			initWorld();
			while (isVisible()) {
				long startTime = System. currentTimeMillis();
				updateWorld();
				
				paintWorld();
				checkCollisions();
				try {
					Thread. sleep(20);
					usedTime = System. currentTimeMillis()-startTime;
				} catch (InterruptedException e) {}
			}
			}
		
		public static void main(String[] args) {
			Gra inv = new Gra();
			inv. game();
			}
		
	
		
	}

Monster.java:

	public class Monster extends Actor {
		protected int vx,vy;
		
		public Monster(Stage stage) {
			super(stage);
			setSpriteName("potworek.gif");
		}
		//kolizja
		public void collison(Actor a) {
			if (a instanceof Bullet)
				remove();
		}//kolizja
		public void act() {
			x+=vx;
			y+=vy;
			if (x < 0 || x > Stage.SZEROKOSC)
			vx = -vx;
			if (y < 0 || y > Stage.WYSOKOSC)
			vy = -vy;
			}
			public int getVx() { return vx; }
			public void setVx(int i) {vx = i; }
			public int getVy() { return vy; }
			public void setVy(int i) {vy = i; }
		
			
	}

Actor.java:

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;

	public class Actor {
		protected int x,y;
		protected int width, height;
		protected String spriteName;
		protected Stage stage;
		protected SpriteCache spriteCache;
		protected boolean markedForRemoval;
		
		
		public Actor(Stage stage) {
			this.stage = stage;
			spriteCache = stage.getSpriteCache();
		}
		
		public void paint(Graphics2D g){
			g. drawImage( spriteCache.getSprite(spriteName), x,y, stage );
		}
		
		public int getX() { return x; }
		public void setX(int i) { x = i; }
		public int getY() { return y; }
		public void setY(int i) { y = i; }
		public String getSpriteName() { return spriteName; }
		
		public void setSpriteName(String string) {
			spriteName = string;
			BufferedImage image = spriteCache.getSprite(spriteName);
			height = image.getHeight();
			width = image.getWidth();
		}
		
		public int getHeight() { return height; }
		public int getWidth() { return width; }
		public void setHeight(int i) {height = i; }
		public void setWidth(int i) { width = i; }
		public void act() { }
		
		//kolizja
		public Rectangle getBounds() {
			return new Rectangle(x,y,width,height);
		}
		
		public void collision(Actor a){}
		
		
		public void remove() {
			markedForRemoval=true;
		}
		public boolean isMarkedForRemoval() {
			return markedForRemoval;
		}
		
		//KOLIZJA
	
		
}

Bullet.java:

import java.awt.Rectangle;

public class Bullet extends Actor {
	protected static final int BULLET_SPEED=10;
	public Bullet(Stage stage) {
		super(stage);
		setSpriteName("bullet.gif");
	}
	public void act() {
		super.act();
		y-=BULLET_SPEED;
		if (y < 0)
			remove();
		}
	
	
	}

I jeszcze Player.java

import java.awt.event.KeyEvent;
public class Player extends Actor {
		protected static final int PLAYER_SPEED = 4;
		protected int vx;
		protected int vy;
		private boolean left,right;
		
		public Player(Stage stage) {
			super(stage);
			setSpriteName("gracz.gif");
		}
		
		public void act() {
			super.act();
			x+=vx;
		//	y+=vy;
			if (x < 0 || x > Stage.SZEROKOSC)
				vx = -vx;
			//if (y < 0 || y > Stage.WYSOKOSC)
			//	vy = -vy;
		}
		
		public int getVx() { return vx; }
		public void setVx(int i) {vx = i; }
	//	public int getVy() { return vy; }
	//	public void setVy(int i) {vy = i; }
		
		protected void updateSpeed() {
			vx=0;vy=0;
			if (left) vx = -PLAYER_SPEED;
			if (right) vx =PLAYER_SPEED;
		}
		public void keyReleased(KeyEvent e) {
			switch (e.getKeyCode()) {
			case KeyEvent.VK_LEFT : left = false; break;
			case KeyEvent.VK_RIGHT : right = false; break;
			}
			updateSpeed();
	}
		
		public void fire() {
			Bullet b = new Bullet(stage);
			b.setX(x);
			b.setY(y - b.getHeight());
			stage.addActor(b);
		}
		
		public void keyPressed(KeyEvent e) {
			switch (e. getKeyCode()) {
			case KeyEvent.VK_LEFT : left = true; break;
			case KeyEvent.VK_RIGHT : right = true; break;
			case KeyEvent.VK_SPACE : fire(); break;
			
		
			}
			updateSpeed();
			}
			}

Czyżby czegoś brakowało w klasie Bullet? Może użyć innej metody, niż instanceof? Siedzę nad tym już dłuższy czas i obawiam się, że błąd jest banalny.

1

Gdyby kogoś interesowało rozwiązanie:

w klasie Monster.java była literówka:

	//kolizja
		public void collision(Actor a) {
			if (a instanceof Bullet)
				remove();
		}//kolizja
		

collison zamiast collision...

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.