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

2012/11/4

How to update the background image continuously by drawing other images onto it?

ErikO ErikO

2012/11/4

#
I'm trying to make blood stains for a shooting game, so that every time a target gets hit a blood stain appears in the world (and new targets are inseted). First I used a class that extended Actor to do this but that had the problem that whenever a preexisting target moved passed a newly inserted stain the target was draw behind the stain. So I figured this could be solved by drawing the stains on the background instead. I've created a BackGround class which has an image as a field variable and a method for updating that image by drawing stains onto it by using the GreenfootImage.drawImage() method, and also created an instance of this class as a field variable in my world class, which is set as the world background in the act method, and updated every time a target is hit. The problem is however, that instead of just drawing the stain, it draws the entire world onto the preexisting image of the world and uses that a the background (see image and relevant code below ). Does anyone know how I might solve this problem? Thanks in advance :) From the world class:
public class ShootingRange extends World
{
     
    public BackGround backGround = new BackGround();
    
    public ShootingRange()
    {    
        super(1100, 500, 1);
    }
    
    public void act()
    {
        setBackground(backGround.getBackGround());
     }

public void handleTargetCollision()
    {
        List<Ball> balls = getObjects(Ball.class);
        for(int i=0; i<balls.size(); i++)
        {
            Ball b = balls.get(i);
            b.handleTargetCollision();
        }
    }
}
From the background class:
public class BackGround  
{
    private GreenfootImage backGround;
    
    GreenfootImage[] images = new  GreenfootImage[]{
        new GreenfootImage("blood1.png"),
        new GreenfootImage("blood2.png"),
        new GreenfootImage("blood3.png"), 
        new GreenfootImage("blood4.png"),};
    
    public BackGround()
    {
        backGround = new GreenfootImage(1100,500);
    }

  
    public void setBackGround(int x, int y)
    {
        GreenfootImage blood = images[Greenfoot.getRandomNumber(4)];
        blood.rotate(Greenfoot.getRandomNumber(360));
        backGround.drawImage(blood,x,y);
    }
    
    public GreenfootImage getBackGround()
    {
        return backGround;
    }
}
From the ball class:
public void handleTargetCollision()
    {
        World w = getWorld();
        ShootingRange s = (ShootingRange)w;
        if(hitTarget()==true)
        {
            List<Target> list = this.getIntersectingObjects(Target.class);
                for(int j=0; j<list.size(); j++)
                {
                    Target t = list.get(j);
                    if(s.Sound==true) t.hit();
                    int x = t.getX();
                    int y = t.getY();
                    DestroyedTarget d = new DestroyedTarget();
                    d.setImage(t.getImage());
                    d.vel.setX(t.vel.getX());
                    s.removeObject(t);
                    s.addObject(d,x,y);
                    if(s.Blood==true)
                    {
                        s.backGround.setBackGround(x,y);
                    }
                    s.hitCount++;
                    hitCount++;
                }
        }
    }
This is what it looks like:
danpost danpost

2012/11/4

#
Go back to using actors for the stains. Use the World class method 'setPaintOrder(class, class, ...)' to insist on what classes you want on top. You can place the method call in the world constructor.
ErikO ErikO

2012/11/5

#
Great! Thanks allot :)
You need to login to post a reply.