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

2014/3/15

A problem with spawning

Alex89 Alex89

2014/3/15

#
Hi everyone! The player should be able to spawn three hallucinations and one wall. There is no problem in spawning hallucinations but I just don't get why I can't spawn a wall. The code is literally the same except for the classes used. There's another problem. I tried around a little to fix this problem but I could only spawn the wall after all three hallucinations have been spawned and it didn't spawn in front of the player. It ignored the player's rotation. I welcome any help. Thanks in advance.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Sentry extends Actor
{
    private int halluNumber=0;
    private int wallNumber=0;
    private int halluTimer=10;
    private char key;

    public void act()
    {
        movement();
        keyPressed();
    } 

    public void movement()
    {
        /* Sentry's movement.
         * Controls via Keyboard. (Up, Down, Left, Right)
         * If there is an Hallucination in the game, the Sentry can't move during that time.
         */
        if(getWorld().getObjects(Hallucination.class).size() == 0)
        {
            if(Greenfoot.isKeyDown("up"))
            {
                move(2);
            }

            if(Greenfoot.isKeyDown("down"))
            {
                move(-2);
            }

            if(Greenfoot.isKeyDown("left"))
            {
                turn(-2);
            }

            if(Greenfoot.isKeyDown("right"))
            {
                turn(2);
            }
        }
    }
    
    public void keyPressed()
    {
        if("h".equals(Greenfoot.getKey())) //Declares the char variable key to "h" in case the player pressed the h-key on the keyboard.
        {
            key = 'h';
            return;
        }
    
        if("w".equals(Greenfoot.getKey())) //Declares the char variable key to "w" in case the player pressed the w-key on the keyboard.
        {
            key = 'w';
            return;
        }
        
        switch(key)
        {
             case 'h':      //In case "h" was pressed the spawnHallu method gets called up.
                spawnHallu();
                key = '0';  //Sets the char variable key to 0 in case the player wants to create another hallucination or wall.
                break;      //Else the programme is going to create hallucinations over and over again until the char variable key changes it's value.
             
             case 'w':      //In case "w" was pressed the spawnWall method gets called up.
                spawnWall();
                key = '0';  //Sets the char variable key to 0 in case the player wants to create an hallucination.
                break;      //Else the programme tries to create walls over and over again until the char variable key changes it's value.
        }
    }
    
    public void spawnHallu()
    {
        if(halluNumber<3)
        {
             Hallucination hallucination = new Hallucination(); //Creates the Hallucination.
             getWorld().addObject(hallucination, getX(), getY()); //Adds the Hallucination to the world.
             hallucination.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
             hallucination.move(40); //Adds the Hallucination 40 pixel in front of the player.
             halluNumber++;
             return;
        }
    }
    
    public void spawnWall()
    {
        if(wallNumber<1)
        {
             Wall wall = new Wall(); //Creates the Wall.
             getWorld().addObject(wall, getX(), getY()); //Adds the Wall to the world.
             wall.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
             wall.move(40); //Adds the Wall 40 pixel in front of the player.
             wallNumber++;
             return;
        }
    }
}
danpost danpost

2014/3/15

#
One of the main problems is the use of Greenfoot.getKey() in both lines 49 and 55. 'getKey' retrieves a keystroke from the keyboard buffer. Once a key is retrieved from the buffer, the buffer updates its pointer to the next keystroke location in the buffer. Therefore, when you call it AGAIN on line 55, you will lose the original key returned from the first call to it. Only call it once and save the returned value in a String variable; then, use the variable to compare with the multiple possibilities:
String key = Greenfoot.getKey();
if ("w".equals(key)) ...
if ("h".equals(key)) ...
Alex89 Alex89

2014/3/16

#
Thanks for the help but with the JDK Greenfoot is using I can't use a string (in this case the string "key") as an operator for switch-case. I tried to use an int variable that sets it's value to 1 if h is pressed and 2 if w is pressed. But now there is nothing that spawns if I press those keys.
    public void keyPressed()
    {
        if("h".equals(key)) //Declares the int variable keypressed to 1 in case the player pressed the h-key on the keyboard.
        {
            keypressed = 1;
            return;
        }
    
        if("w".equals(key)) //Declares the int variable keypressed to 2 in case the player pressed the w-key on the keyboard.
        {
            keypressed = 2;
            return;
        }
        
        switch(keypressed)
        {
             case 1:      //In case "h" was pressed the spawnHallu method gets called up.
                spawnHallu();
                keypressed = 0;  //Sets the int variable keypressed to 0 in case the player wants to create another hallucination or wall.
                break;      //Else the programme is going to create hallucinations over and over again until the char variable key changes it's value.
             
             case 2:      //In case "w" was pressed the spawnWall method gets called up.
                spawnWall();
                keypressed = 0;  //Sets the int variable keypressed to 0 in case the player wants to create an hallucination.
                break;      //Else the programme tries to create walls over and over again until the char variable key changes it's value.
        }
    }
danpost danpost

2014/3/16

#
It does not help that your two 'return' statements prevent everything from line 15 on from being executed (unless it is not a key you are checking for).
Alex89 Alex89

2014/3/17

#
Hm so what to do to solve this problem? If I leave the 'return' statements out the programme spawns the three hallucinations instantly. Still there is no wall that will be spawned if I press the 'w' key.
danpost danpost

2014/3/17

#
What does your Sentry class code look like now?
Alex89 Alex89

2014/3/20

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


public class Sentry extends Actor
{
    private int halluNumber=0;
    private int wallNumber=0;
    private int halluTimer=10;
    private char key;

    public void act()
    {
        movement();
        keyPressed();
    } 

    public void movement()
    {
        /* Sentry's movement.
         * Controls via Keyboard. (Up, Down, Left, Right)
         * If there is an Hallucination in the game, the Sentry can't move during that time.
         */
        if(getWorld().getObjects(Hallucination.class).size() == 0)
        {
            if(Greenfoot.isKeyDown("up"))
            {
                move(2);
            }

            if(Greenfoot.isKeyDown("down"))
            {
                move(-2);
            }

            if(Greenfoot.isKeyDown("left"))
            {
                turn(-2);
            }

            if(Greenfoot.isKeyDown("right"))
            {
                turn(2);
            }
        }
    }
    
    public void keyPressed()
    {
        if("h".equals(key)) //Declares the int variable keypressed to 1 in case the player pressed the h-key on the keyboard.
        {
            keypressed = 1;
            return;
        }
    
        if("w".equals(key)) //Declares the int variable keypressed to 2 in case the player pressed the w-key on the keyboard.
        {
            keypressed = 2;
            return;
        }
        
        switch(keypressed)
        {
             case 1:      //In case "h" was pressed the spawnHallu method gets called up.
                spawnHallu();
                keypressed = 0;  //Sets the int variable keypressed to 0 in case the player wants to create another hallucination or wall.
                break;      //Else the programme is going to create hallucinations over and over again until the char variable key changes it's value.
             
             case 2:      //In case "w" was pressed the spawnWall method gets called up.
                spawnWall();
                keypressed = 0;  //Sets the int variable keypressed to 0 in case the player wants to create an hallucination.
                break;      //Else the programme tries to create walls over and over again until the char variable key changes it's value.
        }
    }
    
    public void spawnHallu()
    {
        if(halluNumber<3)
        {
             Hallucination hallucination = new Hallucination(); //Creates the Hallucination.
             getWorld().addObject(hallucination, getX(), getY()); //Adds the Hallucination to the world.
             hallucination.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
             hallucination.move(40); //Adds the Hallucination 40 pixel in front of the player.
             halluNumber++;
             return;
        }
    }
    
    public void spawnWall()
    {
        if(wallNumber<1)
        {
             Wall wall = new Wall(); //Creates the Wall.
             getWorld().addObject(wall, getX(), getY()); //Adds the Wall to the world.
             wall.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
             wall.move(40); //Adds the Wall 40 pixel in front of the player.
             wallNumber++;
             return;
        }
    }
}
danpost danpost

2014/3/20

#
Alex89 wrote...
Thanks for the help but with the JDK Greenfoot is using I can't use a string (in this case the string "key") as an operator for switch-case.
Where are you using a 'switch' statement? I know that I did not mention anything about using a switch statement. If you really want to use a 'switch' statement, you could use something like:
switch ("wh".indexOf(key))
which uses an int as the switch argument ('0' for "w" and '1' for "h"). I do not see, any more, where you are using 'getKey' in your code. What happened to it? Please refer back to my first post in this discussion.
Alex89 Alex89

2014/3/23

#
I'm sorry I did something wrong in my last post. This is how the class looks like right now. The switch argument starts at line 65. getKey is used in line 53 and 59.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * @WellProductions 
 * @0.0.1
 */
public class Sentry extends Creatures
{
    private int halluNumber=0;
    private int wallNumber=0;
    private int halluTimer=10;
    private String key = Greenfoot.getKey();
    private int keypressed=0;

    public void act()
    {
        movement();
        keyPressed();
    } 

    public void movement()
    {
        /* Sentry's movement.
         * Controls via Keyboard. (Up, Down, Left, Right)
         * If there is an Hallucination in the game, the Sentry can't move during that time.
         */
        if(getWorld().getObjects(Hallucination.class).size() == 0)
        {
            if(Greenfoot.isKeyDown("up"))
            {
                move(2);
            }

            if(Greenfoot.isKeyDown("down"))
            {
                move(-2);
            }

            if(Greenfoot.isKeyDown("left"))
            {
                turn(-2);
            }

            if(Greenfoot.isKeyDown("right"))
            {
                turn(2);
            }
        }
    }
    
    public void keyPressed()
    {
        if("h".equals(key)) //Declares the int variable keypressed to 1 in case the player pressed the h-key on the keyboard.
        {
            keypressed = 1;
            //return;
        }
    
        if("w".equals(key)) //Declares the int variable keypressed to 2 in case the player pressed the w-key on the keyboard.
        {
            keypressed = 2;
            //return;
        }
        
        switch(keypressed)
        {
             case 1:      //In case "h" was pressed the spawnHallu method gets called up.
                spawnHallu();
                keypressed = 0;  //Sets the int variable keypressed to 0 in case the player wants to create another hallucination or wall.
                break;      //Else the programme is going to create hallucinations over and over again until the char variable key changes it's value.
             
             case 2:      //In case "w" was pressed the spawnWall method gets called up.
                spawnWall();
                keypressed = 0;  //Sets the int variable keypressed to 0 in case the player wants to create an hallucination.
                break;      //Else the programme tries to create walls over and over again until the char variable key changes it's value.
        }
    }
    
    public void spawnHallu()
    {
        if(halluNumber<3)
        {
             Hallucination hallucination = new Hallucination(); //Creates the Hallucination.
             getWorld().addObject(hallucination, getX(), getY()); //Adds the Hallucination to the world.
             hallucination.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
             hallucination.move(40); //Adds the Hallucination 40 pixel in front of the player.
             halluNumber++;
             return;
        }
    }
    
    public void spawnWall()
    {
        if(wallNumber<1)
        {
            Wall wall = new Wall(); //Creates the Wall.
            getWorld().addObject(wall, getX(), getY()); //Adds the Wall to the world.
            wall.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
            wall.move(40); //Adds the Wall 40 pixel in front of the player.
            wallNumber++;
            return;
        }
    }
}
danpost danpost

2014/3/23

#
You are using 'getKey' in a field declaration statement. By doing so, the value of 'key' will be set the moment you create a Sentry, and will not change thereafter. You do not need 'key' anywhere except in the 'keyPressed' method and since it is not anything that needs held on to, it should be declared as a local field within the method. Move line 12 to before line 53 and remove the 'private' modifier.
Alex89 Alex89

2014/3/24

#
Thank you very much danpost! The problem is solved now. :)
You need to login to post a reply.