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

2015/7/30

How to create scrolling world

Gh0sty Gh0sty

2015/7/30

#
I'm extremely new to Greenfoot and I am trying to create a game where when the actor moves, the screen follows the actor, keeping it in the center until it reaches the edge of the world. I have tried multiple different codes and I'm doing something wrong because it never works. It usually says it doesn't recognize the X variable. Could someone show me an example of a code for this scrolling screen?
danpost danpost

2015/7/30

#
If you show the code you tried, we could probably show you where you went wrong. (read the link below the reply box on 'Posting code') For example code, you could download and look at the code to my Bee Quick (ImgScroll Demo) scenario.
Gh0sty Gh0sty

2015/7/30

#
So I copied your code for the ImgScroll Class perfectly. However in my Grass (in your game it was Welt) I tried to copy your scrolling code like so:

    public void act()
    {
        scroller.scroll(getWidth()/2-Slendy.getX(), getHeight()/2-Slendy.getY());
    }
I also copied this part
    public ImgScroll scroller;
    
    public Grass()
    {
        super(960, 600, 1, false); 
        scroller = new ImgScroll(this, new GreenfootImage("creepy grass"), 1920, 1200);
        prepare();
    }
The second part compiled fine, the first part however came up with this error:
non-static method getX() cannot be referenced from a static context
danpost danpost

2015/7/31

#
Gh0sty wrote...
The second part compiled fine, the first part however came up with this error: non-static method getX() cannot be referenced from a static context
That is because you are using the class name 'Slendy' and not the Slendy object created by 'new Slendy()'. Look what you replaced in my code with 'Slendy' and where it came from.
Gh0sty Gh0sty

2015/7/31

#
okay I fixed what you said, but now when I try to run the game this error message pops up in terminal:
java.lang.NullPointerException
	at Grass.act(Grass.java:37)
	at greenfoot.core.Simulation.actWorld(Simulation.java:600)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:535)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2015/7/31

#
You need to post the code to the Grass class from the beginning (so that the correct line number can be determined). The error occurs in the 'act' method, in line 37 of the class. But, it is impossible to say why until you post the code.
Gh0sty Gh0sty

2015/8/1

#
import greenfoot.*;

/**
 * Write a description of class Grass here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Grass extends World
{
    public ImgScroll scroller;
    private Slendy slendy;
    
    public Grass()
    {
        super(960, 600, 1, false); 
        scroller = new ImgScroll(this, new GreenfootImage("creepy grass"), 1920, 1200);
        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Girl girl = new Girl();
        addObject(girl, 15, 537);
        girl.setLocation(30, 518);
        Slendy slendy = new Slendy();
        addObject(slendy, 839, 155);
        slendy.setLocation(920, 60);
    }

    public void act()
    {
        scroller.scroll(getWidth()/2-slendy.getX(), getHeight()/2-slendy.getY());    
    }
}
that's the entire code
danpost danpost

2015/8/1

#
Your 'slendy' field declared on line 12 is never set to any value and line 37 tries to use 'getX' and 'getY' on it. You probably think that line 30 does so; but, that is not the case. Line 30 creates a local variable which is referenced in lines 31 and 32. Once the method has finished executing, that reference is lost. By changing line 30 to the following, you will refer to the field declared on line 12 instead of creating a new variable:
slendy = new Slendy();
Gh0sty Gh0sty

2015/8/2

#
Thank you so much for your assistance! Is there any way you know of to increase Java Heap Memory?
You need to login to post a reply.