This site requires JavaScript, please enable it in your browser!
Greenfoot back
jmstinson221
jmstinson221 wrote ...

2015/4/8

Scrolling Background null pointer

jmstinson221 jmstinson221

2015/4/8

#
Having some trouble with scrolling, the null pointer is at line bgImage.drawImage(bg,scrollHPosition, scrollVPosition); java.lang.NullPointerException at Arena1.scrollBackground(Arena1.java:96) at Arena1.checkKeyPress(Arena1.java:57) at Arena1.act(Arena1.java:46) at greenfoot.core.Simulation.actWorld(Simulation.java:589) at greenfoot.core.Simulation.runOneLoop(Simulation.java:524) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
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
import greenfoot.*; 
import java.util.*;
 
public class Arena1 extends World
{
    private static int CharScore;
    private static int Gladiator1Score;
    private static int Gladiator2Score;
    private static int Gladiator3Score;   
    private static final int WIDTH = 800;
    private static final int HEIGHT = 500;   
    public static int GameLevel;
    private GreenfootImage bgImage;
    public int scrollSpeed;
    public int scrollHPosition = 0;
    public int scrollVPosition = 0;
     
    private static int timer = 0;  
    private boolean arenapopulated = false;
    private static  final int softwallX = 2000;
    private static  final int softwallY = 1600;   
   
     
   
    public Arena1()    {
        super(800, 500, 1, false);
         
        GreenfootImage bgImage = new GreenfootImage("Arena1background.jpg");
         
         
         
        GameLevel = 2;
        GameSpace.GameLevel = 2;
        addObject(new Character(), getWidth()/2, getHeight()/2);
        addObject(new Gladiator(), 100, 50);
        addObject(new Gladiator(), 700, 50);
        addObject(new Gladiator(), 100, 450);
         
        populateslaves();
        
        setBackground(bgImage);
    }   
    public void act()    {
        timer++;
        scrollSpeed = (Character.basespeed + Character.speedlevel);
        checkKeyPress();
    }
    private void checkKeyPress()   {    
         
                if (Greenfoot.isKeyDown("a"))
                {
                   scrollBackground(-scrollSpeed,0);
                   scrollActors(-scrollSpeed,0);
                }
                if (Greenfoot.isKeyDown("d"))
                {
                   scrollBackground(scrollSpeed,0);
                   scrollActors(scrollSpeed,0);
                }
                if (Greenfoot.isKeyDown("w"))
                {
                   scrollBackground(0,-scrollSpeed);
                   scrollActors(0,scrollSpeed);
                }
                if (Greenfoot.isKeyDown("s"))
                {
                   scrollBackground(0,scrollSpeed);
                   scrollActors(0,-scrollSpeed);
                }
    }
     
     private void updateScore()   {
        
     }
     
    private void populateslaves()    {
        int i;
        for(i=0; i<200; i++)
        {
            addObject(new Slave(5), Greenfoot.getRandomNumber(softwallX), Greenfoot.getRandomNumber(softwallY));
        }
    }
     
public void scrollBackground(int dx, int dy) {
        dx = dx % WIDTH;
        if (dx > 0)
            dx = dx - WIDTH;
        scrollHPosition = (scrollHPosition + dx) % WIDTH;
 
        dy = dy % HEIGHT;
        if (dy > 0)
            dy = dy - HEIGHT;
        scrollVPosition = (scrollVPosition + dy) % HEIGHT;
 
        GreenfootImage bg = getBackground();
        bgImage.drawImage(bg,scrollHPosition, scrollVPosition);
        bgImage.drawImage(bg,scrollHPosition + WIDTH, scrollVPosition);
        bgImage.drawImage(bg,scrollHPosition , scrollVPosition + HEIGHT);
        bgImage.drawImage(bg,scrollHPosition + WIDTH , scrollVPosition + HEIGHT); 
    }
 
 public void scrollActors(int dx, int dy) {
        List<Actor> actors = getObjects(Enemy.class);
        for(Actor a : actors) {
            a.setLocation(a.getX()+dx, a.getY()+dy);}
    }
}
danpost danpost

2015/4/8

#
Remove the first word from line 28. With it, you are declaring a local variable; without it you are utilizing the field declared at line 13.
You need to login to post a reply.