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

2020/4/10

how would i create an openable door

1
2
3
4
Weeb.exe Weeb.exe

2020/4/10

#
currently i am making a game which has doors that open if you touch it and press e this is the code
public void act() 
    {
                if (Greenfoot.isKeyDown("e") && (isTouching (OpenableDoor.class)))
        {
            openDoor();
        }
        else 
        {
            closeDoor();
        }
    } 

    public void openDoor()
    {
        allowCollision();
    }

    public void closeDoor()
    {
        DoorClosed();
    }

    public boolean DoorClosed()
    {
        if (allowCollision() = false)
        {
            int spriteWidth = getImage().getWidth();
            int xDistance = (int) (spriteWidth/2);

            Actor closeDoor = getOneObjectAtOffset (xDistance, 0, OpenableDoor.class);
            if (closeDoor == null)
            {
                return false;
            }
            else
            {
                stopByOpenableDoor(closeDoor);
                return true;
            }
        }
    }

    public void stopByOpenableDoor(Actor closeDoor)
    {
        int wallWidth = closeDoor.getImage().getWidth();
        int newX = closeDoor.getX() -(wallWidth + getImage().getWidth())/2;
        setLocation(newX , getY());
    }

    public boolean allowCollision()
    {

    }
(btw this is the code from the character) how would i make this so i can open it it also says this in the allowCollision() in door closed
unexpected type required: variable found: value
Super_Hippo Super_Hippo

2020/4/10

#
1. You try to close a door when either e isn’t pressed or the character isn’t touching a door. Which doesn’t really make much sense. 2. “allowCollision” has a return type of “boolean” but it doesn’t return anything. 3. In line 25, you use = (assignment) instead of == (comparison). What is “allowCollision” supposed to do? You call it when you want to open a door. And you call it is a condition to close a door. I would do something like this:
//in Door class
private boolean open;

public Door(boolean open)
{
    this.open = open;
    updateImage();
}

public void toggleOpen()
{
    open = !open;
    updateImage();
}

private void updateImage()
{
    setImage(open ? "openDoor.png" : "closedDoor.png");
}
//in Character
if (Greenfoot.isKeyDown("e"))
{
    Door door = (Door) getOneIntersectingObject(Door.class);
    if (door != null)
    {
        door.toggleOpen();
    }
}
danpost danpost

2020/4/10

#
The second code set provided by Super Hippo will not work as intended as the door will repeatedly open and close during intersecting with the key down. Try this instead:
// in Door class
private boolean open, eDown;

public void act()
{
    if (Greenfoot.isKeyDown("e") != eDown)
    {
        eDown = !eDown;
        if (isTouching(Player.class) && eDown)
        {
            open = !open;
            setImage(open ? "openDoor.png" : "closedDoor.png");
        }
    }
}
(replace Player in line 9 with appropriate class name of character)
Weeb.exe Weeb.exe

2020/4/14

#
ok thank you guys
Weeb.exe Weeb.exe

2020/4/14

#
danpost wrote...
The second code set provided by Super Hippo will not work as intended as the door will repeatedly open and close during intersecting with the key down. Try this instead:
// in Door class
private boolean open, eDown;

public void act()
{
    if (Greenfoot.isKeyDown("e") != eDown)
    {
        eDown = !eDown;
        if (isTouching(Player.class) && eDown)
        {
            open = !open;
            setImage(open ? "openDoor.png" : "closedDoor.png");
        }
    }
}
(replace Player in line 9 with appropriate class name of character)
problem now since there isn't a toggle open door it says it cant find it how would i fix this?
danpost danpost

2020/4/14

#
Weeb.exe wrote...
problem now since there isn't a toggle open door it says it cant find it how would i fix this?
You shouldn't need to call it with the given code. Lines 11 and 12 do the toggling. That is, unless there is some other reason the door would change its open state other than user pressing "e" while touching..
Weeb.exe Weeb.exe

2020/4/15

#
another problem i can walk through the door how would i make it that if it was closed it wouldn't allow me to walk through?
danpost danpost

2020/4/15

#
The operator on line 25 of your first code post would be an issue.
Weeb.exe Weeb.exe

2020/4/16

#
danpost wrote...
The operator on line 25 of your first code post would be an issue.
what do you mean by that? how would i do that? would i just get make it so it is like this?
public boolean DoorClosed()
    {
        if (allowCollision() = false)//I know this is wrong except i don't know how to fix it?
        {
            int spriteWidth = getImage().getWidth();
            int xDistance = (int) (spriteWidth/2);
 
            Actor closeDoor = getOneObjectAtOffset (xDistance, 0, OpenableDoor.class);
            if (closeDoor == null)
            {
                return false;
            }
            else
            {
                stopByOpenableDoor(closeDoor);
                return true;
            }
        }
    }
danpost danpost

2020/4/16

#
Weeb.exe wrote...
what do you mean by that? how would i do that?
You compare for equality correctly on line 9. Look at the operator there.
Weeb.exe Weeb.exe

2020/4/16

#
then how do i fix allowCollision() line? How would i create the code for that
danpost danpost

2020/4/17

#
Weeb.exe wrote...
then how do i fix allowCollision() line? How would i create the code for that
Use "==" instead of "=". You want to compare for equality, not set it.
Weeb.exe Weeb.exe

2020/4/24

#
sorry it has been a while went to my mothers and i don't have a computer there how would i do the suggestion you posted above
danpost danpost

2020/4/24

#
Weeb.exe wrote...
how would i do the suggestion you posted above
danpost wrote...
Use "==" instead of "=".
Weeb.exe Weeb.exe

2020/4/25

#
like this?
if (allowCollision() == false)
if so i don't have an allow collision code so how would i make that?
There are more replies on the next page.
1
2
3
4