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

2014/7/2

method does nothing

ehx ehx

2014/7/2

#
Hey, I'm working at creating a game out of the Robots scenario and one method I use and that should work does nothing instead. Here is the whole code of the problem class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * RoboterSteuerung ist dafür zuständig,
 * dass die Roboter mit Hilfe der Tastatur
 * gesteuert werden können.
 * 
 * Enables to control the robots with keys.
 * 
 * @author EHX
 * @version 01/07/2014
 */
public class RoboterSteuerung extends Roboter
{
    /**
     * Alle steuerbaren Roboter und ein Zeiger.
     * All controllable robots and a pointer.
     */
    Robita robita;
    Robby robby;
    Robson robson;
    Robocount robocount;
    Roboter contRob;
    public RoboterSteuerung(){
    }
    /**
     * Diese Methode initialisiert zu Beginn die Roboter. Gesteuert wird zuerst Robita.
     * Initializes the robots at the beginning and sets the control on Robita.
     */
    public void initialize(){
        if(robita==null){
            robita=(Robita)this.getOneObjectAtOffset(1,1,Robita.class);
        }
        if(robby==null){
            robby=(Robby)this.getOneObjectAtOffset(1,2,Robby.class);
        }
        if(robson==null){
            robson=(Robson)this.getOneObjectAtOffset(1,3,Robson.class);
        }
        if(robocount==null){
            robocount=(Robocount)this.getOneObjectAtOffset(1,4,Robocount.class);
        }
        contRob=robita;
    }
    /**
     * Die Hauptmethoden: Einfache Steuerbefehle.
     * This is the main method with simple controls.
     */
    public void generalControls(){
        if(Greenfoot.isKeyDown("up")){
            contRob.bewegungsfunktion(); //this works, by the way. The function is defined in the Roboter code.
        }
        if(Greenfoot.isKeyDown("left")){
            contRob.dreheLinks();
        }
        if(Greenfoot.isKeyDown("right")){
            contRob.dreheRechts();
        }
        /**
         * The following code should change controls to the next robot,
         * but it seems to do nothing instead.
         */
        if(Greenfoot.isKeyDown("down")){
            if(contRob==robita){
                contRob=robby;
            }
            if(contRob==robby){
                contRob=robson;
            }
            if(contRob==robson){
                contRob=robocount;
            }
            if(contRob==robocount){
                contRob=robita;
            }
        }
    }
        
    public void act() 
    {
        initialize();
        generalControls();
    }    
}
danpost danpost

2014/7/2

#
As coded, the 'down' part should pass control from one Roboter to the next, as you want. However, this may happen several act cycles in a row due to your usage of 'isKeyDown', which will return true multiple times before the key is released. Also (and this may be why it does not seem to work), you have four Roboters in the world and each one will change the 'contRob' to the next one each act cycle, essential making one cycle of 'contRob' values. I would first only run the controls for the 'contRob' object by placing the following line as the first line in 'generalControls':
if (contRob != this) return;
Then, I would either add a boolean to track the state of the 'down' key to regulate it or use 'getKey' instead of 'isKeyDown' for the 'down' command.
ehx ehx

2014/7/3

#
thanks, but that line makes everything stop working at all because the control pad never initializes itself with 'contRob' so it always stops however, I'm going to look up the Documentation for how to use 'getKey'.
ehx ehx

2014/7/3

#
it didn't help either
ehx ehx

2014/7/3

#
by the way, it shouldn't happen four times because the control pad itself is making the changes and not 'contRob'
danpost danpost

2014/7/3

#
Maybe you should not be initializing every act cycle, but only when the control pad is added to the world. However, if all four fields are set initially, then it really should not change how things are working. Anyway, it would seem like extra duty for the CPU to do that each cycle.
danpost danpost

2014/7/3

#
Change line 63 above to the following and see what happens:
if ("down".equals(Greenfoot.getKey())){
danpost danpost

2014/7/3

#
It would most certainly be better to put the movement code in the Roboter class instead of creating another subclass of Roboter for controlling them. First the control is not a Roboter object in itself and this could cause some of the complications you are having.
ehx ehx

2014/7/10

#
by now I understood what you meant about the four Robots, the method changes back to Robita because the program follows all the if-sentences separately. If some "else" commands don't fix it, I'll completely change the strategy
danpost danpost

2014/7/10

#
An effective solution would be to remove the control panel altogether and declare the 'contRob' field as 'static' in your Roboter class. By making the field a 'static' one, you are allowing all objects of the class to share the same field (instead of separate non-static fields that can contain different values for each object). Then, in the 'act' method of the Roboter class:
public void act()
{
    if (contRob == null) contRob == this; // gain control
    if (contRob != this) return;
    // action code goes here
    if ("down".equals(Greenfoot.getKey())) contRob = null; // give up control
}
With this, when one robot gives up control, the next one to act will pick up control. You do not have to know which one (specifically) had it or which one it is going to and this will work with any number of robots.
You need to login to post a reply.