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

2017/3/1

Just one does move...

TyasinY TyasinY

2017/3/1

#
I need help. German is my first language. Sorry for my bad english. Just the first ''Frog"(Actor) does act if the variable "c" is not zero and is intersecting with "KontrollKreuz"(another Actor). The other Frogs do not react until the first Frog is removed.
public class KontrollKreuz extends Actor
{
    private int p = 64;
    public static int c;
    
    public KontrollKreuz() {
        
    }
    
    public void act() {
        control();
    }
    
    public void control() {
        boolean k = false;
        while(Greenfoot.isKeyDown("right") && getX() != 32 + 15*p) {
            if(k == false) {
                setLocation(getX() + p, getY());
            }
            k = true;
        }
        k = false;
        while(Greenfoot.isKeyDown("left") && getX() != 32) {
            if(k == false) {
                setLocation(getX() - p, getY());
            }
            k = true;
        }
        while(Greenfoot.isKeyDown("down") && getOneIntersectingObject(Frog.class) != null) {
            
            if(Greenfoot.isKeyDown("right")) {
                KontrollKreuz.c = 1;
            }
            if(Greenfoot.isKeyDown("left")) {
                KontrollKreuz.c = -1;
            }
        }
    }
}
public class Frog extends Actor
{
    private int p = 64;
    private int jp;
    
    public Frog() {
        jp = 1;
    }
    
    public void act() {
        reactOnEntities();
    }
    
    public void reactOnEntities() {
        Actor lillipad = getOneIntersectingObject(Lillipad.class);
        int c = KontrollKreuz.c;
        if(c != 0 && getOneIntersectingObject(KontrollKreuz.class) != null) {
            setLocation(getX() +  p * (jp * c), getY());
            if(getOneIntersectingObject(Lillipad.class) == null) {
                setLocation(getX() -  p * (jp * c), getY());
            }
            else {
                getWorld().removeObject(lillipad);
            }
        }
        KontrollKreuz.c = 0;
    }
}
danpost danpost

2017/3/1

#
I am not sure that you even need a KontrollKreuz class. All you really need is a way to determine which frog is currently active. A simple class field should be sufficient for that:
private static Frog activeFrog ;
Then, in the act method of the Frog class, you can have this structure:
public void act()
{
    if (activeFrog == null) activeFrog = this;
    if (activeFrog == this)
    {
        // do what a frog does when it is the active frog
        ...
        // when removed from world
        activeFrog = null;
        getWorld().removeObject(this);
    }
    else
    {
        // do what a frog does when not the active frog
    }
}
TyasinY TyasinY

2017/3/1

#
Thanks. But i want to control Frogs with a Kontrollkreuz. The Frog who intersects with the Kontrollkreuz should be the one who does move. Is there anything i did wrong for that?
danpost danpost

2017/3/1

#
TyasinY wrote...
Thanks. But i want to control Frogs with a Kontrollkreuz. The Frog who intersects with the Kontrollkreuz should be the one who does move. Is there anything i did wrong for that?
Well, for one thing, I do not see where you are keeping the KontrollKreuz with the frog. When the controlled frog moves, I would think you would want the controller would move with it. Also, when the controlled frog is removed, you need some code to put it with another frog so the next one can be controlled.
TyasinY TyasinY

2017/3/3

#
No, i don't want the Kontrollkreuz to move with the frog. And in don't want to remove the Frog i moved. There are many Frogs. The Frog who intersects with the Kontrollkreuz should move. But the other Frogs do not react to the Kontrollkreuz, until the first Frog is removed. Can you maybe explain why?
davmac davmac

2017/3/3

#
Because on line 17 you limit reaction dependent on 'c != 0':
        if(c != 0 && getOneIntersectingObject(KontrollKreuz.class) != null) {
On line 26 yet set 'c' to 0, which will prevent any further reaction:
KontrollKreuz.c = 0;
So, only one frog will have `c != 0` be true.
danpost danpost

2017/3/3

#
TyasinY wrote...
No, i don't want the Kontrollkreuz to move with the frog. And in don't want to remove the Frog i moved. There are many Frogs. The Frog who intersects with the Kontrollkreuz should move. But the other Frogs do not react to the Kontrollkreuz, until the first Frog is removed. Can you maybe explain why?
Move line 26 in the Frog class above up one line. The first frog to act may not be the one under control and was resetting 'c' back to zero before the one that was under control was able to test its value.
TyasinY TyasinY

2017/3/3

#
Thanks! Finally it worked, and i got the Problem fixed. Thanks alot!
You need to login to post a reply.