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

2016/9/16

Player onstantly adding without eating and help with other ideas

1
2
Boboy18 Boboy18

2016/9/16

#
Hi, my Player class is making points constantly add without collecting anything. Heres the code. I thought it will only add if the burger is removed. Turns out it doesn't. -_-
 public void eating()
    {
       Actor Burger;
       Burger = getOneObjectAtOffset(0,0,Burger.class);
    
       if (Burger != null);   
       {
            World world;
            world = getWorld();
            world.removeObject(Burger);
            counter.add(2);
       }
       
       if(counter.getValue() >= 50)
       {
         gameOver();  
       }
    }
danpost danpost

2016/9/16

#
It is not a good idea to name variables with the same name as one of your classes. Try:
Actor burger;
burger = getOneObjectAtOffset(0,0,Burger.class);
if (burger != null)
By convention, variable and field names should begin with lowercase letters and class names with uppercase letters.
Boboy18 Boboy18

2016/9/16

#
Ok Danpost, I still have the problem where it constantly adds without eating anything. Did you want me to post all the code to check if the problem is elsewhere?
danpost danpost

2016/9/16

#
Boboy18 wrote...
Ok Danpost, I still have the problem where it constantly adds without eating anything. Did you want me to post all the code to check if the problem is elsewhere?
Yes -- it might help. As a help, the method above can be more simply written as follows:
public void eating()
{
    if (isTouching(Burger.class))
    {
        removeTouching(Burger.class);
        counter.add(2);
        if (counter.getValue() >= 50)
        {
            gameOver();
        }
    }
}
Boboy18 Boboy18

2016/9/16

#
Thank you so much, it worked. I have another idea, I haven't started it yet because I don't know how to start it. The idea is a Selection menu. In my game I'm going to have only 1 level but the user can choose what speed he/she wants the enemies. I've got the idea but don't know how to code it. (I'm learning as I progress)
danpost danpost

2016/9/16

#
Boboy18 wrote...
Thank you so much, it worked.
I was kind of surprised, reading this! It was not intended to correct your issue. However, looking back at your original code I noticed what the problem was. At the end of the 'if' statement on line 6, you have placed a semi-colon ( ; ). This behaves like an no-operation statement in itself. Therefore, what was happening was the 'no-op' was executing (or not) for the 'if' statement and the block of code following was being executed because those lines were outside the scope of the 'if' block (which is not enclosed in brackets, but is the single unbracketed statement).
I have another idea, I haven't started it yet because I don't know how to start it. The idea is a Selection menu. In my game I'm going to have only 1 level but the user can choose what speed he/she wants the enemies. I've got the idea but don't know how to code it. (I'm learning as I progress)
You will need to explain, in detail, how you want your scenario to begin. This will not only include what backgrounds you will be using and when (and what the default image is for the class), but also how you want the selection process to happen. Your current world class code(s) will also be required; and possibly the class of the enemies as well. Best would be to show some attempt at coding/implementing the selection menu to back up your explanation. This makes it much easier for us to understand exactly what you are trying to do and to give you more appropriate advice. It also helps to benefit you in your learning process.
Super_Hippo Super_Hippo

2016/9/16

#
In my Insect War game, I made a selection menu too. When clicking on "Start Game", it will create the world based on the parameters it got with the selection which was made. If you only want one parameter to be alternated (the speed), you could also make some buttons (each with a different speed variable) and when one is clicked, the new game world is based on this parameter. You can pass a parameter if you write an constructor of the game world which needs a parameter. Like this:
private int speed = 0;

public GameWorld(int spd)
{
    super(/*...*/);
    speed = spd;
    //....
}
To create a new world:
//instead of
Greenfoot.setWorld(new GameWorld());
//you can use
Greenfoot.setWorld(new GameWorld(speed));
//with 'speed' being the variable in the button class
//or in the selection world class (see the text above for the two possibilities)
Boboy18 Boboy18

2016/9/17

#
I will give the selection screen a go but will take me maybe 2-3 days due to exams at school.
Super_Hippo wrote...
In my Insect War game, I made a selection menu too. When clicking on "Start Game", it will create the world based on the parameters it got with the selection which was made. If you only want one parameter to be alternated (the speed), you could also make some buttons (each with a different speed variable) and when one is clicked, the new game world is based on this parameter. You can pass a parameter if you write an constructor of the game world which needs a parameter. Like this:
private int speed = 0;

public GameWorld(int spd)
{
    super(/*...*/);
    speed = spd;
    //....
}
To create a new world:
//instead of
Greenfoot.setWorld(new GameWorld());
//you can use
Greenfoot.setWorld(new GameWorld(speed));
//with 'speed' being the variable in the button class
//or in the selection world class (see the text above for the two possibilities)
Super_Hippo will this code affect all the classes within a world? I only want the one class to have different speeds.
danpost danpost

2016/9/17

#
Boboy18 wrote...
will this code affect all the classes within a world? I only want the one class to have different speeds.
As is, it does not affect any class. All codes given so far are for the value setting of the field 'speed' in the GameWorld class. You have yet to make use of that field in any class. You do not need to have the field declared in the GameWorld class; it can be in the class that uses the field. You will still need to set the value of that field from the constructor of the GameWorld class. Whatever class it is in, you can have this:
private static int speed;

public static void setSpeed(int spd)
{
    speed = spd;
}
Then change line 6 in the constructor to:
Enemy.setSpeed(spd); // replace 'Enemy' with the name of the class you placed the field in
Now, you can make use of the field within that same class to control the speed of its actors.
Boboy18 Boboy18

2016/9/21

#
Hi, sorry for delay (School stuff) I have try to attempt to do the selection screen. So far I have got the buttons(Easy, Medium and hard buttons) to change worlds when clicked. The code I have used is here.
    public void act() 
    {
        ChangeScreen();
    }    
    
    private void ChangeScreen()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new MyWorld());
        }
    }  
The code for changing the speed for my enemies, I still struggle to understand. I have not put any code on any of my 2 worlds scripts. Here is the code for the enemy if it helps.
public void act() 
    {
        move(9);
        randomTurn();
        turnAtEdge();
        eating();
    }  
    
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100)<10)
        {
            turn(Greenfoot.getRandomNumber(40)-20);
        }
    }
    
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(8);
        }
    }
    
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    
    public void eating()
    {
       if (isTouching(Player.class))
       {
         removeTouching(Player.class);
         Greenfoot.stop();
       }
    }
}
danpost danpost

2016/9/21

#
Line 3 has a literal '9' in it for the moving distance. You cannot use a literal or a constant value for something you want to be variable (changing). That is where the field come into play. With the 'speed' field, you can use:
move(speed);
and you can assign any integer value to the 'speed' field at any time. Therefore, the button code can be something like this:
if (Greenfoot.mouseClicked(this))
{
    Greenfoot.setWorld(new MyWorld());
    Enemy.setSpeed(12); // a different value for each button
}
Boboy18 Boboy18

2016/9/21

#
Ok I have fixed the enemy class, but now in my button class doesn't work. Here's the code.
private static int speed;
    
    public static void setSpeed(int spd)
    {
        speed = spd;
    }
    
    
    public void act() 
    {
        ChangeScreen();
    }    
    
    private void ChangeScreen()
    {
        if(Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new MyWorld());
            Enemy.setSpeed(12);
        }
    }
danpost danpost

2016/9/21

#
Boboy18 wrote...
Ok I have fixed the enemy class, but now in my button class doesn't work. < Code Omitted>
Lines 1 through 6 should be in your Enemy class -- not in the Button class.
Boboy18 Boboy18

2016/9/22

#
YAY It works now. I will post more ideas if I have any after. Thank you again.
Boboy18 Boboy18

2016/9/23

#
I got another idea that links with speed variable. I want that after every 100 points collected, the speed of the enemies would increase. So far I've got this in my player class.(What the user is going to control)
public void IncreaseSpeed()
    {
        if (counter.getValue() == 100)
            {
                speed.add(5);
            }
    }
I believe that this is the right idea but this doesn't work because it says this "cannot find symbol - variable speed". I don't know how to make my enemy class link with my player class.
There are more replies on the next page.
1
2