Hello, I want to make a game in the style of Super Mario Bros in which my character has to change characters if he touches an object. Unfortunately, I don't know how to do it.
![Twitter](/assets/twitter-4e19209ef84344ee0c433f4c7bad8d49.png)
![Twitter.hover](/assets/twitter.hover-1fb19a5bafc50deace8f88eaec867845.png)
public void collision() { if (isTouching(Mushroom.class)) { removeTouching(RMushroom.class); GMario gMario = new GMario(); addObject(GMario(), getX(), getY()); getWorld().removeObject(this); } if (isTouching(Plant.class)) { removeTouching(RMushroom.class); FMario fMario = new FMario(); addObject(FMario(), getX(), getY()); getWorld().removeObject(this); } if ( isTouching(Enemies.class) ) { Greenfoot.stop(); } }
if (isTouching(RMushroom.class)) { removeTouching(RMushroom.class); } if (isTouching(Plant.class)) { removeTouching(RMushroom.class); FMario fMario = new FMario(); addObject(FMario(), getX(), getY()); getWorld().removeObject(this); } if ( isTouching(Enemies.class) ) { SMario sMario = new SMario(); addObject(SMario(), getX(), getY()); getWorld().removeObject(this); }
if (isTouching(RMushroom.class)) { removeTouching(RMushroom.class); } if (isTouching(Plant.class)) { removeTouching(Plant.class); } if ( isTouching(Enemies.class) ) { SMario sMario = new SMario(); addObject(SMario(), getX(), getY()); getWorld().removeObject(this); }
private static final int SMARIO = 0, GMARIO = 1, FMARIO = 2; private int curMario = SMARIO; // with action code like the following if (isTouching(Mushroom.class)) { removeTouching(Mushroom.class); if (curMario == SMARIO) { setMario(GMARIO); } if (isTouching(Plant.class)) { removeTouching(Plant.class); if (curMario != FMARIO) { setMario(FMARIO); } } if (isTouching(Enemies.class)) { if (curMario != SMARIO) { removeTouching(Enemies.class); setMario(SMARIO); } else { Greenfoot.stop(); } } } // the setMario(int) method private void setMario(int state) { curMario = state; setImage(images[curMario]); } // with image fields (you supply proper names) private GreenfootImage[] images = { new GreenfootImage("smario.png"), new GreenfootImage("gmario.png"), new GreenfootImage("fmario.png") };