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

2012/10/11

Removing an object that is a certain size?

1
2
MoneyFishSlap MoneyFishSlap

2012/10/11

#
I have a meteor that gets smaller every time it is hit by a missile and i want to make it so that when it is a certain size it would get deleted from the world.
Razzo Razzo

2012/10/11

#
In your act statement for your meteor, add this..
if(size < 5) // size being your Variable 5 Being the  Biggest size to remove
getWorld().removeObject(this);
danpost danpost

2012/10/11

#
Actually, it would be better to place the check in the 'hit' method (or the method that would reduce the size of the object). Right after reducing the size, check it to see if it is less than your minimum size and remove it, if so.
MoneyFishSlap MoneyFishSlap

2012/10/11

#
"cannot find symbol - variable size"
Razzo Razzo

2012/10/11

#
As I stated. Size is your variable, Which contains the actual size. Just change size to however big (or small) you want it. And yeah, listen to danpost, Putting this into your hit statement is much better!
danpost danpost

2012/10/11

#
Use 'getImage().getWidth()' instead of 'size'.
Razzo Razzo

2012/10/11

#
danpost! Stop making me feel like an Idiot! :D
MoneyFishSlap MoneyFishSlap

2012/10/11

#
I new to all this so treat me like a 5 year old,i get confused easily.
SPower SPower

2012/10/11

#
getImage() returns the image of the asteroid (if the code is in the Asteroid class), and the getWidth() method an an image returns the width of the image.
danpost danpost

2012/10/11

#
@Razzo, Sorry, I am just trying to help out. He never said anything about having a variable called 'size' and really there is no need for one if the size of the image can be acquired. When I started helping out here, I was often corrected, by davmac, no less. Think how I felt! Actually, the advice you gave was not bad, per se; it just was not optimal. Do not feel bad. And, again, my intention was not to make you feel 'like an Idiot'. Well, my appologies .
MoneyFishSlap MoneyFishSlap

2012/10/11

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cloud here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Meteor extends Mover
{
    /**
     * Act - do whatever the Cloud wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    
    move (4);
    if (this.atWorldEdge()) 
    {
        turn (180);
    }
    
    if (Greenfoot.getRandomNumber(100) < 10)
    {
        turn (Greenfoot.getRandomNumber(90) - 45);
    }
    Actor missle;
    missle =  getOneObjectAtOffset(0, 0, Missle.class);
    if (missle != null) 
    {
       GreenfootImage image = getImage();  
       image.scale(image.getWidth() - 10, image.getHeight() - 10);  
       setImage(image);
    {
       World world;
       world = getWorld();
       world.removeObject(missle);
    }
    if(getImage().getWidth() < 1)
    {
        getWorld().removeObject(this);
    }
    }
    }  
  }
MoneyFishSlap MoneyFishSlap

2012/10/11

#
Thats what i got so far but it dosent get deleted and still comes up with the error message saying ""java.lang.IllegalArgumentException: Width (0) and height (0) cannot be <= 0
SPower SPower

2012/10/11

#
The problem is here:
image.scale(image.getWidth() - 10, image.getHeight() - 10);
if image.getWidth() and/or image.getHeight() is 10, it will be zero (since you substract 10). If you scale an image, you can't make the width or height smaller or equal to 0. Do this:
int width = image.getWidth() -10;
int height = image.getHeight() -10;
if (width > 0 && height > 0) {
     image.scale(width, height
}
to prevent it being smaller or equal to 0.
danpost danpost

2012/10/11

#
It is not good practice to scale an image multiple times, though in this case you may get away with it. Better is to create a new GreenfootImage from the image file and scale it to the new size.
MoneyFishSlap MoneyFishSlap

2012/10/11

#
Say if i wanted to remove it when it was a quater of it's original size, what number should i replace * with?
if(getImage().getWidth() < *)
There are more replies on the next page.
1
2