I cannot solve this with slick2D Java - java

I'm trying to make a splash screen for a game. I've searched Google without avail. I was following this tutorial here and it worked for him. My code was a little different but looked a little like his. Here is my code:
GAME CLASS:
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Life - Alpha";
public static int menu = 1;
public static int play = 2;
public static int splash = 0;
public Game(String gamename) {
super(gamename);
}
public void initStatesList(GameContainer gc) throws SlickException {
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(0);
}
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new Game(gamename));
app.setDisplayMode(800, 600, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
MENU CLASS:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
public Menu(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image back = new Image("res/Back.png");
g.drawImage(back, 0, 0);
Image panel = new Image("res/Panel.png");
g.drawImage(panel, 240, 100);
Image logo = new Image("res/Life Alpha.png");
g.drawImage(logo, 290, 130);
Image playButton = new Image("res/Play.png");
g.drawImage(playButton, 330, 310);
Image exitButton = new Image("res/Exit.png");
g.drawImage(exitButton, 330, 370);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();
if((xpos > 330 && xpos < 480) && (ypos > 250 && ypos < 290)) {
if(input.isMousePressed(0)) {
sbg.enterState(2);
}
}
}
public int getID() {
return 0;
}
}
PLAY CLASS:
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play extends BasicGameState {
public Play(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image back = new Image("res/Back.png");
g.drawImage(back, 0, 0);
Image panel = new Image("res/Panel2.png");
g.drawImage(panel, 170, 100);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
}
public int getID() {
return 1;
}
}
SPLASHSCREEN CLASS:
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class SplashScreen extends BasicGameState {
Image splash;
private int elapsedTime;
private final int DELAY = 3000;
public SplashScreen(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
splash = new Image("res/SplashScreen.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawImage(splash, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(1);
}
}
public int getID() {
return 0;
}
}
The error i get is:
Sat Apr 27 22:48:01 EST 2013 INFO:Slick Build #274
Sat Apr 27 22:48:01 EST 2013 INFO:LWJGL Version: 2.8.5
Sat Apr 27 22:48:01 EST 2013 INFO:OriginalDisplayMode: 1366 x 768 x 32 #60Hz
Sat Apr 27 22:48:01 EST 2013 INFO:TargetDisplayMode: 800 x 600 x 0 #0Hz
Sat Apr 27 22:48:01 EST 2013 INFO:Starting display 800x600
Sat Apr 27 22:48:01 EST 2013 INFO:Use Java PNG Loader = true
Sat Apr 27 22:48:01 EST 2013 INFO:Controllers not available
Exception in thread "main" java.lang.NullPointerException
at mms001.Game.initStatesList(Game.java:18)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at mms001.Game.main(Game.java:29)

It looks like there might be a couple of issues here.
I noticed that your Menu class is returning 0 as it's ID and so is the SplashScreen class. I bet if you changed the getID of the Menu class to be 1, that would fix part of your problem.
class Menu...
public int getID() {
return 1;
}
You will also need to update your Play class's getID method as well to return 2.
class Play...
public int getID() {
return 2;
}
Once you do that they will match your static fields defined in your main class.
Then update your initStatesList method to init splash and enter the menu state
public void initStatesList(GameContainer gc) throws SlickException {
this.getState(splash).init(gc, this);
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
Hope this helps.

Related

Firing bullet from gun's current position to mouse x & y position

Please help I'm kinda lost in my coding.
So I've created a bullet class:
package javagame.states;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;
public class Bullet {
private Vector2f pos;
private Vector2f speed;
private int lived = 0;
private boolean aktiv = true;
private static int MAX_LIFETIME = 2000;
public Bullet (Vector2f pos, Vector2f speed){
this.pos = pos;
this.speed = speed;
}
public Bullet(){
aktiv = false;
}
public void update(int t){
rotation++;
if(aktiv){
Vector2f realSpeed = speed.copy();
realSpeed.scale((t/1000.0f));
pos.add(realSpeed);
lived += t;
if(lived > MAX_LIFETIME) aktiv = false;
}
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, Graphics g) throws SlickException {
if(aktiv){
g.setColor(Color.red);
g.fillOval(pos.getX(), pos.getY(), 10, 10);
}
}
public boolean isAktiv(){
return aktiv;
}
}
then called the bullet class here in my robot class:
package javagame.states;
import java.util.Iterator;
import java.util.LinkedList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Robot2 extends BasicGameState{
private LinkedList<Bullet> bullets;
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
bullets = new LinkedList<Bullet>();
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
for(Bullet b : bullets){
b.render(gc, g);
}
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Iterator<Bullet> i = bullets.iterator();
while(i.hasNext()){
Bullet b = i.next();
if(b.isAktiv()){
b.update(delta);
}else{
i.remove();
}
}
Input input = gc.getInput();
int mouseX = input.getMouseX();
int mouseY = input.getMouseY();
int xDistance = (int) (RobotX() + Robotwidth() / 2 - mouseX); //RobotX is the robot image x position same to RobotY
int yDistance = (int) (RobotY() + Robotheight() / 2 - mouseY);
double angleToTurn = Math.toDegrees(Math.atan2(yDistance, xDistance));
if(mouseX != 0){
angleToTurn += 270;
}else{
if(mouseY != 0)
angleToTurn += 180;
}
robot.setRotation((float) angleToTurn); //robot is an image. didn't included the code here to shorten my codes
if(gc.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)){
bullets.add(new Bullet(new Vector2f(Sprites.getRobotX(), Sprites.getRobotY()), new Vector2f(mouseX, mouseY)));
}
}
#Override
public int getID() {
return States.ROBOT;// has an integer value
}
}
the robot is rotating perfectly according to mouse's cursor movement but when I hit the mouse's left button to fire a bullet, it doesn't follow the mouse's cursor location. This is what I've got when i run these codes and pressing the left button of my mouse. Please check my sample image.
http://s12.postimg.org/kwsr41p71/Untitled_1.jpg
What I want to achieve is to correct the direction of the bullet according to mouse cursor location.
Any help would be greately much appreciated. Thank you very much.

Slick addState java.lang.Error

Ok, so I'm doing something wrong and I can't figure out what. I'm following a tutorial about building a simple game with Slick. I know there is almost nothing in the code, but at this point the code should be able to compile.
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame{
public static final String gamename = "Game name!";
public static final int menu = 0;
public static final int play = 1;
public Game(String gamename){
super(gamename);
this.addState(new Menu(menu));
this.addState(new Play(play));
}
public void initStatesList(GameContainer gc) throws SlickException{
this.getState(menu).init(gc, this);
this.getState(play).init(gc, this);
this.enterState(menu);
}
public static void main(String[] args) {
AppGameContainer appgc;
try{
appgc = new AppGameContainer(new Game(gamename));
appgc.setDisplayMode(640, 360, false);
appgc.start();
}catch(SlickException e){
e.printStackTrace();
}
}
}
And here are the classes
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Play {
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
}
public int getID(){
return 1;
}
}
and
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu {
public Menu(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
}
public int getID(){
return 0;
}
}
The error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method addState(GameState) in the type StateBasedGame is not applicable for the arguments (Menu)
The method addState(GameState) in the type StateBasedGame is not applicable for the arguments (Play)
at javagame.Game.<init>(Game.java:12)
at javagame.Game.main(Game.java:25)
As Game extends StateBasedGame and it does not override addState(), when you say this.addState(new Menu(menu)); it is trying to call method defined in StateBasedGame API referance
So your Menu and Play class should be sub class of GameState

Image mouse hover error

my problem is that whenever I try to change an image when a mouse is hovered over it, it doesn't change, and when I do playgame.destroy(); it just shows a white screen behind it, not the other image. Heres my code:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
Image playgame;
Image exitgame;
Image playgame_hover;
Image exitgame_hover;
public String mouse = "No Input Yet!";
public Menu(int state) {
}
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
playgame = new Image("res/playgame.png");
exitgame = new Image("res/exitgame.png");
playgame_hover = new Image("res/playgame_hover.png");
exitgame_hover = new Image("res/exitgame_hover.png");
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawString(mouse, 590, 10);
playgame.draw(100,100);
exitgame.draw(100, 200);
}
#Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int mousex = Mouse.getX();
int mousey = Mouse.getY();
mouse = "Mouse coordinate x: " + mousex + " y: " + mousey;
// x-min:105 x-max:300 y-min: y-max:300
if(input.isMouseButtonDown(0)) {
if((mousex>100 && mousex<600) && (mousey>357 && mousey<437)) {
sbg.enterState(1);
}
if((mousex>100 && mousex<600) && (mousey>257 && mousey <337)) {
System.exit(0);
}
}
if((mousex>100 && mousex<600) && (mousey>357 && mousey<437)) {
playgame.destroy();
playgame_hover.draw(100, 100);
}
if((mousex>100 && mousex<600) && (mousey>257 && mousey <337)) {
exitgame.destroy();
exitgame_hover.draw(100, 200);
}
}
#Override
public int getID() {
return 0;
}
}
You can only draw things in the render method. You have to save the state changed in the update method and then read those states in the render method.

Mouse Over Area for slick2d?

I cannot find an answer in Google, but is there a Mouse Over Area for Slick2D? Google just gives me results of Java's MouseOverArea. I just want to know if there is a MouseOverArea for Slick2D, and how it looks like.
Here is my code:
Game class
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame {
public static final String gamename = "Life - Alpha";
public static int splash = 0;
public static int menu = 1;
public static int loading = 2;
public static int play= 3;
public Game(String gamename) {
super(gamename);
}
public void initStatesList(GameContainer gc) throws SlickException {
this.addState(new SplashScreen (splash));
this.addState(new Menu (menu));
this.addState(new Exit (exit));
this.addState(new Loading (loading));
this.addState(new Play(play));
this.enterState(0);
}
public static void main(String[] args) {
AppGameContainer app;
try {
app = new AppGameContainer(new Game(gamename));
app.setDisplayMode(800, 600, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
SplashScreen class:
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class SplashScreen extends BasicGameState {
Image splash;
private int elapsedTime;
private final int DELAY = 3000;
public SplashScreen(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
splash = new Image("res/SplashScreen.png");
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawImage(splash, 0, 0);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(1);
}
}
public int getID() {
return 0;
}
}
Menu class:
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Menu extends BasicGameState {
public Menu(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Background.png");
g.drawImage(background, 0, 0);
Image logo = new Image("res/Logo.png");
g.drawImage(logo, 275, 50);
Image playButton = new Image("res/Play button.png");
g.drawImage(playButton, 210, 250);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
int xpos = Mouse.getX();
int ypos = Mouse.getY();
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
//I want to put the Slick2D MouseOverArea code here...
//So then when I put the mouse over the playButton, something will display.
}
}
public int getID() {
return 1;
}
}
Loading class:
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Loading extends BasicGameState {
private int elapsedTime;
private final int DELAY = 5000;
public Loading(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
Image background = new Image("res/Back.png");
g.drawImage(background, 0, 0);
Image loading = new Image("res/Loading.png");
g.drawImage(loading, 210, 150);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
elapsedTime += delta;
if(elapsedTime >= DELAY) {
sbg.enterState(3);
}
}
public int getID() {
return 2;
}
}
Play class:
I'm working on it still... but this class doesn't need the MouseOverArea, the Menu class does.
So that was my code above. I just need a MouseOverArea for Slick2D. Google doesn't help. Hope you can.
Also, can you have a TextField in Slick2D? I don't know if I can. I know in normal Java you can, but can you in Slick2D?
If there are any mistakes, don't worry, I can fix them.
Thanks
Isn't the code put in here?
if ((xpos > 300 && xpos < 510) && (ypos > 230 && ypos < 260)) {
//mouseover
if (input.isMousePressed(0)) {
sbg.enterState(2);
}
}
}

textfield not working with BasicGameState

I've tested TextField on a clean BasicGame and it worked. (I see the border and i can type.)
The code:
TextField lanText;
TrueTypeFont font;
public void init(........)
{
font = new TrueTypeFont(new java.awt.Font(java.awt.Font.SERIF,java.awt.Font.BOLD,8),false);
lanText = new TextField(gc, font, 50, 100, 350, 25);
}
public void render(.....)
{
lanText.render(gc, g);
}
But when i try it on my game with BasicGameState it doesn't work. What's wrong? (I can see the border but I cant type)
I have 4 states. Menu 0. Game 1. Coop 2. Options 3.
Im trying to add it to the state 2(Coop).
public class Game
extends StateBasedGame
{
public final int menu = 0;
public final int game = 1;
public final int option = 2;
public final int coop = 3;
public Game(String gamename)
{
super(gamename);
this.addState(new Menu(menu));
this.addState(new Game(game));
this.addState(new Option(option));
this.addState(new Coop(coop));
}
public void initStatesList(GameContainer gc)
throws SlickException
{
this.getState(menu).init(gc, this);
this.getState(game).init(gc, this);
this.getState(option).init(gc, this);
this.getState(coop).init(gc, this);
this.enterState(menu);
}
}
Ok, I figured it out. Here is the code for anyone who will have the same problem.
import java.awt.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Coop
extends BasicGameState
{
TextField text;
UnicodeFont font;
public Coop(int state)
{
}
public void init(GameContainer gc , StateBasedGame sbg)
throws SlickException
{
font = getNewFont("Arial" , 16);
}
public void render(GameContainer gc , StateBasedGame sbg , Graphics g)
throws SlickException
{
text.render(gc , g);
g.setFont(font);
}
public void update(GameContainer gc , StateBasedGame sbg , int delta)
throws SlickException
{
font.loadGlyphs();
}
public int getID()
{
return 3;
}
public void enter(GameContainer gc , StateBasedGame sbg)
throws SlickException
{
text = new TextField(gc , font , 150 , 270 , 200 , 35);
}
public UnicodeFont getNewFont(String fontName , int fontSize)
{
font = new UnicodeFont(new Font(fontName , Font.PLAIN , fontSize));
font.addGlyphs("#");
font.getEffects().add(new ColorEffect(java.awt.Color.white));
return (font);
}
}
I don't know why it needs to use font.loadGlyphs(); but without it, it wont work.

Categories