Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  cannot make my sprite walk and stop  (Read 836 times)
0 Members and 1 Guest are viewing this topic.
Offline MrDodo

Senior Newbie





« Posted 2012-05-21 18:31:57 »

Hi guys,

I'm an absolute newbie, and during the past weeks I have started writing a small point and click adventure game.

Until yesterday I have a "game" that was able to load a background and the sprite for the player, and listening to the mouse move the sprite to the point were you click. But I only have one animation, so the player animation was always "walking".
I have read a bit about how to manage more than one animation using arrays, so now I'm trying to have the same thing but with two animations: one when the player is walking, and another one for when it is not moving at all (in this animation the sprite will blink or something like that). My problem is that now I cannot make it run anymore, right now I have a null pointer exception that I cannot explain... (because I think I didn't change anything there!)
Exception in thread "main" java.lang.NullPointerException
   at InputManagerTest.checkGameInput(InputManagerTest.java:135)

and line 135 is just
1  
player.setVelocityX(velocityX);
The problem is calling the player.anything, but I don't know why it always give the null pointer exception. but it was working fine before last changes!

Aaaaany help will be super useful for me, and I thank you in advance because I'm sure I am doing hundreds of things incorrectly... but I already spent some hours playing around, looking on my java book and on the forum I am stuck.

I copy the relevant code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
    
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;

import graphics.*;
import input.*;
import test.GameCore;

public class InputManagerTest extends GameCore {

    public static void main(String[] args) {
        new InputManagerTest().run();
    }

    protected GameAction exit;
    protected GameAction moveRight;
    protected GameAction pause;
    protected InputManager inputManager;
    private Player player;
    private Image bgImage;
    private boolean paused;
   
    private int mouseX;
   
    private Sprite playerSprite;

    public void init() {
        super.init();
        Window window = screen.getFullScreenWindow();
        inputManager = new InputManager(window);

        createGameActions();
        createSprite();
        paused = false;
    }


    /**
        Tests whether the game is paused or not.
    */

    public boolean isPaused() {
        return paused;
    }


    /**
        Sets the paused state.
    */

    public void setPaused(boolean p) {
        if (paused != p) {
            this.paused = p;
            inputManager.resetAllGameActions();
        }
    }


    public void update(long elapsedTime) {
        // check input that can happen whether paused or not
       checkSystemInput();

        if (!isPaused()) {
            // check game input
           checkGameInput();

            // update sprite
           player.update(elapsedTime);
        }
    }


    /**
        Checks input from GameActions that can be pressed
        regardless of whether the game is paused or not.
    */

    public void checkSystemInput() {
        if (pause.isPressed()) {
            setPaused(!isPaused());
        }
        if (exit.isPressed()) {
            stop();
        }
    }


    /**
        Checks input from GameActions that can be pressed
        only when the game is not paused.
    */

    public void checkGameInput() {
        float velocityX = 0;

        if (moveRight.isPressed()) {
           player.walk();
           player.setState(Player.STATE_WALK);
       
        }
       
        player.setVelocityX(velocityX);

    }


    public void draw(Graphics2D g) {
        // draw background
       g.drawImage(bgImage, 0, 0, null);

        // draw sprite
       g.drawImage(player.getImage(),
            Math.round(player.getX()),
            Math.round(player.getY()),
            null);
    }


    /**
        Creates GameActions and maps them to keys.
    */

    public void createGameActions() {
        exit = new GameAction("exit",
            GameAction.DETECT_INITAL_PRESS_ONLY);
        moveRight = new GameAction("moveRight");
        pause = new GameAction("pause",
            GameAction.DETECT_INITAL_PRESS_ONLY);

        inputManager.mapToKey(exit, KeyEvent.VK_ESCAPE);
        inputManager.mapToKey(pause, KeyEvent.VK_P);

        inputManager.mapToMouse(moveRight, InputManager.MOUSE_BUTTON_1);

    }


    /**
        Load images and creates the Player sprite.
    */

    private void createSprite() {
       Image[][] images = new Image[4][];
        // load images
       bgImage = loadImage("dcity.JPG");
        images[0] = new Image[] {
                loadImage("titocamina1.PNG"),
                loadImage("titocamina2.PNG"),
                loadImage("titocamina3.PNG"),
                loadImage("titowink3.PNG"),
            };

            images[1] = new Image[images[0].length];
       
        // create animation            
         Animation [] playerAnim = new Animation[2];
          playerAnim[0] = createPlayerAnim(images[1][0], images[1][1], images[1][2]);
          playerAnim[1] = createPlayerAnim(images[1][2], images[1][2], images[1][3]);
         
          playerSprite = new Player(playerAnim[0], playerAnim[1]);
 
    }
    private Animation createPlayerAnim(Image player1,Image player2, Image player3)
        {
            Animation anim = new Animation();
            anim.addFrame(player1, 150);
            anim.addFrame(player2, 150);
            anim.addFrame(player3, 150);
            anim.addFrame(player2, 150);
           
            return anim;
        }

}


and now the Player:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
package input;

import graphics.*;
//import input.InputManager;


/**
    The Player class extends the Sprite class to add states
*/

public class Player extends Sprite {
   
    protected InputManager inputManager;

    public static final int STATE_WALK = 0;
    public static final int STATE_STAND = 1;

    public static final float SPEED = .4f;

    private int state;
    private Animation animWalk;
    private Animation animStand;
   
    private int mouseX;

    public Player(Animation animWalk, Animation animStand) {
        super(animStand);
        this.animWalk=animWalk;
        this.animStand=animStand;
        state = STATE_STAND;
    }


    /**
        Gets the state of the Player;
    */

    public int getState() {
        return state;
    }


    /**
        Sets the state of the Player;
    */


        public void setState(int state) {
            if (this.state != state) {
                this.state = state;
            }
        }


    /**
        Causes the Player to Walk
    */
 
        public void walk() {
       
       mouseX=inputManager.mouseLocation.x;
       state = STATE_WALK;

       if ((mouseX-(getX()))>10 && mouseX>(getX()+200)){
           setVelocityX(+1);
       }
       else {
                    setState(STATE_STAND);
       }
       
       if ((mouseX-(getX()))<10 && mouseX<getX()){
           setVelocityX(-1);  
       }
       else {
             setState(STATE_STAND);
       }
       
    }

    /**
        Updates the player's position and animation. Also, sets the
        Player's state.
    */

        public void update(long elapsedTime) {
            // select the correct Animation
           Animation newAnim = anim;
            if (getVelocityX() < 0) {
                newAnim = animWalk;
            }
            else if (getVelocityX() > 0) {
                newAnim = animWalk;
           }
            else if (getVelocityX() == 0) {
                newAnim = animStand;
            }

            // update the Animation
           if (anim != newAnim) {
                anim = newAnim;
                anim.start();
            }
            else {
                anim.update(elapsedTime);
           }
            // move player
           super.update(elapsedTime);
        }

    }


and the Sprite class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
package graphics;


import java.awt.Image;

public class Sprite {

    protected Animation anim;
    // position (pixels)
   private float x=0;
    private float y=0;
    // velocity (pixels per millisecond)
   private float dx;
    private float dy;

    /**
        Creates a new Sprite object with the specified Animation.
    */

    public Sprite(Animation anim) {
        this.anim = anim;
    }

    /**
        Updates this Sprite's Animation and its position based
        on the velocity.
    */

    public void update(long elapsedTime) {
        x += dx * elapsedTime;
        y += dy * elapsedTime;
        anim.update(elapsedTime);
    }

    /**
        Gets this Sprite's current x position.
    */

    public float getX() {
        return x;
    }

    /**
        Gets this Sprite's current y position.
    */

    public float getY() {
        return y=750;
    }

    /**
        Sets this Sprite's current x position.
    */

    public void setX(float x) {
        this.x = x;
    }

    /**
        Sets this Sprite's current y position.
    */

    public void setY(float y) {
        this.y = y;
    }

    /**
        Gets this Sprite's width, based on the size of the
        current image.
    */

    public int getWidth() {
        return anim.getImage().getWidth(null);
    }

    /**
        Gets this Sprite's height, based on the size of the
        current image.
    */

    public int getHeight() {
        return anim.getImage().getHeight(null);
    }

    /**
        Gets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */

    public float getVelocityX() {
        return dx;
    }

    /**
        Gets the vertical velocity of this Sprite in pixels
        per millisecond.
    */

    public float getVelocityY() {
        return dy;
    }

    /**
        Sets the horizontal velocity of this Sprite in pixels
        per millisecond.
    */

    public void setVelocityX(float dx) {
        this.dx = dx;
    }

    /**
        Sets the vertical velocity of this Sprite in pixels
        per millisecond.
    */

    public void setVelocityY(float dy) {
        this.dy = dy;
    }

    /**
        Gets this Sprite's current image.
    */

    public Image getImage() {
        return anim.getImage();
    }
}

Offline sproingie

JGO Kernel


Medals: 202



« Reply #1 - Posted 2012-05-21 18:58:35 »

There's only one way that can be an NPE, and that's because player is null.  You don't set player anywhere in that code.
Offline MrDodo

Senior Newbie





« Reply #2 - Posted 2012-05-21 20:00:42 »

That's right, i mess up the names, and I set playerSprite when I wanted to set player... Now at least I solved that one, thanks again!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (38 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!