Illegal start of expression java slick - java

I have this problem, and it keeps on throwing the illegal start of expression, and I checked the brackets, and I think they're fine. Here's the code: Also, it says that theres an error at line 30, which I don't really understand....
public class Menu extends BasicGameState {
Image playNow;
Image exitGame;
public String mouse = "No Input Yet!";
public Menu(int state) {
}
#Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
playNow = new Image("res/playNow.png");
exitGame = new Image("res/exitGame.png");
}
#Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
g.drawString(mouse, 590, 10);
g.drawString("Welcome to my Game!", 100, 50);
playNow.draw(100,100);
exitGame.draw(100, 200);
}
//slick counts from the bottom-left of the display, not the top-left, like java does
#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<300) && (mousey>400 && mousey< 435) { //error
sbg.enterState(1);
}
if(mousex>100 && mousex<300) && (mousey>300 && mousey <335) { //error
System.exit(0);
}
}
}
#Override
public int getID() {
return 0;
}
}
I really need help quickly.

In Java, the condition for an if must be completely surrounded by parentheses. Change
if(mousex>100 && mousex<300) && (mousey>400 && mousey< 435) {
to
if((mousex>100 && mousex<300) && (mousey>400 && mousey< 435)) {
... and the other if condition similarly.
The compiler thought that (mousex>100 && mousex<300) was the entire condition, and && (mousey>400 && mousey< 435) didn't make sense as the body for the condition.

Related

Null Pointer Exception once again

I asked this same question a while ago, and i was told to initialize my squareChecker variable and so i did. I still get the same error for some reason that i am unaware of. I have spent literally all day trying to figure out this problem by myself, but to no avail. Anybody know what my problem is?
My Normal Mode class (where i'm calling methods from square class and rendering things):
public class NormalMode extends BasicGameState {
private Square[][] square = new Square[4][4];
private Square squareChecker;
Graphics g;
public NormalMode() { }
#Override
public void init(GameContainer arg0, StateBasedGame Game)throws SlickException {
squareChecker = new Square(arg0);
squareChecker.startGame(g);
for(int i = 0; i < square.length; i++){
for(int j = 0; j < square.length; j++){
square[i][j] = new Square(arg0);
square[i][j].createRandom();
}
}
}
#Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException{
square[0][0].squareDraw(100, 300, g);
square[0][1].squareDraw(150, 300, g);
square[0][2].squareDraw(200, 300, g);
square[0][3].squareDraw(250, 300, g);
square[1][0].squareDraw(100, 400, g);
square[1][1].squareDraw(150, 400, g);
square[1][2].squareDraw(200, 400, g);
square[1][3].squareDraw(250, 400, g);
square[2][0].squareDraw(100, 500, g);
square[2][1].squareDraw(150, 500, g);
square[2][2].squareDraw(200, 500, g);
square[2][3].squareDraw(250, 500, g);
square[3][0].squareDraw(100, 600, g);
square[3][1].squareDraw(150, 600, g);
square[3][2].squareDraw(200, 600, g);
square[3][3].squareDraw(250, 600, g);
}
#Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException { }
#Override
public int getID() {
return 3;
}
}
My Square Class:
public class Square {
private StateBasedGame game;
boolean correct;
boolean clickable;
boolean clicked;
boolean started;
int squares;
int squareX;
int squareY;
public Image squareIncorrect;
public Image squareCorrect;
//For drawing these images from other classes
{
try {
squareIncorrect = new Image("res/squareIncorrect.png");
} catch (SlickException e1) {
e1.printStackTrace();
}
try {
squareCorrect = new Image("res/squareCorrect.png");
} catch (SlickException e) {
e.printStackTrace();
}
}
public Square(GameContainer container) { }
public boolean checkCorrect(){
return correct;
}
public boolean checkClickable(){
return clickable;
}
public boolean checkClicked(){
return clicked;
}
public int returnNumberOfSquares(){
return squares;
}
//Draw appropriate square on screen depending on the creatRandom. CREATE THE RANDOM FIRST!
public void squareDraw(int x ,int y, Graphics g)throws SlickException{
if(correct == true){
g.drawImage(squareCorrect, x, y);
started = true;
}
else if(correct == false){
g.drawImage(squareIncorrect, x, y);
started = true;
}
}
public void createRandom(){
Random rand = new Random();
int sRand = rand.nextInt(2);
if(sRand == 0){
correct = false;
clickable = true;
clicked = false;
System.out.println("This square is a phony!");
}
else if(sRand == 1){
correct = true;
clickable = true;
clicked = false;
System.out.println("This is a true Square!");
squares = squares+1;
}
}
public void youWin(int x, int y, Graphics g){
if(squares == 0){
g.drawString("You win!", x, y);
try {
wait(4000);
game.enterState(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void startGame(Graphics g){
g.drawString("Remember the blue squares! The game will start in 5 seconds!", 200, 300);
}
}
This is the error i get:
Exception in thread "main" java.lang.NullPointerException
at data.src.Square.startGame(Square.java:112)
at data.src.NormalMode.init(NormalMode.java:23)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
at org.newdawn.slick.ScalableGame.init(ScalableGame.java:69)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
at data.src.Core.main(Core.java:30)
In NormalMode.Init() you call squareChecker.startGame(g);
g is defined as Graphics g;.
startGame() calls g.drawString().
This leads me to conclude that you don't instantiate g prior to passing it to your startGame() method.

Trouble with sprite animation slick2d

We are having trouble making our character animate in a java slick2d game. The tutorial I seen shows doing it how I have done it. Right now it only shows the 1st image when going left or right. It never cycles between the 2 images set in walkLeft and walkRight.
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends BasicGameState {
Animation cowboy;
Animation movingLeft;
Animation movingRight;
Image background;
boolean quit = false;
int[] animationDuration = {200,200};
float cowboyPositionX = 0;
float cowboyPositionY = -370;
float shiftX = cowboyPositionX + 200;
float shiftY = cowboyPositionY + 830;
public Game(int state) {
}
public void init(GameContainer gc, StateBasedGame sbg)
throws SlickException {
background = new Image("images/background.png");
Image[] walkLeft = { new Image("images/cowboyleft1.png"), new Image("images/cowboyleft2.png") };
Image[] walkRight = { new Image("images/cowboyright1.png"), new Image("images/cowboyright2.png") };
movingLeft = new Animation(walkLeft, animationDuration, false);
movingRight = new Animation(walkRight, animationDuration, false);
cowboy = movingRight;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
background.draw(cowboyPositionX, cowboyPositionY);
cowboy.draw(shiftX, shiftY);
g.drawString("Cowboy's X:" + cowboyPositionX + "\nCowboy's Y: "
+ cowboyPositionY, 400, 20);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
throws SlickException {
Input input = gc.getInput();
if (input.isKeyDown(Input.KEY_LEFT)) {
cowboy = movingLeft;
cowboyPositionX += horizontalSpeed;
if (cowboyPositionX > 0) {
cowboyPositionX -= delta * horizontalSpeed;
}
}
if (input.isKeyDown(Input.KEY_RIGHT)) {
cowboy = movingRight;
cowboyPositionX -= horizontalSpeed;
if (cowboyPositionX < -2975.0) {
cowboyPositionX += delta * horizontalSpeed;
}
}
public int getID() {
return 1;
}
}
In your update method try inserting cowboy.update(delta); that should work. It worked for the program that I have been working on.

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.

When drawing ImageArray on screen with For-loop, keep drawing on screen / When making it .jar, it wont execute

I made this 2D game in Java, with Slick. The problem seems to be in this code:
public void initRedAndBlueBall() throws SlickException
{
// Makes sure the red and the blue ball aren't too close
for(int i = 0; i <= amountOfEatenBalls; i++)
{
// Get random cordinates for the red and blue ball.
randomRedX[i] = random.nextInt(550) + 24;
randomRedY[i] = random.nextInt(350) + 24;
randomBlueX = random.nextInt(550) + 24;
randomBlueY = random.nextInt(350) + 24;
// Initialize the new Redball with a picture.
redball[i] = new Image("res/RedBall.png");
// Draws the red ball on the screen.
drawRedBall(randomRedX[i], randomRedY[i], redball[i]);
if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == false)
{
System.out.println("The red and the blue ball have good positions");
break;
}
else if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == true)
{
System.out.println("The red and the blue ball are to close!");
System.out.println("Repositioning the red and blue ball");
}
}
}
Here's all of my code:
Game.java
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class Game extends StateBasedGame
{
public static final String gamename = "Mini Game";
public static final int menu = 0;
public static final int play = 1;
public static final int gameOver = 2;
public static final int windowX = 600;
public static final int windowY = 400;
public Game(String gamename)
{
super(gamename);
this.addState(new Menu(menu));
this.addState(new Play(play));
this.addState(new GameOver(gameOver));
}
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(windowX, windowY, false);
appgc.start();
}
catch(SlickException e)
{
e.printStackTrace();
}
}
}
Menu.java
package javagame;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.MouseListener;
public class Menu extends BasicGameState
{
Image PlayNow, PlayNowHover, ExitGame, ExitGameHover, PlayButton, ExitGameButton;
int MousePosX;
int MousePosY;
public Menu(int state) {}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
// Billederne er 125x26
PlayNow = new Image("res/PlayNow.png");
PlayNowHover = new Image("res/PlayNowHover.png");
ExitGame = new Image("res/ExitGame.png");
ExitGameHover = new Image("res/ExitGameHover.png");
PlayButton = PlayNow;
ExitGameButton = ExitGame;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
// Framet er 600x400
g.drawString(MousePosX + ", " + MousePosY, 500, 10);
g.drawLine(0, 26, 600, 26);
g.drawImage(PlayButton, 200, 100);
g.drawImage(ExitGameButton, 200, 150);
g.drawString("You move with the arrowkeys.", 200, 200);
g.drawString("There's 1 rule:", 200, 215);
g.drawString("Don't eat the red balls!", 200, 230);
g.drawString("But you can enjoy the blue ones :- P", 200, 245);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
MousePosX = input.getMouseX();
MousePosY = input.getMouseY();
// PlayNow Hover or Normal
if(MousePosX > 200 && MousePosX < 325 && MousePosY > 100 && MousePosY < 126)
{
PlayButton = PlayNowHover;
}
else
{
PlayButton = PlayNow;
}
//ExitGame Hover or Normal
if(MousePosX > 200 && MousePosX < 325 && MousePosY > 150 && MousePosY < 176)
{
ExitGameButton = ExitGameHover;
}
else
{
ExitGameButton = ExitGame;
}
// Now the buttons have a function
if(MousePosX > 200 && MousePosX < 325 && MousePosY > 100 && MousePosY < 126)
{
if(Mouse.isButtonDown(0))
{
sbg.enterState(1);
}
}
if(MousePosX > 200 && MousePosX < 325 && MousePosY > 150 && MousePosY < 176)
{
if(Mouse.isButtonDown(0))
{
System.exit(0);
}
}
}
public int getID()
{
return 0;
}
}
Play.java
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import java.util.Random;
public class Play extends BasicGameState
{
Image ball, blueball;
Image[] redball;
Animation roll;
int[] duration = {200, 200};
float defBallPosX = 300; // DEFAULT
float defBallPosY = 200; // DEFAULT
float ballPosX = 300;
float ballPosY = 200;
int MousePosX;
int MousePosY;
Random random;
float[] randomRedX; // X for the RedBall.png
float[] randomRedY; // Y for the RedBall.png
float randomBlueX; // X for the BlueBall.png
float randomBlueY; // Y for the BlueBall.png
static int amountOfEatenBalls;
boolean blueAndRedAreNotToClose;
public Play(int state)
{
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
ball = new Image("res/PurpBallNorm.png"); // 25x25 px
Image[] images = {ball, ball};
redball = new Image[50];
blueball = new Image("res/BlueBall.png");
roll = new Animation(images, duration, false);
random = new Random();
randomRedX = new float[50];
randomRedY = new float[50];
amountOfEatenBalls = 0;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
// The Window is 600x400
g.drawString(MousePosX + ", " + MousePosY, 500, 10);
g.drawLine(0, 26, Game.windowX, 26); // Ceiling
g.drawLine(1, 26, 1, Game.windowY); // Left Wall
g.drawLine(Game.windowX - 2, 26, Game.windowX - 2, Game.windowY - 2); // Right Wall
g.drawLine(1, Game.windowY - 2, Game.windowX - 2, Game.windowY - 2); // Floor
roll.draw(ballPosX, ballPosY);
initRedAndBlueBall();
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
MousePosX = input.getMouseX();
MousePosY = input.getMouseY();
if(input.isKeyDown(Input.KEY_UP))
ballPosY -= delta * .1f;
else if(input.isKeyDown(Input.KEY_DOWN))
ballPosY += delta * .1f;
else if(input.isKeyDown(Input.KEY_LEFT))
ballPosX -= delta * .1f;
else if(input.isKeyDown(Input.KEY_RIGHT))
ballPosX += delta * .1f;
blueBallTouch();
for(int i = 0; i <= amountOfEatenBalls; i++)
{
if(isInTheRangeOf(ballPosX, randomRedX[i], 25) == true && isInTheRangeOf(ballPosY, randomRedY[i], 25) == true)
{
System.out.println("Player has eaten a red ball!");
sbg.enterState(2);
}
}
// Makes sure the ball can't run out of the window
if(ballPosX <= 2)
{
ballPosX += delta * .1f;
}
else if(ballPosX >= Game.windowX - 27)
{
ballPosX -= delta * .1f;
}
else if(ballPosY <= 26)
{
ballPosY += delta * .1f;
}
else if(ballPosY >= Game.windowY - 27)
{
ballPosY -= delta * .1f;
}
}
public int getID()
{
return 1;
}
public boolean isInTheRangeOf(float x, float y, float range)
{
float a = 0;
float b = 0;
if(x > y)
{
a = x;
b = y;
}
else if(y > x)
{
b = x;
a = y;
}
if((a - b) <= range)
{
return true;
}
else if((a - b) >= range)
{
return false;
}
return true;
}
public void initRedAndBlueBall() throws SlickException
{
// Makes sure the red and the blue ball aren't too close
for(int i = 0; i <= amountOfEatenBalls; i++)
{
// Get random cordinates for the red and blue ball.
randomRedX[i] = random.nextInt(550) + 24;
randomRedY[i] = random.nextInt(350) + 24;
randomBlueX = random.nextInt(550) + 24;
randomBlueY = random.nextInt(350) + 24;
// Initialize the new Redball with a picture.
redball[i] = new Image("res/RedBall.png");
// Draws the red ball on the screen.
drawRedBall(randomRedX[i], randomRedY[i], redball[i]);
if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == false)
{
System.out.println("The red and the blue ball have good positions");
break;
}
else if(isInTheRangeOf(randomRedX[i], randomBlueX, 30) == true)
{
System.out.println("The red and the blue ball are to close!");
System.out.println("Repositioning the red and blue ball");
}
}
}
public void drawRedBall(float x, float y, Image image)
{
Graphics g = new Graphics();
g.drawImage(image, x, y);
}
public void blueBallTouch() throws SlickException
{
if(isInTheRangeOf(ballPosX, randomBlueX, 25) == true && isInTheRangeOf(ballPosY, randomBlueY, 25) == true)
{
amountOfEatenBalls++;
initRedAndBlueBall();
}
else
{
}
}
public static int getAmountOfEatenBalls()
{
return amountOfEatenBalls;
}
}
GameOver.java
package javagame;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
public class GameOver extends BasicGameState
{
Image ExitGame, ExitGameHover, ExitGameButton;
int MousePosX;
int MousePosY;
public GameOver(int state)
{
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
ExitGame = new Image("res/ExitGame.png");
ExitGameHover = new Image("res/ExitGameHover.png");
ExitGameButton = ExitGame;
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException
{
g.drawImage(ExitGameButton, 200, 100);
g.drawString("You've eaten: " + Play.getAmountOfEatenBalls() + " balls!", 200, 190);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException
{
Input input = gc.getInput();
MousePosX = input.getMouseX();
MousePosY = input.getMouseY();
if(MousePosX > 200 && MousePosX < 325 && MousePosY > 100 && MousePosY < 126)
{
ExitGameButton = ExitGameHover;
}
else
{
ExitGameButton = ExitGame;
}
if(MousePosX > 200 && MousePosX < 325 && MousePosY > 100 && MousePosY < 126)
{
if(Mouse.isButtonDown(0))
{
System.exit(0);
}
}
}
public int getID()
{
return 2;
}
}
-This is the first time I use stackoverflow, so if this is totally unreadable, tell me what I should do instead of uploading all the code..
Thanks in advance!
The problem is most probably with resource loading. You load image as file, it works when they are unpacked, but when they are in jar you should use Class.getResourceAsStream
For example image is in jar file accessible with following res/image.jpg then you should use getClass().getResourceAsStream("/res/image.jpg"). Then you can read image contents using this stream.

Stopping image movement

okay, so i have my image move straight down along the y axis whenever i click the mouse, my only problem is i don't know how to get it to stop when it hits the bottom of the screen, can someone please help?
import java.awt.Point;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Control extends BasicGameState {
public static final int ID = 1;
public Methods m = new Methods();
public Point[] point = new Point[(800 * 600)];
int pressedX;
int pressedY;
int num = 0;
String Build = "1.1";
public void init(GameContainer container, StateBasedGame game) throws SlickException{
}
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (Point p : point) {
if (p != null) {
m.drawParticle(p.x, p.y += 1);
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
public void update(GameContainer container, StateBasedGame game, int delta) {
}
public void mousePressed(int button, int x, int y) {
pressedX = x;
pressedY = y;
num = num + 1;
point[num] = new Point(pressedX, pressedY);
}
public int getID() {
return ID;
}
}
I'd imagine somewhere you will want to check the x/y pos of the particle before you renderer it and remove it from the array when it's out of bounds...
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
for (int index = 0; index < point.length; index++) {
Point p = point[index];
if (p != null) {
p.y++;
if (p.y > height) { // You'll need to define height...
point[index] = null; // Or do something else with it??
} else {
m.drawParticle(p.x, p.y);
}
}
}
g.drawString("Particle Test", 680, 0);
g.drawString("Build: " + Build, 680, 15);
g.drawString("Pixels: " + num, 10, 25);
}
You could also do a preemptive check, which would allow you to know what points are at the bottom of the screen...
if (p != null) {
if (p.y >= height) { // You'll need to define height...
// Do something here
} else {
p.y++;
m.drawParticle(p.x, p.y);
}
}

Categories