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)
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.