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

2017/11/22

Two Identical Objects. One doesn't work

Daniel_ Daniel_

2017/11/22

#
I'm trying to create objects the player can interact with to bring them to another world. These door objects act like this.
int WorldNumber;
    public Door(int WorldNumber){
        this.WorldNumber = WorldNumber;
    }

    public void act() 
    {
        
        if (getOneIntersectingObject(player.class) != null && Greenfoot.isKeyDown("enter") && getWorld() instanceof WalkingWorld && !isPressing) {
            Greenfoot.setWorld(new InsideArea(WorldNumber));
        } 
        if (getOneIntersectingObject(player.class) != null && Greenfoot.isKeyDown("enter") && getWorld() instanceof InsideArea && !isPressing) {
            Greenfoot.setWorld(new WalkingWorld(WalkingWorld.CurrentWorld));
        }
   isPressing();
    } 
The first if statement checks if the player is interacting in one world, while the other one checks the same thing but in another world. When I add one of these objects into the world, it works like a charm. However, when I place two instances of the object, the second one I placed doesn't work, while the first one does. If I flip the order, the first one instantiated is still the only one that works To place them I just use addObject Any help would be appreciated. Thanks.
danpost danpost

2017/11/22

#
A broader view of what you have is needed to work out the issue. At minimum (to start with), everything dealing with 'isPressing' and with 'isPressing()'.
Daniel_ Daniel_

2017/11/22

#
My door object extends this trigger object, as multiple objects will use the isPressing() method.
public class Trigger extends Actor
{
    /**
     * Act - do whatever the Trigger wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed into the environment.
     */
    static boolean isPressing;
    
    public void act() 
    {
        // Add your action code here.
    }    
    public static boolean isPressing(){
        if (Greenfoot.isKeyDown("enter")){
            isPressing = true;
        } else {
            isPressing = false;
        }

        return isPressing;
    }
    
}
Basically my door uses this method to check if the user is pressing enter. It does this to prevent the player from holding the enter key and instantaniously shift through worlds.
Super_Hippo Super_Hippo

2017/11/22

#
Don't make everything static! I guess the Door class extends the Trigger class. What happens: - isPressing is false - player stands on second door - you click enter - first door's act method executes, the conditions are not met because the player is not standing on it, the isPressing method is executed and sets isPressing to true - now the second door's act method executes and the conditions are also not met because isPressing is true
Daniel_ Daniel_

2017/11/22

#
Ohhh I see what you're saying. I just changed some things around and made it a non-static. Things work now, thank you Super_Hippo.
danpost danpost

2017/11/22

#
I think you had the right idea as far as getting a value for what 'isPressing' is used for, but implemented it poorly or did not think it out far enough. I ran several things through my head as far as what might work and came up with only one possible way. You could use a Trigger class similar to what you have, but not as a superclass to the classes that use it. It would look like this:
public class Trigger
{
    private static boolean justPressed;
    private static boolean isPressed;
    
    public static void update()
    {
        justPressed = false;
        if (isPressed != greenfoot.Greenfoot.isKeyDown("enter"))
        {
            isPressed = ! isPressed;
            justPressed = isPresssed;
        }
    }
    
    public static boolean detected()
    {
        return justPressed;
    }
}
Notice, it not only tracks the state of the key ('isPressed'), but also flags the moment it is pressed ('justPressed'). With this, all your World subclass act methods can call:
Trigger.update();
and any and all objects that use the trigger can just ask:
if (Trigger.detected())
All in all, what I did was move the comparison of current and last state of the key to the Trigger class which allows updating its value once per act step from the world before the actors act. This was not possible before because the Trigger class would hold the current state without any knowledge of the previous state (the current state would be compared to the current state -- a rather useless condition).
Daniel_ Daniel_

2017/11/23

#
Oh, I see. This makes things a lot easier and more flexible, I understand where I went wrong. Thanks Dan,
You need to login to post a reply.