Gra invaders wyswietlenie animacji

0

Witam, uczę się dopiero programowania w javie znalazłem w internecie plik pdf opisujący tworzenie gry typu invaders tutaj link:
keyer-pass-programs.googlecode.com/files/invaders.pdf

Kod który mam wygląda następująco:

klasa wojnaswiatow

package invaders;

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;

public class Wojna_Swiatow extends Canvas implements Stage {

    public long usedTime;
    public BufferStrategy strategia;
    private SpriteCache spriteCache;
    private ArrayList actors;

    public Wojna_Swiatow() {
        spriteCache = new SpriteCache();
        JFrame okno = new JFrame(".: Wojna Swiatow :.");
        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();
    }

    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);
        }
    }

    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);
        }
        g.setColor(Color.white);
        if (usedTime > 0) {
            g.drawString(String.valueOf(1000 / usedTime) + " fps", 0, Stage.WYSOKOSC - 50);
        } else {
            g.drawString("--- fps", 0, Stage.WYSOKOSC - 50);
        }
        strategia.show();
    }

    public void updateWorld() {
        for (int i = 0; i < actors.size(); i++) {
            Actor m = (Actor) actors.get(i);
            m.act();
        }
    }

    @Override
    public SpriteCache getSpriteCache() {
        return spriteCache;
    }

    public void game() {
        usedTime = 1000;
        initWorld();

        while (isVisible()) {
            long startTime = System.currentTimeMillis();
            updateWorld();
            paintWorld();
            usedTime = System.currentTimeMillis() - startTime;
            try {
                Thread.sleep(Stage.SZYBKOSC);
            } catch (InterruptedException e) {
            }
        }
    }

    public static void main(String[] args) {
        Wojna_Swiatow inv = new Wojna_Swiatow();
        inv.game();
    }
}

klasa Actor:

 public class Actor {

    protected int x, y;
    protected int width, height;
    protected String spriteName;
    protected Stage stage;
    protected SpriteCache spriteCache;
    
    protected int currentFrame;
    protected String[] spriteNames;

    public Actor(Stage stage) {
    this.stage = stage;
    spriteCache = stage.getSpriteCache();
    currentFrame = 0;
    }

    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 setSpriteNames(String[] names) {
        spriteNames = names;
        height = 0;
        width = 0;
        for (int i = 0; i < names.length; i++ ) {
            BufferedImage image = spriteCache.getSprite(spriteNames[i]);
            height = Math.max(height,image.getHeight());
            width = Math.max(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() {
currentFrame = (currentFrame + 1) % spriteNames.length;
}
}

klasa monster

 public class Monster extends Actor {

    protected int vx;

    public Monster(Stage stage) {
        super(stage);
        setSpriteNames(new String[]{"potworek0.gif","potworek1.gif"});
    }

    public void act() {
        x += vx;
        if (x < 0 || x > 770) {
            vx = -vx;
        }
    }

    public int getVx() {
        return vx;
    }

    public void setVx(int i) {
        vx = i;
    }
}

klasa SpriteCache

 public class SpriteCache {

    public HashMap sprites;

    public SpriteCache() {
        sprites = new HashMap();
    }
    

    public BufferedImage loadImage(String sciezka) {
        URL url = null;
        try {
            url = getClass().getClassLoader().getResource(sciezka);
            return ImageIO.read(url);
        } catch (Exception e) {
            System.out.println("Przy otwieraniu " + sciezka + " jako " + url);
            System.out.println("Wystapil blad : " + e.getClass().getName() + " " + e.getMessage());
            System.exit(0);
            return null;
        }
    }

    public BufferedImage getSprite(String sciezka) {
        BufferedImage img = (BufferedImage) sprites.get(sciezka);
        if (img == null) {
            img = loadImage("img/"+sciezka);
            sprites.put(sciezka, img);
        }
        return img;
    }
}

klasa stage

 public interface Stage extends ImageObserver{
    
    public static final int SZEROKOSC = 800;
    public static final int WYSOKOSC = 600;
    public static final int SZYBKOSC = 60;
    public SpriteCache getSpriteCache();
    
}

Problem zaczyna się w momencie gdy próbuje stworzyć animacje wyświetlanych potworów a mianowicie dostaje błąd:
Przy otwieraniu img/null jako null
Wystapil blad : java.lang.IllegalArgumentException input == null!
Wygląda to tak jak by program nie mógł znaleźć obrazków lecz do momentu gdy wyświetlany był jeden obrazek nic takiego się nie działo, pliki z obrazkami umieszczone są w folderze src/img. Próbowałem już wielu sposobów naprawienia tego błędu lecz nic nie zadziałało. Może ktoś potrafił by wskazać błąd jaki zrobiłem?

0

Pliki graficzne na pewno są w złym katalogu. URL zwracany przez getResource() wskazuje na miejsce gdzie są pliki classs, a nie pliki źródłowe.

0

Czy umieszczę obrazki w katalogu z plikami clas czy w głównym katalogu projektu nic to nie zmienia ciągle jest ten sam błąd.

2

Obejrzałem kod dokładnie, moja rada nie korzystaj z tego kursu. W klasie Actor jest taka metoda:

public void paint(Graphics2D g) 
{
    g.drawImage(spriteCache.getSprite(spriteName), x, y, stage);
}

Ona musi powodować błąd, pole spriteName jest nullem.

0

A może znasz jakiś inny kurs podobny do tego lub sposób aby ominąć wykorzystywanie tej funkcji?

0

Naprawiony kod

public class Actor {
	protected int x,y;
	protected int w,h;
	protected String spriteName;
	protected Stage stage;
	protected SpriteCache spriteCache;
	protected int currentFrame; 
	protected String[] spriteNames;
	protected int frameSpeed;
	protected int t;
	int sP = 1;
	
	public Actor(Stage stage) { 
		this.stage = stage; 
		spriteCache = stage.getSpriteCache(); 
		frameSpeed = 1; 
		t=0;

	}
	public int getFrameSpeed() {return frameSpeed; } 
	public void setFrameSpeed(int i) {frameSpeed = i; }
	public void paint (Graphics2D g){
		g.drawImage(spriteCache.getSprite(spriteNames[currentFrame]),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 getSpriteNames(){ return spriteNames[currentFrame]; }
	
	public void setSpriteNames(String[] names) { 
		spriteNames = names; 
		System.out.print(spriteNames[currentFrame]);
		h = 0; 
		w = 0; 
		for (int i = 0; i < names.length; i++ ) { 
			BufferedImage image = spriteCache.getSprite(spriteNames[i]); 
			h = Math.max(h,image.getHeight()); 
			w = Math.max(w,image.getWidth()); } 
		}
	public int getHeight() {return h;}
	public int getWidth() {return w;}
	public void setHeight(int i) {h = i;}
	public void setWidth(int i) {w = i;}
	public void act(){
		t++;
		if(t% frameSpeed == 0){
			t=0;
			currentFrame = (currentFrame + 1) % 2;
	}
	}
}

0

zamiat 2 w:

if(t% frameSpeed == 0){
			t=0;
			currentFrame = (currentFrame + 1) %2; 

powinno być

if(t% frameSpeed == 0){
			t=0;
			currentFrame = (currentFrame + 1) % spriteNames.length;

1 użytkowników online, w tym zalogowanych: 0, gości: 1