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

2016/3/7

What is wrong with my code?

1
2
Bassusour Bassusour

2016/3/7

#
I did this exact same code with one of my other classes, with a diffrent method name and diffrent images, and that works just fine. So, what is wrong with my code?
     /** Denne metode laver animationen
         * Hvis man ikke bevæger sig, executer den ikke
       */
       public void animation()
       {
           String knap = Greenfoot.getKey();
        {
        if ()
        {
           if (getImage() == spredteBen) 
           {
           setImage(samledeBen);
           }
          else
           {
           setImage(spredteBen);
           }
        }
      }
    }
and my variables are:
private GreenfootImage spredteBen = new GreenfootImage("Gå 1.png");
     private GreenfootImage samledeBen = new GreenfootImage("Gå 2.png");
And in my contructor, the code is the following:
setImage(spredteBen);
the problem with my code is, that is doesn't execute, and is stuck with one image.
danpost danpost

2016/3/7

#
Bassusour wrote...
I did this exact same code with one of my other classes, with a diffrent method name and diffrent images, and that works just fine.
How can it work fine when you should be getting an error with the code given??? The 'if' statement on line 10 does not have a boolean condition -- no true/false value or expression is given between the round parenthesis. Also, line 6 is, as coded above, irrelevant as 'knap' is not used later in the method (or, maybe it is supposed to be used within the condition -- if so, then the image will change once per keystroke, which is not really animation). You should explain when you want the image to change during each of the following situations: * at the instant the key is pressed * while the key is down * at the instant the key is released * while the key is up
Bassusour Bassusour

2016/3/8

#
I'm sorry i gave you the wrong code for the method. This was in between the parantheses
    /** Denne metode laver animationen
         * Hvis man ikke bevæger sig, executer den ikke
       */
       public void animation()
       {
           String knap = Greenfoot.getKey();
        if ("left".equals(knap) || "right".equals(knap) || "up".equals(knap) || "down".equals(knap))
        {
           if (getImage() == spredteBen) 
           {
           setImage(samledeBen);
           }
          else
           {
           setImage(spredteBen);
           }
      }
    }
Super_Hippo Super_Hippo

2016/3/8

#
Do you call the 'animation' method from the act-method? So, something like
public void act()
{
    animation();
}
Just tested it. For me, it works.
Bassusour Bassusour

2016/3/8

#
Yes i do. Just talked about this with my teacher, and he says it is a bug in greenfoot
Super_Hippo Super_Hippo

2016/3/8

#
Show the whole class code and we might find a mistake.
Bassusour Bassusour

2016/3/8

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

/**  
 * Denne klasse er Player_2
 */
 public class Player_2 extends Player_1
 {
     private GreenfootImage billede_1 = new GreenfootImage("luigi_patrick_kopi.png");
     private GreenfootImage billede_2 = new GreenfootImage("luigi_patrick.png");
     
     private int ormeSpist;
    public Player_2()
     {
         setImage(billede_1);
         ormeSpist = 0;
     }
     public void act()
     {
     lookForWorm();
     animation_2();
     bevægelse();
     gameOver2();
     
     getWorld().showText("Player 2:   Orme spist : " + ormeSpist,120,50);
    } 
    /** Denne metode gør dig i stand til at rykke med piletasterne
    */
    public void bevægelse()
    {
    if(Greenfoot.isKeyDown("d"))
    {
        setLocation(getX()+3, getY());
    }
    if(Greenfoot.isKeyDown("a")) 
    {
        setLocation(getX()-3, getY());
    }
    if(Greenfoot.isKeyDown("s")) 
    {
        setLocation(getX(), getY()+3);
    }
    if(Greenfoot.isKeyDown("w")) 
    {
        setLocation(getX(), getY()-3);
    }
    }
        /** Denne metode laver animationen
         * Hvis man ikke bevæger sig, executer den ikke
       */
       public void animation_2()
       {
           String knap_2 = Greenfoot.getKey();
        {
        if ("left".equals(knap_2) || "right".equals(knap_2) || "up".equals(knap_2) || "down".equals(knap_2))
        {
           if (getImage() == billede_1) 
           {
           setImage(billede_2);
           }
          else
           {
           setImage(billede_1);
           }
        }
      }
    }
    /**
       * Check whether we have stumbled upon a worm.
       * If we have, eat it. If not, do nothing.
       */
       public void gameOver2()
       {
           if ( ormeSpist >= 8)
           {
               Greenfoot.stop();
               Greenfoot.playSound("fanfare.wav");
               getWorld().showText("Player 2 WON!",280,280);
               Greenfoot.stop();
            }  
    }
    /**
       * Check whether we have stumbled upon a worm.
       * If we have, eat it. If not, do nothing.
       */
       public void lookForWorm()
       {
           if ( canSee(Worm.class) )
           {
               eat(Worm.class);
               Greenfoot.playSound("slurp.wav");
               
               ormeSpist = ormeSpist + 1;
            }  
    }
    }
       
Super_Hippo Super_Hippo

2016/3/8

#
Why is Player_2 a subclass of Player_1? I don't think that this makes any sense.
Bassusour Bassusour

2016/3/8

#
Yeah i just changed that, so it isn't a subclass of Player_1 anymore, but a subclass of animals. The problem is still there tho
Super_Hippo Super_Hippo

2016/3/8

#
Is the Player_1 code or the Player_2 code working? And what is the Player_1 code?
Bassusour Bassusour

2016/3/8

#
The Player_1 code is working fine. And here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)  

/**  
 * Denne klasse er Player 1.  
 */
 public class Player_1 extends Animal
 {
     private GreenfootImage spredteBen = new GreenfootImage("Gå 1.png");
     private GreenfootImage samledeBen = new GreenfootImage("Gå 2.png");
     private int xyz;
     
     /**
     Jeg mangler en loop-metode (til hurtig hummer), at gå fremaf, en power-up så krabber kan spise hummere, krabber løber hurtigere efter tid, Hummerne retter sig efter en player en gang i mellem, Hummer løber hurtigere jo flere orme der er 
     spist. 
     */
     
     private int ormeSpist;
    public Player_1()
     {
         setImage(spredteBen);
         
         ormeSpist = 0;
     }
     public void act()
     {
     lookForWorm();
     animation();
     bevægelse();
     gameOver();
     getWorld().showText("Player 1:   Orme spist : " + ormeSpist,120,30);
    } 
    public int getOrmeSpist()
    {
    return ormeSpist;
    }
    /** Denne metode gør dig i stand til at rykke med piletasterne
     */
    public void bevægelse()
    {
    if(  Greenfoot.isKeyDown("right"))
    {
        setLocation(getX()+3, getY());
    }
    if(Greenfoot.isKeyDown("left")) 
    {
        setLocation(getX()-3, getY());
    }
    if(Greenfoot.isKeyDown("down")) 
    {
        setLocation(getX(), getY()+3);
    }
    if(Greenfoot.isKeyDown("up")) 
    {
        setLocation(getX(), getY()-3);
    }
    }
    /**
       * Denne metode sørger at slutte spillet når man vinder
       * Man vinder ved at spise 8 orme
       */
       public void gameOver()
       {
           if ( ormeSpist >= 8)
           {
               Greenfoot.stop();
               Greenfoot.playSound("fanfare.wav");
               getWorld().showText("Player 1 WON!",280,280);
            }  
    }
        /** Denne metode laver animationen
         * Hvis man ikke bevæger sig, executer den ikke
       */
       public void animation()
       {
           String knap = Greenfoot.getKey();
        if ("left".equals(knap) || "right".equals(knap) || "up".equals(knap) || "down".equals(knap))
        {
           if (getImage() == spredteBen) 
           {
           setImage(samledeBen);
           }
          else
           {
           setImage(spredteBen);
           }
      }
    }
    /**
       * Denne metode gør at du spiser ormene. Den tæller også ormene du har spist. 
       */
       public void lookForWorm()
       {
           if ( canSee(Worm.class) )
           {
               eat(Worm.class);
               Greenfoot.playSound("slurp.wav");
               ormeSpist = ormeSpist+1;
            }  
    }
    }
       
danpost danpost

2016/3/8

#
Due to the nature of the way the 'getKey' method works, it can only be feasibly called once per act cycle. This means you cannot have both a Player_1 and a Player_2 object in the world which both use the method. It is best to use it only for data entry type structures or just in your world subclass (where there will always only be one world active at a time). Using the 'isKeyDown' method is what is needed in your 'animation_#' methods.
Bassusour Bassusour

2016/3/8

#
Thanks for all the replies. But when i changed to using the isKeyDown it is still stuck in one position
danpost danpost

2016/3/8

#
To use the 'isKeyDown' like a 'getKey', you need to introduce a boolean field to track the state of the key or keys. To track one key, let us say the "space" key::
// instance field
private boolean spaceDown;

// tracking the state of the key
if (spaceDown != Greenfoot.isKeyDown("space")) // if change in state
{
    spaceDown = !spaceDown; // change tracking field
    if (spaceDown)
    {
        // actions when key is pressed
    }
    // optional else
    else
    {
        // actions when key is released
    }
}
// the following is for actions between changes in the state of the key
if (spaceDown)
{
    // actions while key is down
}
else
{
    // actions while key is up
}
danpost danpost

2016/3/8

#
By your Player_1 code, it appears you have 8-way movement and the image is to change any time an arrow key is released. Is this really what you are wanting? If not, be specific with what you are really wanting.
There are more replies on the next page.
1
2