将不同的对象添加到不同级别的对象数组列表仅适用于一个级别 [Slick2D]
Adding Different Objects To An ArrayList of Objects For Different Levels Only Working For One Level [Slick2D]
我正在创建一个游戏,其中每个级别都有不同的 Enemies
,它们是圆形。对于 init
方法中 Play
class 的第一级,我调用 class 将圆圈添加到 ArrayList
,我就这样做了一旦 Player
到达关卡的末端,一个名为 level
的变量增加 1,Player
重生,ArrayList
清除并调用圆圈添加 class 具有更新的级别变量。
由于某种原因,添加新圈子后程序关闭。我已经尝试使用它,以便在 init 方法中我有 if 语句来确定 level
的值,但我找不到多次调用 init
方法的方法。我尝试过其他方法,但似乎没有任何效果。这个添加圈子的方法是我在向你们寻求帮助之前的最后一次尝试。
这是我的 Play
Class:
package tricky.game;
import java.util.*;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.geom.*;
public class Play extends BasicGameState{
public Shape border,bg,sZone,eZone;
public Player p;
public Options o;
public int level = 1;
public addCircles a;
public Play(int play){
}
@Override
public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
sZone = new Rectangle(280,180,100,100);
eZone = new Rectangle(820,520,100,100);
border = new Rectangle(380,180,440,440);
bg= new Rectangle(0,0,1200,800);
a = new addCircles();
p = new Player();
p.init(container, sbg);
o = new Options();
a.setLevel(level);
a.setCircles();
}
@Override
public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {
Input input = container.getInput();
g.setColor(Color.white);
g.fill(bg);
g.setColor(Color.black);
g.drawString("X: "+input.getMouseX()+"\t Y: "+input.getMouseY(), 50, 50);
g.setColor(Color.lightGray);
g.fill(border);
g.fill(sZone);
g.fill(eZone);
g.setLineWidth(3f);
g.setColor(Color.black);
g.draw(border);
g.draw(sZone);
g.draw(eZone);
g.resetLineWidth();
g.setColor(Color.red);
g.drawString("START",302,225);
g.drawString("END",854,564);
p.render(container, sbg, g);
o.render(container, sbg, g);
g.drawString("Level:"+level,540,135);
g.drawString("ArrayList size:"+a.circle.size(),540,100);
for (int i = 0; i < a.circle.size(); i++) {
a.circle.get(i).render(container, sbg, g);
}
}
@Override
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
Input input = container.getInput();
p.update(container, sbg, delta);
o.update(container, sbg, delta);
for (int i = 0; i < a.circle.size(); i++) {
a.circle.get(i).update(container, sbg, delta);
if(p.getHB().intersects(a.circle.get(i).getCircle())){
p.respawn();
}
}
if(p.getHB().intersects(eZone)){
level++;
a.circle.clear();
p.respawn();
a.setLevel(level);
a.setCircles();
}
if(p.getHB().intersects(border) && !(p.getHB().intersects(sZone))){
p.setBorders(381,180,820,620);
}
if(p.getHB().intersects(sZone) && !(p.getHB().intersects(border))){
p.setBorders(280,180,820,280);
}
if(p.getHB().intersects(sZone) && (p.getHB().intersects(border))){
p.setBorders(380,180,820,280);
}
}
@Override
public int getID() {
return 2;
}
}
这是我的Enemies
Class
package tricky.game;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.geom.*;
public class Enemies {
private float x,y,maxX,maxY,minX,minY;
public boolean dir=false;
private Shape circle;
public int type,radius;
public Enemies(int x,int y,int maxX,int minX,int maxY,int minY,int type,int radius,boolean dir){
this.x=x;
this.y=y;
this.maxX=maxX;
this.minX=minX;
this.maxY=maxY;
this.minY=minY;
this.type=type;
this.radius=radius;
this.dir=dir;
}
public void init(GameContainer container, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException{
g.setColor(Color.cyan);
g.fill(circle);
g.setLineWidth(1.5f);
g.setColor(Color.black);
g.draw(circle);
g.resetLineWidth();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
circle = new Circle(x, y, radius);
if(type==1){
if (!dir) {
x+=4;
}
if (x>=maxX) {
dir=true;
}
if (dir) {
x-=4;
}
if (x<=minX) {
dir=false;
}
}
if(type==2){
if (!dir) {
y+=4;
}
if (y>=maxY) {
dir=true;
}
if (dir) {
y-=4;
}
if (y<=minY) {
dir=false;
}
}
if(type==3){
if(!dir){
y+=4;
x+=4;
}
if(y>=maxY-20 && x>=maxX-20){
dir=true;
}
if(dir){
y-=4;
x-=4;
}
if(y<=minY+20 && x<=minX+20){
dir=false;
}
}
}
public Shape getCircle(){
return circle;
}
}
这是我的 addCircles
class:
package tricky.game;
import java.util.*;
public class addCircles {
public ArrayList<Enemies> circle = new ArrayList();
public int level;
public void setLevel(int level){
this.level = level;
}
public void setCircles(){
if(level==1){
circle.add(new Enemies(420,220,780,420,0,0,1,20,false));
circle.add(new Enemies(780,580,780,420,0,0,1,20,true));
circle.add(new Enemies(600,260,0,0,540,260,2,20,false));
}
if(level==2){
circle.add(new Enemies(600,400,820,380,620,180,3,20,false));
}
}
}
编辑:我所说的关闭是指它给我以下错误:
Mon Jan 15 08:40:03 EST 2018 ERROR:null
java.lang.NullPointerException
at org.newdawn.slick.geom.ShapeRenderer.validFill(ShapeRenderer.java:94)
at org.newdawn.slick.geom.ShapeRenderer.fill(ShapeRenderer.java:107)
at org.newdawn.slick.Graphics.fill(Graphics.java:554)
at tricky.game.Enemies.render(Enemies.java:29)
at tricky.game.Play.render(Play.java:63)
at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at tricky.game.TrickyGame.main(TrickyGame.java:32)
Mon Jan 15 08:40:03 EST 2018 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at tricky.game.TrickyGame.main(TrickyGame.java:32)
BUILD SUCCESSFUL (total time: 9 seconds)
编辑 2:我发现将新敌人渲染到屏幕上的操作是破坏程序的原因。我完成了第一个级别,添加了新的 Enemies 对象并且它起作用了,只是出于某种原因程序不想渲染它。
编辑 3:我修复了所有问题。这是我渲染对象的方式。我之前使用 g.draw()
现在我使用 g.fillOval()
我已经解决了我遇到的问题。由于某些原因,在使用 g.draw()
函数进行渲染时,程序不想渲染它。我现在正在使用 g.drawOval() function
并且一切正常。
我正在创建一个游戏,其中每个级别都有不同的 Enemies
,它们是圆形。对于 init
方法中 Play
class 的第一级,我调用 class 将圆圈添加到 ArrayList
,我就这样做了一旦 Player
到达关卡的末端,一个名为 level
的变量增加 1,Player
重生,ArrayList
清除并调用圆圈添加 class 具有更新的级别变量。
由于某种原因,添加新圈子后程序关闭。我已经尝试使用它,以便在 init 方法中我有 if 语句来确定 level
的值,但我找不到多次调用 init
方法的方法。我尝试过其他方法,但似乎没有任何效果。这个添加圈子的方法是我在向你们寻求帮助之前的最后一次尝试。
这是我的 Play
Class:
package tricky.game;
import java.util.*;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.geom.*;
public class Play extends BasicGameState{
public Shape border,bg,sZone,eZone;
public Player p;
public Options o;
public int level = 1;
public addCircles a;
public Play(int play){
}
@Override
public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
sZone = new Rectangle(280,180,100,100);
eZone = new Rectangle(820,520,100,100);
border = new Rectangle(380,180,440,440);
bg= new Rectangle(0,0,1200,800);
a = new addCircles();
p = new Player();
p.init(container, sbg);
o = new Options();
a.setLevel(level);
a.setCircles();
}
@Override
public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException {
Input input = container.getInput();
g.setColor(Color.white);
g.fill(bg);
g.setColor(Color.black);
g.drawString("X: "+input.getMouseX()+"\t Y: "+input.getMouseY(), 50, 50);
g.setColor(Color.lightGray);
g.fill(border);
g.fill(sZone);
g.fill(eZone);
g.setLineWidth(3f);
g.setColor(Color.black);
g.draw(border);
g.draw(sZone);
g.draw(eZone);
g.resetLineWidth();
g.setColor(Color.red);
g.drawString("START",302,225);
g.drawString("END",854,564);
p.render(container, sbg, g);
o.render(container, sbg, g);
g.drawString("Level:"+level,540,135);
g.drawString("ArrayList size:"+a.circle.size(),540,100);
for (int i = 0; i < a.circle.size(); i++) {
a.circle.get(i).render(container, sbg, g);
}
}
@Override
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
Input input = container.getInput();
p.update(container, sbg, delta);
o.update(container, sbg, delta);
for (int i = 0; i < a.circle.size(); i++) {
a.circle.get(i).update(container, sbg, delta);
if(p.getHB().intersects(a.circle.get(i).getCircle())){
p.respawn();
}
}
if(p.getHB().intersects(eZone)){
level++;
a.circle.clear();
p.respawn();
a.setLevel(level);
a.setCircles();
}
if(p.getHB().intersects(border) && !(p.getHB().intersects(sZone))){
p.setBorders(381,180,820,620);
}
if(p.getHB().intersects(sZone) && !(p.getHB().intersects(border))){
p.setBorders(280,180,820,280);
}
if(p.getHB().intersects(sZone) && (p.getHB().intersects(border))){
p.setBorders(380,180,820,280);
}
}
@Override
public int getID() {
return 2;
}
}
这是我的Enemies
Class
package tricky.game;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.geom.*;
public class Enemies {
private float x,y,maxX,maxY,minX,minY;
public boolean dir=false;
private Shape circle;
public int type,radius;
public Enemies(int x,int y,int maxX,int minX,int maxY,int minY,int type,int radius,boolean dir){
this.x=x;
this.y=y;
this.maxX=maxX;
this.minX=minX;
this.maxY=maxY;
this.minY=minY;
this.type=type;
this.radius=radius;
this.dir=dir;
}
public void init(GameContainer container, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer container, StateBasedGame sbg, Graphics g) throws SlickException{
g.setColor(Color.cyan);
g.fill(circle);
g.setLineWidth(1.5f);
g.setColor(Color.black);
g.draw(circle);
g.resetLineWidth();
}
public void update(GameContainer container, StateBasedGame sbg, int delta) throws SlickException {
circle = new Circle(x, y, radius);
if(type==1){
if (!dir) {
x+=4;
}
if (x>=maxX) {
dir=true;
}
if (dir) {
x-=4;
}
if (x<=minX) {
dir=false;
}
}
if(type==2){
if (!dir) {
y+=4;
}
if (y>=maxY) {
dir=true;
}
if (dir) {
y-=4;
}
if (y<=minY) {
dir=false;
}
}
if(type==3){
if(!dir){
y+=4;
x+=4;
}
if(y>=maxY-20 && x>=maxX-20){
dir=true;
}
if(dir){
y-=4;
x-=4;
}
if(y<=minY+20 && x<=minX+20){
dir=false;
}
}
}
public Shape getCircle(){
return circle;
}
}
这是我的 addCircles
class:
package tricky.game;
import java.util.*;
public class addCircles {
public ArrayList<Enemies> circle = new ArrayList();
public int level;
public void setLevel(int level){
this.level = level;
}
public void setCircles(){
if(level==1){
circle.add(new Enemies(420,220,780,420,0,0,1,20,false));
circle.add(new Enemies(780,580,780,420,0,0,1,20,true));
circle.add(new Enemies(600,260,0,0,540,260,2,20,false));
}
if(level==2){
circle.add(new Enemies(600,400,820,380,620,180,3,20,false));
}
}
}
编辑:我所说的关闭是指它给我以下错误:
Mon Jan 15 08:40:03 EST 2018 ERROR:null
java.lang.NullPointerException
at org.newdawn.slick.geom.ShapeRenderer.validFill(ShapeRenderer.java:94)
at org.newdawn.slick.geom.ShapeRenderer.fill(ShapeRenderer.java:107)
at org.newdawn.slick.Graphics.fill(Graphics.java:554)
at tricky.game.Enemies.render(Enemies.java:29)
at tricky.game.Play.render(Play.java:63)
at org.newdawn.slick.state.StateBasedGame.render(StateBasedGame.java:199)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:688)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at tricky.game.TrickyGame.main(TrickyGame.java:32)
Mon Jan 15 08:40:03 EST 2018 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:691)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:411)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:321)
at tricky.game.TrickyGame.main(TrickyGame.java:32)
BUILD SUCCESSFUL (total time: 9 seconds)
编辑 2:我发现将新敌人渲染到屏幕上的操作是破坏程序的原因。我完成了第一个级别,添加了新的 Enemies 对象并且它起作用了,只是出于某种原因程序不想渲染它。
编辑 3:我修复了所有问题。这是我渲染对象的方式。我之前使用 g.draw()
现在我使用 g.fillOval()
我已经解决了我遇到的问题。由于某些原因,在使用 g.draw()
函数进行渲染时,程序不想渲染它。我现在正在使用 g.drawOval() function
并且一切正常。