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

2011/7/9

setBackground method

ianking ianking

2011/7/9

#
I'm trying to change the background image to a scenario dynamically - ie when a room variable changes value. This code compiles when placed in a maze subclass of the World, but I get a "non-static method cannot be referenced from a static context" error when I place the code in an actor subclass. Can anyone help / explain? int room = 1; setBackground(backgroundarray); backgroundarray is a string array which has been declared and populated in the maze subclass int room =1; maze.setBackground(maze.backgroundarray); gives me the error if I place it in an actor subclass.
kiarocks kiarocks

2011/7/9

#
maybe try the setWorld() method?
DonaldDuck DonaldDuck

2011/7/9

#
You need a reference to the World object. Try this.
((maze) getWorld()).setBackground(((maze) getWorld()).backgroundarray[room]);
ianking ianking

2011/7/9

#
Thanks, but although ((maze) getWorld()).setBackground(((maze) getWorld()).backgroundarray); compiles as does getWorld().setBackground (maze.backgroundarray); neither actually changes the background of the world.
davmac davmac

2011/7/10

#
Most likely, there is a logical problem in your code. Perhaps the setBackground() call is never executed; it is difficult to say for sure without seeing more of your code. The best thing to do is upload it (with source code!) and post a link to it here. On another note: it's a point of style, but class names in Java should really start with a capital letter - it should be "Maze", not "maze".
DonaldDuck DonaldDuck

2011/7/10

#
Your problem might be with your string array. You need to set the image with a greenfoot image, not a string. Perhaps getWorld().setBackground(new GreenfootImage(((maze) getWorld()).backgroundarray); will work?
DonaldDuck DonaldDuck

2011/7/10

#
Otherwise, what I do is make an array of greenfootImages and change the image by that. I'm not positive on the creation of the image array, but this should be right...
private static GreenfootImage[] backgroundarray = new GreenfootImage[100];

public myActor()
{
    if(backgroundarray[1] == null)
    {
        backgroundarray[1] = new GreenfootImage("room1.png");
        backgroundarray[2] = new GreenfootImage("room2.png");
        //Load all images here
    }
}
This way, you can use your getWorld().setBackground(backgroundarray); from the actor object to select the worlds image from an array of images loaded in the constructor, depending on the room.
davmac davmac

2011/7/10

#
DonaldDuck - World has two setBackground methods, one which takes a String parameter and the other takes a GreenfootImage; the problem isn't that a String is being used.
ianking ianking

2011/7/10

#
I've changed the array to a GreenfootImage but still no joy. Probably a logical error but if anyone can help, here is the scenario: http://greenfootgallery.org/scenarios/3285
davmac davmac

2011/7/10

#
Well, it works correctly in the prepare() method of the Maze class. There you set room = 2, and then set the background to backgroundarray, which appears to be working. The Room class also sets the world background, as:
        getWorld().setBackground(new GreenfootImage(((Maze) getWorld()).backgroundarray[7]));
This however is never executed, because no Room objects are ever created nor added to the world.
ianking ianking

2011/7/10

#
Aha - makes sense now thanks. Now to get the rest of the game to work.....
You need to login to post a reply.