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

2024/5/28

How to change character?

sama sama

2024/5/28

#
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.
danpost danpost

2024/5/29

#
sama wrote...
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.
Will the character still be the same character, but different -- like taking a potion and "changing"? Please be more detailed as to what you are trying to do. Providing code helps immensely, as well.
sama sama

2024/5/29

#
I would like that when my character touches a plant, he could change into a new character that throws fire. And when he touches a mushroom, my character has to change into another bigger character. And when my character, who has touched one of his objects touches an enemy, he must change to the character he was before having taken one of his objects. You can see below, the collision code of my main character when he has not taken an object.
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(); 
        }
    }  
You can see below, the collision code of my main character when he has taken a mushroom.
        
        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);
        }
You can see below, the collision code of my main character when he has taken a plant.
       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);
        }
sama sama

2024/5/29

#
I tried to do this but it doesn't work. The objects are removed but my character does't change to another character.
danpost danpost

2024/5/30

#
sama wrote...
I tried to do this but it doesn't work. The objects are removed but my character does't change to another character.
I suggest that you use only one class for this character. The character can take on any form and do whatever depending on its state. Furthermore, I suggest that static final int values be used to define the states. An instance field will hold the current state and will also be used to make sure the actor does what it is supposed to do when in the state it is in. So, maybe something like:
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")
};
sama sama

2024/5/31

#
It works half way. I mean when Mario picks up an item, he has two chances of survival before dying. But, the problem now is that Mario doesn't change characters. Do I have to put all my images of each character for this to work?
danpost danpost

2024/6/1

#
sama wrote...
It works half way. I mean when Mario picks up an item, he has two chances of survival before dying. But, the problem now is that Mario doesn't change characters. Do I have to put all my images of each character for this to work?
That is one way, with each being "labeled". And, that is probably the easiest way.
You need to login to post a reply.