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

2013/5/31

Multiple objects

alagon alagon

2013/5/31

#
So my game is a typing game and it spawn all the bombs but none of them start moving. Help?
public void act()
{
        for(int x = 0; x <10 ; x++)
        {
            addObject(new Bomb(), Greenfoot.getRandomNumber(getWidth()-100)+50, 0);
        }
}
danpost danpost

2013/5/31

#
This method (which I would presume, at least indications are, would be in your world class) will produce about 600 bombs every second. I do not think that is what you want (regardless of whether they move or not). I do not know what limits you want to impose on the production of bombs, so it would be difficult to help you out in any decent manner. If responding to this, please be specific as to what you want (rate of production, limit on how many in world at one time, regular or random production, etc.). After the production is regulated properly, then the movement can be worked on.
alagon alagon

2013/5/31

#
Well i got the movement and the bomb. I want it to produce like 1 every 3 seconds. And the problem is just that they do get spawned at a normal rate, but they don't fly down to the screen. Yes the code is in the world
danpost danpost

2013/5/31

#
What code do you have in the Bomb class?
alagon alagon

2013/5/31

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


public class Bomb extends Actor
{ 
int timer = 100;
    public void act() 
{
        do
    {
        
        for (int x = 0; x < 1001; x++)
        {
        //Gif Movement    
        for(int z = 1; z<13; z++)
            {    
                setImage("Missile/" + z + ".png");
                setLocation(getX(), getY()+1);
                Greenfoot.delay(2);
            }
        //Explosion    
            if (getY() >= 500)
        {
            for(int z = 1; z<25; z++)
            {    
            setImage("Explosion/"+ z +".png");
            Greenfoot.delay(3);
            }
            getWorld().removeObject(this);    
        }   
        }
    } while(timer == 100); 
}
}
danpost danpost

2013/5/31

#
For one thing, you do not change the value of 'timer' anywhere; so, the 'do' loop will execute forever. For another thing, using the 'delay' statement will cause everything to freeze while the method executes (the only changes you could see are those that are done within the method that is executing). You need to remove the 'delay' statements and find a way to use your 'timer' field to regulate the animation/movement of the actor. Keep in mind that a loop is already being created by the system executing all act methods of all active objects every frame and this cycle should not be impeded upon (like by using 'delay' or 'wait'). Basically, you need the code to express what you want done each act cycle (each step of execution; not the entirety of all steps at one time). As an example: for the falling of a bomb, coding the following:
public void act()
{
    for (int i=0; i<1001; i++) {
        setLocation(getX(), getY()+1);
    }
}
will immediately show the bomb at the bottom of the screen because the act method is executed in one step. You will not even see the bomb going down the screen; it will just jump there. To have the bomb move down once per act cycle (one step at a time, just remove the loop.
public void act()
{
    setLocation(getX(), getY()+1);
}
If you wanted to have it stop at some point, just give a condition to the movement:
public void act()
{
    if (getY()<500) {
        setLocation(getX(), getY()+1);
    }
}
Now, after it reaches that point, if you want it to explode, you can add an 'else' clause the the 'if statement.
public void act()
{
    if (getY()<500) {
        setLocation(getX(), getY()+1);
    }
    else {
        // some code to make it explode (step-by-step)
    }
}
Remember, the 'for' loop, like you have at line 24 above, will not work; this is where the 'timer' field can be used. It looks like you wanted the 24 images to appear for 3 cycles each; so the animation of the exploding bomb should take 72 act cycles. You can use two timers: one to count to 3 twenty-four times and for every time it reaches 3, increment the second counter. There is another way to do this by using only one counter. Have the one counter go to 72; and each time it is divisible by 3 evenly (timer%3 == 0) change the image using 'timer/3+1'.
private int timer;

public void act()
{
    // if clause as above
    else {
        if (timer%3 == 0) {
            setImage("Explosion/"+(timer/3+1)+".png");
        }
        timer++;
        if (timer == 72) {
            getWorld().removeObject(this);
        }
    }
}
alagon alagon

2013/5/31

#
Thank you so much :D, but my problem isn't the bomb, it is rather the spawning. I can't spawn 2 bombs that move downwards to the screen, when i write the spawn code:
for(int x = 0; x <10 ; x++)
        {
            addObject(new Bomb(), Greenfoot.getRandomNumber(getWidth()-100)+50, 0);
            Greenfoot.delay(100);
        }
It just spawns the bombs one after another, but it does not move them downward towards the screen.
You need to login to post a reply.