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

2016/3/29

If statements in world class?

grog2 grog2

2016/3/29

#
I want the world class to execute a method if a variable is 0, then a different method once it reaches four, but it only executes the first method (I checked in another class, the variable definitely reaches 4) Help?
if (Enemies.kills == 0)
        {
            t1();
        }
        if (Enemies.kills == 4)
        {
            t2();
        }
Enemies.kills starts at 0, so t1(); executes, but the second if and t2(); don't. This is similar to the problem I had before, but I changed the order and the location of the code to different classes, but it still didn't work :/
danpost danpost

2016/3/29

#
grog2 wrote...
I want the world class to execute a method
All you gave was a snippet of code. Please show the entire method.
grog2 grog2

2016/3/29

#
public void t1() { addObject(new Tower(),0,0); } public void t2() { addObject(new ChopShadow(),400,300); addObject(new Chop(),400,300); }
Sorry, the methods just add objects, I didn't think they were important.
grog2 grog2

2016/3/29

#
Oops, I pressed quote by accident :)
danpost danpost

2016/3/29

#
grog2 wrote...
Sorry, the methods just add objects, I didn't think they were important.
Sorry, I meant the method the snippet of code is contained within.
Super_Hippo Super_Hippo

2016/3/29

#
I think he meant the method in which the code you showed is in. Btw, you are able to edit your last post.
grog2 grog2

2016/3/29

#
public class MyWorld extends World
{
    public void t1()
    {
        addObject(new Tower(),0,0);
    }
    public void t2()
    {
        addObject(new ChopShadow(),400,300);
        addObject(new Chop(),400,300);
    }
public MyWorld()
    {   
        super(800, 600, 1); 
if (Enemies.kills == 0)
        {
            t1();
        }
        if (Enemies.kills == 4)
        {
            t2();
        }
}
Is this what you mean?
danpost danpost

2016/3/29

#
grog2 wrote...
< Code Omitted > Is this what you mean?
Yes. It is no wonder the second part does not work. The constructor of the class (the method the snippet is in) is only executed once, when the world is created. To run code repeatedly, you need to place it in the act method or a method it calls. You can add a 'public void act()' method to a world class just like it is in an Actor subclass.
grog2 grog2

2016/3/29

#
Right, but now the methods execute repeatedly, do I need to use a while loop that executes once, or is there a simpler way to stop it?
danpost danpost

2016/3/29

#
You can add a tracking field to control the execution of the snippet:
// instance field in world class
private int enemyKillsDetected;

// controlling conditional
if (enemyKillsDetected < Enemies.kills)
{
    enemyKillsDetected = Enemies.kills;
    // snippet goes here
}
Being 'kills' in the Enemies class is 'static', you should set its value to zero in your world constructor:
// where your snippet of code was
Enemies.kills = 0;
otherwise, its value will not change between resets.
grog2 grog2

2016/3/29

#
Sorry, what did you mean by "controlling conditional"?
danpost danpost

2016/3/29

#
grog2 wrote...
Sorry, what did you mean by "controlling conditional"?
The requirement to execute the code. The true/false interrogative that limits when the snippet is executed. The 'if' clause.
grog2 grog2

2016/3/29

#
Ok, I got it, thanks so much again!
You need to login to post a reply.