I am making a game in Slick2D for Java, but when I tried to display an Image(the Slick2D type) it gives me this error:
java.lang.NullPointerException
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1384)
at org.newdawn.slick.Graphics.drawImage(Graphics.java:1433)
at luke_r.games.java.broadway.states.M.render(M.java:21)
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 luke_r.games.java.broadway.C.main(C.java:21)
Here is the for the M class (excluding imports and class declaration, they are unneeded):
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
PL.x = 100;
PL.y = 100;
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawImage(I.title, 0, 0); //Line 21, the error
PL.render(g);
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{}
#Override
public int getID()
{
return 0;
}
The problem is that I.title == null. Set it to a correct value.
In init function set the url to the image into I.title, i mean => I.title = new Image("-your URL-");
Slick2D works in this form: first of all he is init all params in method init , then until somthing happend that change the current state "he" render => update => render => update =>render => update ... You can set how much the update will occurs in the SetUpClass by using SetTargetFrameRate method.
Related
I've just started playing with slick2d's state based game structure. I've set up the main class as follows
public static final int newMenu = 0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
AppGameContainer appgc;
try {
appgc = new AppGameContainer(new StateBased("StateBased Test"));
appgc.setDisplayMode(640, 360, false);
appgc.start();
} catch (SlickException e) {
}
}
public StateBased(String name) throws SlickException {
super(name);
this.addState(new NewMenu(newMenu));
}
#Override
public void initStatesList(GameContainer gc) throws SlickException {
this.getState(newMenu).init(gc, this);
}
now the new menu class so far has
private final int state;
private final Image bg;
private final static String directory = "Images/StartScreen/";
public NewMenu(int state) throws SlickException {
this.state = state;
bg = new Image(directory + "loadbg.png");
}
but i'm getting at error at the "bg=new image" line. "No OpenGL context found in the current thread." I'm confused since I haven't got this when using basicgame instead of statebasedgame
Can anyone help me solve this
This is the full error
Exception in thread "main" java.lang.RuntimeException: No OpenGL
context found in the current thread. at
org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124) at
org.lwjgl.opengl.GL11.glGetError(GL11.java:1299) at
org.newdawn.slick.opengl.renderer.ImmediateModeOGLRenderer.glGetError(ImmediateModeOGLRenderer.java:384)
at
org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:249)
at
org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:187)
at org.newdawn.slick.Image.(Image.java:192) at
org.newdawn.slick.Image.(Image.java:166) at
org.newdawn.slick.Image.(Image.java:154) at
org.newdawn.slick.Image.(Image.java:132) at
statebased.NewMenu.(NewMenu.java:23) at
statebased.StateBased.(StateBased.java:36)
at
statebased.StateBased.main(StateBased.java:27)
Fixed by moving the code from the constructor to the init method
I've implemented some pathfinding in my 2d game, but I don't know how to move my 'Zombie' character.
I'm using slick2d and everything gets put on the screen in the render method, however I want to update the zombies movement in the Zombie class.
Here is the method in the zombie class:
public void findPrey(){
Pathfinder pf = new Pathfinder();
pf.setStartNode(xPosition,yPosition);
for(PathCoordinates p: pf.calculatePath()){
if(p.getXPosition() > xPosition){
moveRight(2);
}if(p.getXPosition() < xPosition){
moveLeft(2);
}if(p.getYPosition() > yPosition){
moveUp(2);
}if(p.getYPosition() < yPosition){
moveDown(2);
}
}
}
So I want to loop through the List of x and y coordinates and move the zombie.
However nothings happens- the zombie doesnt move at all.
In my 'world' class I have the render method, which loops through the zombies and renders them, the code has been edited slightly:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
for (Zombie z : zombies) {
if (!z.isDead()) {
z.render(gc, sbg, g);
z.findPrey();
}
}
}
So, basically my question is how - do I make my zombie character move?! Thanks for any help or guidance
You should first move the zombie, then render it.
There's another problem: you allow to move the zombie all the way it should with one findPrey() (using updatePosition() in code below) method call.
Zombie class:
public void updatePosition() {
Pathfinder pf = new Pathfinder();
pf.setStartNode(xPosition,yPosition);
PathCoordinates p = pf.calculatePath()[0];
if(p.getXPosition() > xPosition) {
moveRight(2);
} if(p.getXPosition() < xPosition) {
moveLeft(2);
} if(p.getYPosition() > yPosition) {
moveUp(2);
} if(p.getYPosition() < yPosition) {
moveDown(2);
}
}
World class:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
for(Zombie z : zombies) {
if(!(z.isDead())) {
z.updatePosition();
z.render(gc, sbg, g);
}
}
}
I've Googled a lot and could not find the answer to this. I know how to load an image, I need to load images that aren't 256 by 256 or a direct power of 2.
Like how could I load an image that's 128 by 384 or something like that.
What I'm using now:
Load an image:
public static Texture cow = loadTexture("res/cow.png");
private static Texture loadTexture(String file){
try {
return TextureLoader.getTexture("JPG", new FileInputStream(new File(file)));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Now to draw it on a 3D poly face:
txt.bind();
GL11.glBegin(GL11.GL_QUADS);
{
//GL11.glColor3d(lightLevel, lightLevel, lightLevel);
GL11.glColor3d(l, l, l);
GL11.glTexCoord2f(0,0); GL11.glVertex3f(x1,y1, z1);
GL11.glTexCoord2f(1,0); GL11.glVertex3f(x1+(x2-x1), y1,z1);
GL11.glTexCoord2f(1,1); GL11.glVertex3f(x1+(x2-x1), y1+(y2-y1), z1+(z2-z1));
GL11.glTexCoord2f(0,1); GL11.glVertex3f(x1,y1+(y2-y1), z1+(z2-z1));
}
GL11.glEnd();
Now that works perfectly, I just need to load images that aren't a power of 2.
you could use slick2d, it is way easier to use and works with lwjgl! All you have to do is:
Image title = null;
public static void main(String[] args) {
}
#Override
public void init(GameContainer Gc, StateBasedGame Sbg)
throws SlickException {
/**
* Images
*/
title = new Image("gfx/main_menu/title/new_title.png");
}
#Override
public void render(GameContainer Gc, StateBasedGame Sbg, Graphics G)
throws SlickException {
/**
* Background
*/
G.setColor(Color.white);
G.fillRect(0, 0, w*s, h*s);
/**
* Images
*/
title.draw(titleY*s,titleX*s);
}
I am a noob learning basic game programming in Slick 2D Using Eclipse and java
I am following a tutorial at https://www.youtube.com/watch?annotation_id=annotation_871076&feature=iv&src_vid=NoksHLldlcM&v=oWm5JY6IlUo and when i hit run it does not work. I get this error
Exception in thread "main" java.lang.RuntimeException: Resource not found: testdata/alphamap.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
at org.newdawn.slick.Image.<init>(Image.java:196)
at org.newdawn.slick.Image.<init>(Image.java:170)
at org.newdawn.slick.Image.<init>(Image.java:158)
at org.newdawn.slick.Image.<init>(Image.java:136)
at org.newdawn.slick.tests.AlphaMapTest.init(AlphaMapTest.java:33)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at org.newdawn.slick.tests.AlphaMapTest.main(AlphaMapTest.java:79)
This is what I am attempting
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
public class Main extends BasicGame{
private static final String Slick2D = null;
public Main(String title) {
super(title);
// TODO Auto-generated constructor stub
}
//this is where execution starts
public static void man(String args[]) throws SlickException {
AppGameContainer app = new AppGameContainer(new Main("First Slick2D"));
app.setDisplayMode(800, 600, false);
app.start();
}
#Override
public void render(GameContainer gc, Graphics g) throws SlickException {
// draw all the graphics
g.fillOval(200, 200, 100, 300)
g.fillRect(300, 200, 100, 200)
g.fillRoundRect(500, 200, 100, 50, 30)
g.drawLine(0, 0, 800, 600)
g.drawString(Welcome to Slick2D, 400, 0)
}
#Override
public void init(GameContainer arg0) throws SlickException {
// load all fonts, graphics, sounds, etc.
}
#Override
public void update(GameContainer arg0, int arg1) throws SlickException {
// game logic (AI, user input)
}
}
it's simply can't find image. check your scr folder for testdata folder and check if it contains alphamap.png (or you can delete some code where you getting and setting this picture)
Slick expects images to be in a folder called images. I'm not sure why, but it does.
Your direcetories should look like this:
\src\Main.java
\images\alphamap.png
If that doesn't work, make sure your directories are properly named and are within the project folder, not the src folder.
Example:
\MyProject\src\Main.java is your source code
\MyProject\images\alphamap.png
So I am trying to get a simple GUI setup for my teams game we are starting and I'm trying to make a drop down inventory using "I" as the hot key for it and it drops fine but I can't seem to figure out a way without using a different key to make it retract or in essence "un" draw the image'
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws
SlickException {
mapHud.draw(440, 1);
hotBar.draw(160, 454);
if(inv){
inventory.draw(-40, 1);
}
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
if(input.isKeyPressed(Input.KEY_I)){
inv = true;
}
if(input.isKeyPressed(Input.KEY_ESCAPE)) {
sbg.enterState(0);
}
}
Try something like this:
if(input.isKeyPressed(Input.KEY_I)){
inv = !inv;
}