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

2018/2/26

Moving character

Maus2525 Maus2525

2018/2/26

#
I tried to make the actor Alien move and when he encounters any planets, a panel appears with questions. The problem occurs during the collision, the alien must stop when he touches the planet and he mustn't move until he chooses the right answer displayed on the panel (which appears each time the actor touches a planet). When the right answer is chosen, the panel disappears. In my situation the alien keeps moving across the screen.
import greenfoot.*;

public class Alien extends Actor
{
    boolean sw_Mercur = false;
    boolean sw_Venus = false;

    public void act() 
    {

        /*
         * If the alien isn't touching any planet, it moves.
         */
        if (isTouching(Mercur.class) == false)     		move(5);
        else if (isTouching(Venus.class) == false)      move(5);

        /*
         * If the alien touches an already visited planet, he can continue moving.
         */
        if (isTouching(Mercur.class) == true && sw_Mercur == true)     		move(5);
        else if (isTouching(Venus.class) == true && sw_Venus == true)       move(5);

        if (getWorld().getObjects(Menu.class).isEmpty() == true)
        {
            if (isTouching(Mercur.class) == true && sw_Mercur == false)
            {
                getWorld().addObject(new Menu(this, "Mercur"), 1350, 160);          // the panel appears only once with question 1
                sw_Mercur = true;
                if (Greenfoot.isKeyDown("1"))   // the answer is correct
                {
                    getWorld().removeObjects(getWorld().getObjects(Menu.class));    // remove menu
                    move(5);                                                        // and let the alien move again
                }
            }

            if (isTouching(Venus.class) == true && sw_Venus == false)
            {
                getWorld().addObject(new Menu(this, "Venus"), 1350, 160);           // the panel appears only once with question 2
                sw_Venus = true;
                if (Greenfoot.isKeyDown("3"))   // the answer is correct
                {
                    getWorld().removeObjects(getWorld().getObjects(Menu.class));    // remove menu
                    move(5);                                                        // and let the alien move again
                }
            }
		}
	}
}
danpost danpost

2018/2/26

#
Think logically about what lines 14 and 15 will do. If touching Mercur then the else part will be checked. Chances are if touching Mercur, then its not touching Venus; so, it will move.
danpost danpost

2018/2/26

#
Basically, you do not want the alien to move if a panel is showing:
if (getWorld().getObjects(Menu.class).isEmpty())
{
    move(5);
    if (!sw_Mercur && isTouching(Mercur.class))
    {
        getWorld().addObject(new Menu(this, "Mercur"), 1350, 160);
        sw_Mercur = true;
    }
    if (!sw_Venus && isTouching(Venus.class))
    {
        getWorld().addObject(new Menu(this, "Venus"), 1350, 160);
        sw_Venus = true;
    }
}
The menu should check for user response and remove its objects -- not the alien.
Maus2525 Maus2525

2018/2/26

#
I've changed it, deleting the 'else', but it's the same thing happening.
import greenfoot.*;
 
public class Alien extends Actor
{
    boolean sw_Mercur = false;
    boolean sw_Venus = false;
 
    public void act() 
    {
 
        /*
         * If the alien isn't touching any planet, it moves.
         */
        if (isTouching(Mercur.class) == false)     move(5);
        if (isTouching(Venus.class) == false)      move(5);
 
        /*
         * If the alien touches an already visited planet, he can continue moving.
         */
        if (isTouching(Mercur.class) == true && sw_Mercur == true)     move(5);
        if (isTouching(Venus.class) == true && sw_Venus == true)       move(5);
 
        if (getWorld().getObjects(Menu.class).isEmpty() == true)
        {
            if (isTouching(Mercur.class) == true && sw_Mercur == false)
            {
                getWorld().addObject(new Menu(this, "Mercur"), 1350, 160);          // the panel appears only once with question 1
                sw_Mercur = true;
                if (Greenfoot.isKeyDown("1"))   // the answer is correct
                {
                    getWorld().removeObjects(getWorld().getObjects(Menu.class));    // remove menu
                    move(5);                                                        // and let the alien move again
                }
            }
 
            if (isTouching(Venus.class) == true && sw_Venus == false)
            {
                getWorld().addObject(new Menu(this, "Venus"), 1350, 160);           // the panel appears only once with question 2
                sw_Venus = true;
                if (Greenfoot.isKeyDown("3"))   // the answer is correct
                {
                    getWorld().removeObjects(getWorld().getObjects(Menu.class));    // remove menu
                    move(5);                                                        // and let the alien move again
                }
            }
        }
    }
}
Maus2525 Maus2525

2018/2/26

#
Sorry I haven't reloaded the page!
Maus2525 Maus2525

2018/2/26

#
Well, this time the alien stops, but after pressing the right answer, the panel doesn't disappear and the alien doesn't continue moving.
if (getWorld().getObjects(Menu.class).isEmpty())
{
    move(5);
    if (!sw_Mercur && isTouching(Mercur.class))
    {
        getWorld().addObject(new Menu(this, "Mercur"), 1350, 160);
        sw_Mercur = true;
		if (Greenfoot.isKeyDown("1"))   // the answer is correct
        {
			getWorld().removeObjects(getWorld().getObjects(Menu.class));    // remove menu
			move(5);		// continue moving if the answer is correct
		}
    }
    if (!sw_Venus && isTouching(Venus.class))
    {
        getWorld().addObject(new Menu(this, "Venus"), 1350, 160);
        sw_Venus = true;
		if (Greenfoot.isKeyDown("3"))   // the answer is correct
		{
			getWorld().removeObjects(getWorld().getObjects(Menu.class));    // remove menu
			move(5);		// continue moving if the answer is correct
		}
    }
}
danpost danpost

2018/2/26

#
Time to work on the Menu class.
Maus2525 Maus2525

2018/2/27

#
Thank you very much! It's working now, since I've changed the Menu class!!!
You need to login to post a reply.