Slick Forums

Discuss the Slick 2D Library
It is currently Mon May 20, 2013 4:11 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Fri Jun 29, 2012 5:14 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
Hello, I tried to code simple program. Here is the basic concept :

While you are holding left arrow key, program would be adding 1 to float variable red.

Then I'm trying to use that red variable to draw rectangle on screen using custom color.

To assign that red variable to the color, I'm converting the red variable from rgb number ( 255 ) to a float number from 0 to 1, by dividing it by 255, and assign it into color, like this :

color.r = red / 255;

But for some reason, when I run program with this line of code, window just opens and then after just a second it closes.

Can someone please help me ?!?


Top
 Profile  
 
PostPosted: Fri Jun 29, 2012 7:02 pm 
Offline
Regular

Joined: Tue Aug 23, 2011 8:19 am
Posts: 111
What error do you get when the window closes? Also, you should probably show us some of your code so we can see if the error is related to something else in the code.

_________________
My indie games: http://cmagames.webs.com


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 6:47 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
I tried to find error, but eclipse didn't show anything unusual.

I tried running it in both debug mode, and regular mode, and it just does the same thing. In debug mode when there is an error, it pauses the program, and tells me there is error in my program, but in this situation nothing happens, just closes the program.

Here's the whole class I wrote :

Code:
package javagame;

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.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Test extends BasicGameState {

   Image col;
   int x, y, stepSize, filter;
   Input input;
   Color color;
   int sMil;
   double timer = 0, time;
   float Red, Green, Blue;

   public Test(int state) {

   }

   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

      gc.setVSync(true);

      col = new Image("res/collision_img.png");
      stepSize = 1;
   }

   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

      g.setColor(color);
      g.fillRect(100, 100, 50, 50);

      g.drawString("Red: " + Red + "\nGreen: " + Green + "\nBlue: " + Blue, 500, 10);

   }

   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {

      input = gc.getInput();

      if (input.isKeyDown(Input.KEY_LEFT)) {
         Red += 1;
         if (Red >= 255)
            Red = 255;
      }

      if (input.isKeyDown(Input.KEY_RIGHT)) {
         Green += 1;
         if (Green >= 255)
            Green = 255;
      }

      if (input.isKeyDown(Input.KEY_UP)) {

      }

      if (input.isKeyDown(Input.KEY_DOWN)) {

      }
      
      if (input.isKeyPressed(Input.KEY_ESCAPE))
         gc.exit();

   }

   public int getID() {
      return 3;
   }

}


I hope you'll be able to find the error, and educate me, also, earlier I posted my programs and someone told me something about states in init method, can you also explain me more about that ?


Top
 Profile  
 
PostPosted: Sat Jun 30, 2012 10:51 pm 
Offline
Regular

Joined: Tue Aug 23, 2011 8:19 am
Posts: 111
A couple things:

1) The variable named "color" is never initialized. That's probably what's causing the error. You also have some other variables that aren't initialized, but you don't use them as far as I could see.

2) You may already know this and you purposely chose not to use it, but you're not using the optimal method of state IDs. The tutorial I learned from used this overall format for state IDs:
Code:
public class MyState extends StateBasedGame
{
    private int stateID = -1;
   
    public MyState(int sID)
    {
        stateID = sID;
    }
   
    public int getID()
    {
        return stateID;
    }
}
And then you would put a bunch of "public static final int" variables (1 int per state) in your main class so that you can easily transition from any state to another state.

Here is the tutorial I followed to learn StateBasedGame and BasicGameState: http://slick.cokeandcode.com/wiki/doku. ... lickblocks

_________________
My indie games: http://cmagames.webs.com


Top
 Profile  
 
PostPosted: Sun Jul 01, 2012 10:09 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
well I forgot that I deleted it in my class, but after all the if statements for moving, there was code like this :

Code:

        color.r = red;
        color.g = green;
        color.b = blue;



and that is what's producing my error, if I remove it or comment it, error disappears !


Top
 Profile  
 
PostPosted: Sun Jul 01, 2012 11:31 pm 
Offline
Regular

Joined: Tue Aug 23, 2011 8:19 am
Posts: 111
Did you initialize color?

_________________
My indie games: http://cmagames.webs.com


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 9:55 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
I don't know what do you mean by that, so I probably didn't.


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 10:22 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
Thank you so much, I figured it out !

What you pointed me to do is to initialize color value, and I was missing that.

Simply by putting this line of code in my init() method it fixed my problem :

Code:
public void init(...) {

        color = new Color(0, 0, 0);

}



Thank you again, and I hope this post will be useful for other slick users too !


Top
 Profile  
 
PostPosted: Tue Jul 03, 2012 10:25 pm 
Offline
Regular

Joined: Tue Aug 23, 2011 8:19 am
Posts: 111
Here are a couple definitions for you. Very important to know them.

Declaring a variable: This is where you create a type and name for your variable. By default in Java, it is assigned it's base value automatically. If you create an "int", it will be assigned 0. If you create an object (such as a String, Integer, Color, etc), it will be assigned null.
Initializing a variable: This is where you assign a value to the variable.

Example:
Declaring a variable: int number; // number is automatically assigned the value of 0
Initializing a variable: number = 5;

Another example:
Declaring: Color color; // color is automatically assigned the value of null
Initializing: color = new Color(1.0f, 1.0f, 1.0f);

In the code I saw, you declared the instance variable "color", but you never initialized it. If you don't initialize it, then when you call "color.r = red;", you are trying to change the 'r' value of a null variable. This causes a NullPointerException to be thrown because you can't actually do anything to a null variable.

Edit: Glad to be of help :D

_________________
My indie games: http://cmagames.webs.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group