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
5
6
danpost danpost

2020/4/28

#
Weeb.exe wrote...
<< Code Omitted >> it says where i said it cant find it << Quote Omitted >> how would i call that since this method is in the "OpenableDoor" class
Remove the "&& isOpen()". That is checked for in the following lines.
Weeb.exe Weeb.exe

2020/4/28

#
ok sweet
Weeb.exe Weeb.exe

2020/4/28

#
public boolean allowCollision()
    {
        if (isTouching(OpenableDoor.class))
        {
            OpenableDoor door = (OpenableDoor)getOneIntersectingObject(OpenableDoor.class);
            if (door.isOpen())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }// says missing return statement
how do i fix that?
danpost danpost

2020/4/28

#
Add "return true;" at the end (but inside the method).
Weeb.exe Weeb.exe

2020/4/28

#
ok its fixed but now i can walk through the door
Weeb.exe Weeb.exe

2020/4/28

#
I've tried calling the method but i can still walk through it can i make it so cant until its open?
danpost danpost

2020/4/28

#
Post revised OpenableDoor and Characters classes. Also one of its subclasses (the one you tested, preferably).
Weeb.exe Weeb.exe

2020/4/28

#
danpost wrote...
Post revised OpenableDoor and Characters classes. Also one of its subclasses (the one you tested, preferably).
sorry i don't really understand what you just said an you dumb it down a little please? sorry
danpost danpost

2020/4/29

#
Weeb.exe wrote...
can you dumb it down a little
Show your updated codes.
Weeb.exe Weeb.exe

2020/4/29

#
updated code for characters class
public class Characters extends Actor
{
    /**
     * Act - do whatever the Characters wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        DoorClosed();
        allowCollision();
    }  

    public boolean allowCollision()
    {
        if (isTouching(OpenableDoor.class))
        {
            OpenableDoor door = (OpenableDoor)getOneIntersectingObject(OpenableDoor.class);
            if (door.isOpen())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        return true;
    }

    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;
            }
        }
        return true;
    }

    public void stopByOpenableDoor(Actor closeDoor)
    {
        int wallWidth = closeDoor.getImage().getWidth();
        int newX = closeDoor.getX() -(wallWidth + getImage().getWidth())/2;
        setLocation(newX , getY());
    }
}
then OpenableDoor class
public class OpenableDoor extends Actor
{
    private boolean open, eDown;
    /**
     * Act - do whatever the OpenableDoor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("e") != eDown)
        {
            eDown = !eDown;
            if (isTouching(Characters.class) && eDown)
            {
                open = !open;
                setImage(open ? "OpenableDoor1.png" : "OpenableDoor2.png");
            }
        }
        
    }

    public boolean isOpen()
    {
        return open;
    }
}
danpost danpost

2020/4/29

#
You need to show Betty class or one of the Billy classes, also.
Weeb.exe Weeb.exe

2020/4/29

#
danpost wrote...
You need to show Betty class or one of the Billy classes, also.
ok here is betty
public class Betty extends Characters
{
    private GreenfootImage Betty1= new GreenfootImage("Betty1.png"); 
    private GreenfootImage Betty2= new GreenfootImage("Betty2.png");
    private GreenfootImage BettyGun1= new GreenfootImage("Betty_Gun_1.png"); 
    private GreenfootImage BettyGun2= new GreenfootImage("Betty_Gun_2.png");
    private int animatecounter;
    private int pistolGrabbed;
    public int fireTimer = 0;
    private boolean fire = false;
    public int lives = 5;
    /**
     * Act - do whatever the Betty wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        grabPistol();
        BettyMovement();
        checkRightWalls();
        checkLeftWalls();
        platformAbove();
        platformBelow();
        if (fireTimer != 0)
        {
            fireTimer --;
        }
        else
        {
            fire = false;
        }
        if (Greenfoot.isKeyDown("e"))
        {
            OpenableDoor door = (OpenableDoor) getOneIntersectingObject(OpenableDoor.class);

        }
    }
    public void healthBar()
    {
        lives--;
    }
    public void detectBulletCollision()
    {
        if (isTouching(Bullet.class))
        {
            healthBar();
        }
    }
    
    public void grabPistol()
    {
        if (isTouching(Pistol.class) && Greenfoot.isKeyDown("E"))
        {
            removeTouching(Pistol.class);
            pistolGrabbed++;
        }
    }

    private void BettyMovement()  
    {
        if (Greenfoot.isKeyDown("d")) {

            setLocation(getX()+1,getY()); 
            animate();
            animateWithGun();

        }
        if (Greenfoot.isKeyDown("a")) {

            setLocation(getX()-1,getY());
            animate();
            animateWithGun();
        }
        if (Greenfoot.isKeyDown("s")) {

            setLocation(getX(),getY()+1);
            animate();
            animateWithGun();
        }
        if (Greenfoot.isKeyDown("w")) {

            setLocation(getX(),getY()-1);
            animate();
            animateWithGun();
        }
        if (Greenfoot.mouseClicked(this)) 
        {
            shoot();
        }
        if (Greenfoot.isKeyDown("d") && Greenfoot.isKeyDown("shift")  ) {

            setLocation(getX()+2,getY());
            animate();
            animateWithGun();
        }
        if (Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("shift")  ) 
        {

            setLocation(getX(),getY()-2);
            animate();
            animateWithGun();
        }
        if (Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("shift")  )
        {

            setLocation(getX()-2,getY());
            animate();
            animateWithGun();
        }
        if (Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("shift")  )
        {

            setLocation(getX(),getY()+2);
            animate();
            animateWithGun();
        }
        if (Greenfoot.isKeyDown("p") &&  isTouching(RobotEnemy.class))
        {
            removeTouching(RobotEnemy.class);
        }
    }

    public void animate() 
    {
        if (pistolGrabbed <1){
            animatecounter++;
            if (animatecounter > 5) {
                animatecounter = 0;
                if (getImage() == Betty1) {
                    setImage(Betty2);
                }
                else {
                    setImage(Betty1); }
            }
        }
    }

    public void animateWithGun() {
        if (pistolGrabbed ==1){

            animatecounter++;
            if (animatecounter > 5) {
                animatecounter = 0;
                if (getImage() == BettyGun1) {
                    setImage(BettyGun2);
                }
                else {
                    setImage(BettyGun1); }
            }
        }
    }

    public void shoot() {
        if (pistolGrabbed ==1   && fire == false)
        {
            fire();
            fire = true;
            fireTimer = 25;

        }
    }

    private void fire()
    {

        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX(), getY());
        bullet.move(05);
    }

    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);

        Actor rightWall = getOneObjectAtOffset (xDistance, 0, Brick.class);
        if (rightWall == null)
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }

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

    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/-2);

        Actor leftWall = getOneObjectAtOffset (xDistance, 0, Brick.class);
        if (leftWall == null)
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }

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

    public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight / 2 + 5);

        Actor ceiling = getOneObjectAtOffset(0, getImage().getHeight()/2, (Brick.class));
        if (ceiling == null)
        {
            return false;
        }
        else
        {
            stopByCeiling(ceiling);
            return true;
        }
    }

    public void stopByCeiling(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getWidth())/2;
        setLocation(getX() , newY);
    }

    public boolean platformBelow()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight / -2);

        Actor floor = getOneObjectAtOffset(0, yDistance, (Brick.class));
        if (floor == null)
        {
            return false;
        }
        else
        {
            stopByFloor(floor);
            return true;
        }
    }

    public void stopByFloor(Actor floor)
    {
        int floorHeight = floor.getImage().getHeight();
        int newY = floor.getY() - (floorHeight + getImage().getWidth())/2;
        setLocation(getX() , newY);
    }

}
danpost danpost

2020/4/29

#
Okay, in Characters class, remove line 10 and replace lines 7 thru 49 with:
public void act()
{
    OpenableDoor door = getOneIntersectingObject(OpenableDoor.class);
    if (!door.isOpen()) stopByOpenableDoor(door);
}
In line 54, add 1 to the value set to newX ( "... = 1+ ..." ). In ALL subclasses, immediately after calling movement method (insert after line 19 in Betty class above), add the following line:
super.act();
Weeb.exe Weeb.exe

2020/4/29

#
public void act()
{
    OpenableDoor door = getOneIntersectingObject(OpenableDoor.class)incompatible types; greenfoot.Actor cannot be converted to OpenableDoor;
    if (!door.isOpen()) stopByOpenableDoor(door);
}
how do i fix that?
incompatible types; greenfoot.Actor cannot be converted to OpenableDoor
danpost danpost

2020/4/29

#
Weeb.exe wrote...
<< Code Omitted >> how do i fix that?
incompatible types; greenfoot.Actor cannot be converted to OpenableDoor
Sorry, change 1st word in line 3 to "Actor".
There are more replies on the next page.
1
2
3
4
5
6