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

2014/9/20

Images according to Levels

1
2
Mariachi Mariachi

2014/9/20

#
Hello, I want to change the image of the actors according to the levels how can I do that ? I try this but didn't work:
    

private GreenfootImage pike1 = new GreenfootImage("Pike1.gif");
private GreenfootImage water = new GreenfootImage("Water.gif");

public void image()
    {
        if (Level == 1 || Level == 2 || Level == 3)
        {
            setImage(pike1);
        }
        else if (Level == 4 || Level == 5 || Level == 6)
        {
            setImage(water);
        }
    }
danpost danpost

2014/9/20

#
If what you had above did not work, then it must be either you are not calling the method from the constructor of this Actor subclass or your Level field is not being accessed properly (presuming that the images are properly located in the 'images' folder of that of the scenario).
Mariachi Mariachi

2014/9/20

#
The images is located properly but I don't know if the Level field is accessed. How can I check that ?
Mariachi Mariachi

2014/9/20

#
( in my scenario, Level is also the name of the World sub-class )
danpost danpost

2014/9/20

#
To avoid compiler and human confusion, do not name two different things with the same name -- like 'Level' for a class and an int field. Conventionally, fields would start with lowercase characters, anyway. You can check for what the field holds by using a statement similar to the following:
System.out.println(""+Level);
// or
System.out.println(""+Level.toString());
Place the line as the first line in your 'void image' method.
Mariachi Mariachi

2014/9/20

#
Thanks but it doesn't work: in the first case: a terminal window opens with 0 in the second case: an error "int cannot be dereferenced"
danpost danpost

2014/9/20

#
The second case was in case the compiler what trying to look for the class called Level. The first case was to pick up the value of the Level field, if that is what the compiler was looking at. Since your int field was being accessed and returning a value of zero, then you can now try and figure out why the value is zero instead of an actual level number. The fact that you are directly accessing level (by default, you are using 'this.Level') in an actor class, maybe you are not accessing the correct 'Level' field (that is, you have another one in your world class that you should be accessing instead) or the Level field is not being set to a valid Level number to begin with or at all.
Mariachi Mariachi

2014/9/20

#
Sorry it's a bit difficult, I didn't understant what I have to do ...
danpost danpost

2014/9/20

#
Well, where is the Level field that contains the actual level number? If you right-click on the world background and select 'Inspect' from the options, do you get a Level field with a value? and if so, what is its value? You obviously have a Level field in the class of the actor. How are you controlling its value? How is it declared in that actor class?
Mariachi Mariachi

2014/9/21

#
So, when I inspect the World (named Level), I can see the "public static int" from the level (it starts with 0). In the actor, I changed "Level" to "level" and nothing changed, Here is the code of my actor:
public class Pike1 extends Pikes
{ 
    public static int level;
    
    private GreenfootImage pike1 = new GreenfootImage("Pike1.gif");
    private GreenfootImage water = new GreenfootImage("Water.gif");

...

public void image()
    {
        System.out.println(""+level);
        if (level == 1 || level == 2 || level == 3)
        {
            setImage(pike1);
        }
        else if (level == 4 || level == 5 || level == 6)
        {
            setImage(water);
        }
    }
Super_Hippo Super_Hippo

2014/9/21

#
Could it be that the 'level' variable stays at 0 all the time? Do you increase it somewhere?
Mariachi Mariachi

2014/9/21

#
When I change the level (after touching the end of the level) then I inspect the new level, the public static int change to 1 so it doesn't stay at 0...
Super_Hippo Super_Hippo

2014/9/21

#
Do you call the method 'image' after you changed the level variable?
Mariachi Mariachi

2014/9/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pike1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pike1 extends Pikes
{ 
    public static int level;
    
    private GreenfootImage pike1 = new GreenfootImage("Pike1.gif");
    private GreenfootImage water = new GreenfootImage("Water.gif");
     
    /**
     * Act - do whatever the Pike1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        onPike();
        image();
    }    

    // This is the gameplay...
    public void onPike()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor player = getOneIntersectingObject (Player.class);
        if(player != null)
        {
            removePlayerByPikes();
        }
    }
    
    public void image()
    {
        System.out.println(""+level);
        if (level == 1 || level == 2 || level == 3)
        {
            setImage(pike1);
        }
        else if (level == 4 || level == 5 || level == 6)
        {
            setImage(water);
        }
    }
}
This is all the code... Does it answer your question ?
Super_Hippo Super_Hippo

2014/9/21

#
You call the method every act cycle. So the level is 1 and the image isn't pike1 or what is the problem right now?
There are more replies on the next page.
1
2