控制器类:
package com.game.src.main;
import java.awt.Graphics;
import java.util.LinkedList;
import java.util.Random;
public class Controller
{
private LinkedList<Enemy> e = new LinkedList<Enemy>();
Random r = new Random();
Enemy TempEnemy = null;
Game game;
Textures tex;
public Controller(Game game, Textures tex)
{
this.game = game;
this.tex = tex;
**for(int x = 0; x < (Game.WIDTH * Game.SCALE); x += 21)
addEnemy(new Enemy(x, 200, tex));**
}
public void tick()
{
for(int i = 0; i < e.size(); i++)
TempEnemy = e.get(i);
TempEnemy.tick();
}
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
TempEnemy = e.get(i);
TempEnemy.render(g);
}
public void addEnemy(Enemy block)
{
e.add(block);
}
public void removeEnemy(Enemy block)
{
e.remove(block);
}
}
构造函数中的for-Loop不会创建一行敌人。它应该做什么,它应该渲染一排敌人的精灵,而是渲染最后一个在屏幕的最右边。 :\ Enemy Class如下:
package com.game.src.main;
import java.awt.Graphics;
public class Enemy
{
private int x = 0;
private int y = 0;
private Textures tex = null;
public Enemy(int x, int y, Textures tex)
{
this.x = x;
this.y = y;
this.tex = tex;
}
public void tick()
{
y+= 3;
if(y >= (Game.HEIGHT * Game.SCALE))
y = 0;
}
public void render(Graphics g)
{
g.drawImage(tex.spike, x, y, null);
}
}
纹理类:
package com.game.src.main;
import java.awt.image.BufferedImage;
public class Textures
{
public BufferedImage player, spike;
private SpriteSheet ss;
public Textures(Game game)
{
ss = new SpriteSheet(game.getSpriteSheet());
getTextures();
}
private void getTextures()
{
player = ss.grabImage(1, 1, 44, 65);
spike = ss.grabImage(1, 3, 21, 65);
}
public void setUp()
{
player = ss.grabImage(3, 1, 43, 65);
}
public void setDown()
{
player = ss.grabImage(1, 1, 43, 65);
}
public void setLeft()
{
player = ss.grabImage(5, 1, 43, 65);
}
public void setRight()
{
player = ss.grabImage(7, 1, 43, 65);
}
}
答案 0 :(得分:2)
BRACKETS!
更改此
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++)
TempEnemy = e.get(i);
TempEnemy.render(g);
}
到这个
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++){
TempEnemy = e.get(i);
TempEnemy.render(g);
}
}
PS:即使你只需要执行一行,也要为循环,if语句等编写括号。你避免这种挣扎。
此外,您不需要&#34; TempEnemy&#34;,此代码也是如此:
public void render(Graphics g)
{
for(int i = 0; i < e.size(); i++){
e.get(i).render(g);
}
}