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

2017/1/14

Changing image when it collides

Hondrok Hondrok

2017/1/14

#
I want the actor to change its image when it hits Goomba. I tried to do it but it changes Goomba's image line 87
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    private Label myLabel;
    private int count = 10000;
     
    public Mario (Label label)
    {
        myLabel = label;
    }
     
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
           scoreCount();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
           scoreCount();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Mario_idle.png");
            }
            }
        
        else
        {
            setImage("Mario_jump.png");
            fall();
        }
    }
 
    public void scoreCount()
    {
        count-=1;
        myLabel.setText("SCOR: " + count);
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(goomba);
        }
    }
     
    public void hitCastle()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World myWorld = getWorld();
            GameOver_Win gameover_win = new GameOver_Win();
            myWorld.addObject(gameover_win, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            Greenfoot.setWorld(new Level_2());
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Mario_walk.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Mario_walk_mirror.png");
    }
     
     
}
danpost danpost

2017/1/14

#
Hondrok wrote...
I want the actor to change its image when it hits Goomba. I tried to do it but it changes Goomba's image
I cannot see how that would be as you remove the Goomba object that it collides with (line 95). I would suggest adding the following line as the first line in the act method:
if (! getWorld().getObjects(GameOver.class).isEmpty()) return;
This will keep any image set to the actor when hitting a Goomba object and showing a GameOver actor so that any image set is not replaced by a moving (or idle) image on the next act cycle.
Hondrok Hondrok

2017/1/14

#
danpost wrote...
Hondrok wrote...
I want the actor to change its image when it hits Goomba. I tried to do it but it changes Goomba's image
I cannot see how that would be as you remove the Goomba object that it collides with (line 95). I would suggest adding the following line as the first line in the act method:
if (! getWorld().getObjects(GameOver.class).isEmpty()) return;
This will keep any image set to the actor when hitting a Goomba object and showing a GameOver actor so that any image set is not replaced by a moving (or idle) image on the next act cycle.
I don't think you've understood what I actually wanna do, I want to change Mario's image, for example let's say the image is "Mario_death.png", when he collides with goomba
setImage.Mario("Mario_death.png");
and this won't work
danpost danpost

2017/1/14

#
Hondrok wrote...
I don't think you've understood what I actually wanna do, I want to change Mario's image, for example let's say the image is "Mario_death.png", when he collides with goomba
setImage.Mario("Mario_death.png");
and this won't work
Try your line somewhere between lines 92 and 95 with my line first in the act method. Well, your line should be:
setImage("Mario_death.png");
Hondrok Hondrok

2017/1/14

#
danpost wrote...
Hondrok wrote...
I don't think you've understood what I actually wanna do, I want to change Mario's image, for example let's say the image is "Mario_death.png", when he collides with goomba
setImage.Mario("Mario_death.png");
and this won't work
Try your line somewhere between lines 92 and 95 with my line first in the act method. Well, your line should be:
setImage("Mario_death.png");
it works but it sets the image just when it hits goomba and I don't know how to change it to remain persistent
danpost danpost

2017/1/14

#
Hondrok wrote...
it works but it sets the image just when it hits goomba and I don't know how to change it to remain persistent
If you placed my line as the first line in your act method, it should stay persistent (unless you have other classes changing its image).
Hondrok Hondrok

2017/1/14

#
danpost wrote...
Hondrok wrote...
it works but it sets the image just when it hits goomba and I don't know how to change it to remain persistent
If you placed my line as the first line in your act method, it should stay persistent (unless you have other classes changing its image).
it still doesn't work
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    private int count = 10000;
     
    public void act() 
    {
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Mario_idle.png");
           }
        }
        
        else
        {
            setImage("Mario_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        setImage("Mario_death.png");
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_2());
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_3());
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Mario_walk.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Mario_walk_mirror.png");
    }
     
     
}
danpost danpost

2017/1/14

#
Move line 76 down inside the 'if' block' and add my line at the beginning of the act method.
Hondrok Hondrok

2017/1/14

#
danpost wrote...
Move line 76 down inside the 'if' block' and add my line at the beginning of the act method.
nope, still doesn't works
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    private int count = 10000;
     
    public void act() 
    {
       setImage("Mario_death.png");
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Mario_idle.png");
           }
        }
        
        else
        {
            setImage("Mario_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            setImage("Mario_death.png");
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_2());
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_3());
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Mario_walk.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Mario_walk_mirror.png");
    }
     
     
}
danpost danpost

2017/1/14

#
You put the wrong line at the beginning of the act method (see my first post on this discussion thread).
Hondrok Hondrok

2017/1/14

#
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    private int vSpeed = 0;
    private int acceleration = 2;
    private int jumpStrength = 25;
    private int count = 10000;
     
    public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) return;
       setImage("Mario_death.png");
       checkKeys();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
    }
     
    private void checkKeys()  
    {
       if(Greenfoot.isKeyDown("left"))
       {
           moveLeft();
       } 
       if(Greenfoot.isKeyDown("right"))
       {
           moveRight();
       }
       if(Greenfoot.isKeyDown("up"))
       {
           jump();
       }
    }    
 
    public void checkFall()
    {
        if(onGround() && vSpeed != -jumpStrength)
        {
           vSpeed=0; 
           if (Greenfoot.isKeyDown("left") == Greenfoot.isKeyDown("right")) 
           {
               setImage("Mario_idle.png");
           }
        }
        else
        {
            setImage("Mario_jump.png");
            fall();
        }
    }
     
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getImage().getHeight()/2, Ground.class);
        return under != null;
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }   
         
    public void jump()
    {
       if (onGround())
       {
           vSpeed = - jumpStrength;
       }
    }
     
    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            setImage("Mario_death.png");
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_2());
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            Greenfoot.setWorld(new Level_3());
        }
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
        setImage("Mario_walk.png");
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
        setImage("Mario_walk_mirror.png");
    }
     
     
}
danpost wrote...
You put the wrong line at the beginning of the act method (see my first post on this discussion thread).
it woorks :D but now it messed up the onGround method
danpost danpost

2017/1/14

#
You did not remove the line you incorrectly placed at the beginning of the act method.
Hondrok Hondrok

2017/1/14

#
danpost wrote...
You did not remove the line you incorrectly placed at the beginning of the act method.
yey it works but I don't understand the "if (! getWorld().getObjects(GameOver.class).isEmpty()) return; " thing what it does?
danpost danpost

2017/1/14

#
Hondrok wrote...
I don't understand the "if (! getWorld().getObjects(GameOver.class).isEmpty()) return; " thing what it does?
It basically says to exit the method (do not have the actor do anything) if a GameOver object is in the world. If execution of the code in the act was to continue, then lines like 45, 50, 108 and 114 would undo the setting of the death image.
You need to login to post a reply.