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

2014/5/3

Help Making a object break up to other objekt/images

1
2
nc-munk nc-munk

2014/5/3

#
I Have a old code of breaking a actor up in smaller ones, but a would like it to break up to other images. there is alot of things in the code from the old game that have to change or removed. so I hoped that someone would take a look at it
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Asteroid here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Asteroid extends Mover
{
    int health = 50;
    public void act()
    {         
        move (4);
        setRotation(90);
    }
        /**
     * Hit this asteroid dealing the given amount of damage.
     */
    public void hit(int damage) 
    {

        health = health - damage;
        if(health <= 0)
        {
            //Cyberspace.countScore(10);
           
            breakUp(); 
        }
    }
        /**
     * Break up this Asteroid. If we are still big enough, this will create small ones
     */
    private void breakUp() 
    {
        
        if(health <= 0) 
        {
            getWorld().removeObject(this);
        }
        else 
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();

            getWorld().removeObject(this);
        }      
    }
}
thanks in advance
danpost danpost

2014/5/3

#
For one thing, I noticed that lines 47 and 48 call an Asteroid constructor with 2 arguments (looks like 'int, Vector'). You will need a Vector class and an asteroid constructor that takes these arguments (I do not know if either was included in your Mover class). Next thing I noticed was the re-checking if the health was gone. It is checked before calling 'breakUp' and then checked again within the method. I think the one inside the method should be checking the current size of the asteroid to see if it is too small to break up any further.
nc-munk nc-munk

2014/5/3

#
I have a vector class. but do you think it is easier to start from scratch or just change some of the thing in this code
danpost danpost

2014/5/3

#
I does not look like you would have to change too much. I would say it might be easier to just adjust this class.
nc-munk nc-munk

2014/5/4

#
what can I use instead of health because size of the asteroid irrelevant, when it break up I want it to replaced with three other images that then will fly out in different directions
danpost danpost

2014/5/4

#
Are you saying that the initial asteroid will break up into three parts and that those three parts will, without breaking up any more, fly off in different directions? What happens to them, then? They are still asteroids and can still break up more, right? into three even smaller pieces flying off in different directions? There has to be a limit as to how small they will eventually break up into. The size of the asteroid is very relevant. You really do not a health field as you can just use something like 'if (getImage().getWidth() < 10) getWorld().removeObject(this); else breakUp();'. Of course the '10' can be adjusted to your desire.
nc-munk nc-munk

2014/5/4

#
they are a image of parts of the asteroid. I want it to only break up ones and fly in different directions toward the the bottom and if they hit the spaceship deal damage, they will just fly of and disappear then hit the edge
nc-munk nc-munk

2014/5/4

#
like is picture
nc-munk nc-munk

2014/5/4

#
sorry here
danpost danpost

2014/5/4

#
Two ways: (1) create a separate class for the fragments; or, (2) add an instance boolean 'isFragment' field to the Asteroid class and set it to true for the fragments; and add at line 22 something like 'if (isFragment) return;', so the health value does not decrease and try to create more fragments from it.
nc-munk nc-munk

2014/5/4

#
okay but how do I make them go in random directions
danpost danpost

2014/5/4

#
nc-munk wrote...
okay but how do I make them go in random directions
Use 'setRotation(Greenfoot.getRandomNumber(/** some value */)+(/** some other value */);'. The first value is the range (use '360' for any which way), the other value is the minimum value.
nc-munk nc-munk

2014/5/4

#
within the asteroid actor or the ones it splits up to
danpost danpost

2014/5/4

#
nc-munk wrote...
within the asteroid actor or the ones it splits up to
In the constructor of the ones it splits up into.
nc-munk nc-munk

2014/5/4

#
and by the constructor you mean what
There are more replies on the next page.
1
2