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

2018/11/30

How to make a bullet disappear when its at a certain size

Samwcool Samwcool

2018/11/30

#
I am making a 3D game and when the ship shoots a bullet it shrinks to make it look like its going into the distance. My problem is when it's width and length reach 0 it comes up with an error, i need to work around this error by making the bullet disappear when it reaches width 1 and length 1 so the game doesn't get an error. the error: java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0 at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:189) at greenfoot.GreenfootImage.scale(GreenfootImage.java:408) at Bullet.shrink(Bullet.java:39) at Bullet.act(Bullet.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
    public void shrink()
    {
        int percentage = 99;
        GreenfootImage image = getImage();
        image.scale(image.getWidth()*percentage/100, image.getHeight()*percentage/100);
    }
Thats the code i am using to make the bullet shrink
danpost danpost

2018/12/1

#
Samwcool wrote...
I am making a 3D game and when the ship shoots a bullet it shrinks to make it look like its going into the distance. My problem is when it's width and length reach 0 it comes up with an error, i need to work around this error by making the bullet disappear when it reaches width 1 and length 1 so the game doesn't get an error. << Error Omitted >> << Code Omitted >>
You could call shrink only if the image is large enough to shrink:
if (bullet.getImage().getHeight > 1) shrink();
or you just remove the bullet:
if (bullet.getImage().getHeight() < 2) getWorld().removeObject(this); else shrink();
Samwcool Samwcool

2018/12/2

#
That worked thanks
You need to login to post a reply.