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

2018/9/24

Mario not dying when touching object

Starvader Starvader

2018/9/24

#
Hi, I have written code which should enable Mario to kill a goomba if he is above the goomba, but he will die if he is next to the goomba
public boolean onGoomba() {
        Actor under = getOneObjectAtOffset (0, getImage().getHeight() / 2, Goomba.class);
        Actor under1 = getOneObjectAtOffset (1, getImage().getHeight() / 2, Goomba.class);
        Actor under2 = getOneObjectAtOffset (-1, getImage().getHeight() / 2, Goomba.class);
        if (under != null || under1 != null || under2 != null) {
            return true;
        } else {
            return false;
        }
    }

public void checkKill() {
        if (canSee (Goomba.class) && onGoomba()==true) {
            MyWorld x = (MyWorld) this.getWorld();
            Goomba current = getGoomba(Goomba.class);
            for(int i=0;i<2;i++){
                if(x.enemies1[i]==current){
                    x.enemies1[i]=null;
                    Greenfoot.playSound("GoombaDead.mp3");
                }
            }
            x.removeObject(current);
            vSpeed = -20;
            fall();
        }
    }
This is in my Mario class. For my goomba class, I have this:
public void killMario() {
        if (canSee(Mario.class) && nextToMarioLeft()==true || canSee(Mario.class) && nextToMarioRight()==true) {
            eat(Mario.class);
            Greenfoot.stop();
        }
    }

public boolean belowMario() {
        Actor above = getOneObjectAtOffset (0, getImage().getHeight() / 2, Mario.class);
        return above !=null;
    }

public boolean nextToMarioLeft() {
        Actor nextto = getOneObjectAtOffset (getImage().getWidth() / 2, 0, Mario.class);
        return nextto != null;
    }

    public boolean nextToMarioRight() {
        Actor nextto = getOneObjectAtOffset (-(getImage().getWidth()/2), 0, Mario.class);
        return nextto != null;
    }
For some reason, even when I stand next to a goomba, Mario kills the goomba. No matter what, the goomba dies. I dont understand why this isnt working. Could someone please help me out? Thanks in advance
danpost danpost

2018/9/24

#
A few things to consider:
  • only check collision between two object in one class -- not in both (I would put it all in the Mario class);
  • only check collision immediately after each move of the actor;
  • keep horizontal and vertical movement code separate (knowing which move caused a collision will tell you the orientation of the colliding actors);
  • you may want to dispense with the array of enemies in your world class; I doubt very much that there is a need for it
Please give your reason for the array if you feel you need it. To determine orientation, you will also need the sign of the speed along the axis moved. For example if moving vertically ("falling"), if vSpeed is negative then a collision will be a upward moving head-bump and, if positive then it will be a landing atop of the actor.
You need to login to post a reply.