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

2019/5/16

Key calling method once on multiple objects

MRCampbell MRCampbell

2019/5/16

#
I want to duplicate all of the objects that have enough "energy". The code as it is written, objects duplicates multiple times because I cannot release the "d" key quick enough. So I tried adding a boolean to keep track if the key was pressed. I also tried adding delay timer. Finally I tried using the delay method. All three prevented the method from acting on all the objects. Only one of the objects duplicate.
public void duplicate() 
    {
        if(Greenfoot.isKeyDown("d"))
        {                         
            World W = getWorld();  
            if(energy > 30)
            {
                int speedMutation = Greenfoot.getRandomNumber(3) - 1;
                Creature dupCre = new Creature(speed + speedMutation);
                W.addObject(dupCre, getX(), getY());
            } else {
                W.removeObject(this);
                System.out.println("     Another one bites the dust: Mutation Failed");
            }
        }
    }
danpost danpost

2019/5/16

#
You should try doing it from your World subclass.
MRCampbell MRCampbell

2019/5/18

#
I created a method in the Creature class called getCreature. This method returns the Creature object called theCreature.
public Creature getCreature()
    {
        return theCreature;
    }
Here is the new duplicate method in the world class.
public void duplicate() 
    {
        if(Greenfoot.isKeyDown("d"))
        {                         
            Creature theCreature = theCreature.getCreature();
            if(theCreature.speed > 1 && theCreature.energy > 30)
            {
                int speedMutation = Greenfoot.getRandomNumber(3) - 1;
                Creature dupCre = new Creature(theCreature.speed + speedMutation);
                dupCre.energy = (theCreature.energy / 5);
                addObject(dupCre, theCreature.getX(), theCreature.getY());
                System.out.println("     Duplication! Speed: " + dupCre.speed);
                
                theCreature.energy = theCreature.energy - (theCreature.energy / 10);
                
            } else {
                removeObject(theCreature);
                System.out.println("     Another one bites the dust: Mutation Failed");
            }
        }
    }
I want to copy an object's speed and I am having trouble referencing an individual object.
MRCampbell MRCampbell

2019/5/18

#
You can hold off on any help. I think I need to getObjects() and iterate through each. When I mess that up, I will come groveling back.
MRCampbell MRCampbell

2019/5/18

#
This worked. Thanks.
    public void duplicate() 
    {
        if(Greenfoot.isKeyDown("d"))
        {
            Greenfoot.delay(50);
            List<Object> creatureToDuplicate = new ArrayList<Object>();
            creatureToDuplicate = getObjects(Creature.class);
            for (int i = 0; i < creatureToDuplicate.size(); i = i + 1)
            {
                Creature c = (Creature) creatureToDuplicate.get(i);
                if(c.speed > 1 && c.energy > 30)
                {
                    int speedMutation = Greenfoot.getRandomNumber(3) - 1;
                    Creature dupCre = new Creature(c.speed + speedMutation);
                    dupCre.energy = (c.energy / 5);
                    addObject(dupCre, c.getX(), c.getY());
                    System.out.println("     Duplication! Speed: " + dupCre.speed);

                    c.energy = c.energy - (c.energy / 10);
                } else {
                    removeObject(c);
                    System.out.println("     Another one bites the dust: Mutation Failed");
                }
            }
        }
    }
danpost danpost

2019/5/18

#
I presume you put the delay in there to give the user a chance to release the key. Better might be to use a boolean to track the state of the key:
private boolean dDown;

public void duplicate()
{
    if (dDown == Greenfoot.isKeyDown("d")) return; // no change in key state
    dDown = !dDown; // track changing of key state
    if (!dDown) return; // key was not pressed, but released
    for (Object obj : getObjects(Creature.class))
    {
        Creature c = (Creature)obj;
        if (c.speed > 1 && c.energy > 30)
        {
            Creature dupCre = new Creature(c.speed+Greenfoot.getRandomNumber(3)-1);
            dupCre.energy = c.energy/5;
            addObject(dupCre, c.getX(), c.getY());
            c.energy = c.energy*9/10;
            System.out.println("     Duplication! Speed: "+dupCre.speed);
        }
        else
        {
            removeObject(c);
            System.out.println("     Another one bites the dust: Mutation failed");
        }
    }
}
MRCampbell MRCampbell

2019/5/21

#
Can you explain "return"? I though this was used to return something, an int or boolean.
danpost danpost

2019/5/21

#
MRCampbell wrote...
Can you explain "return"? I though this was used to return something, an int or boolean.
The return command is used to cause an immediate break out of the method and "return" back to the method that called it. Once executed, no proceeding lines of code in that method are processed. In a void method, nothing will (or can) be returned. In a non-void method, only a value matching the return type will (or can) be returned. It is a control-flow type command similar to the break command (when void of a return type), which causes an immediate "break" out of a loop structure (for, do or while).
You need to login to post a reply.