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:
From the background class:
From the ball class:
This is what it looks like:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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++; } } } |
