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

2016/10/20

Trying to make Rockets fall out of the sky randomly

1
2
3
Nosson1459 Nosson1459

2016/10/20

#
how is counter supposed to ever equal 20? Only if you press "space" 20 times, and how is the rocket supposed to move down? Even if you put counter++; in the act, it still only calls the method spawnRocket() when you press space which by then counter doesn't and never will equal 20.
danpost danpost

2016/10/20

#
DuckGod wrote...
wait do you mean the class LvlOne2
Yes. Is that what it says at the top?
Nosson1459 Nosson1459

2016/10/20

#
my last post doesn't have much to do with inspections cause I was typing during that discussion if you're wondering where I come in. (But i still have those questions)
DuckGod DuckGod

2016/10/20

#
i dont think i clicked on something else because it says on the top class LvlOne2
danpost danpost

2016/10/20

#
Nosson1459 wrote...
how is counter supposed to ever equal 20? Only if you press "space" 20 times, and how is the rocket supposed to move down? Even if you put counter++; in the act, it still only calls the method spawnRocket() when you press space which by then counter doesn't and never will equal 20.
@Nosson1459: The method the counter is in (the 'spawnRocket' method) zeroes the value of counter when it spawns one; so, it will spawn a rocket about 3 per second if the "space" key is held down. The class of the rocket should control its moving down by its act method and possibly remove itself if it reaches the bottom edge of the world window.
DuckGod DuckGod

2016/10/20

#
ik but it dosen't spawn anything when i hold down space, and i've tried pressing space 20 times and it still doesent do anything.
danpost danpost

2016/10/20

#
DuckGod wrote...
ik but it dosen't spawn anything when i hold down space, and i've tried pressing space 20 times and it still doesent do anything.
Insert at line 44 in your Rocket class (as shown previously), the following line:
counter++;
The counter was not being incremented, so on the first act cycle, the rocket was being removed.
DuckGod DuckGod

2016/10/20

#
so like this?
    public void stopRemove()
    {
        if(counter % 16==0)counter++;
        {
             getWorld().removeObject(this);
        }
    }
danpost danpost

2016/10/20

#
DuckGod wrote...
so like this? < Code Omitted >
No. That will also remove the rocket immediately (unconditionally). Like this:
public void stopRemove()
{
    counter++;
    if (counter % 16 == 0)
    {
        getWorld().removeObject(this);
    }
}
DuckGod DuckGod

2016/10/20

#
they still arn't spawning. it must be in LvlOne2 like maybe
public void spawnRocket()
    {
        if(counter == 20)
        {
            counter = 0;
            addObject(new Rocket(), Greenfoot.getRandomNumber(getWidth()),0);
        }
    }
or this one
    public void act()
    {
        if(Greenfoot.isKeyDown("space"))
        {
             spawnRocket();
        }
        counter++;
    }
or is it because i said
if(!getIntersectingObjects(Grounds.class).isEmpty())
        {
            shootingSpeed=8;
        }
danpost danpost

2016/10/20

#
DuckGod wrote...
they still arn't spawning. it must be in LvlOne2 like maybe
Not in there. That should be fine.
or this one
Well, maybe line 7 should be at line 3 in the 'spawnRocket' method. Or, on the other hand, maybe line 3 of the 'spawnRocket' method should read like this:
if (counter >= 20)
or is it because i said
I am actually puzzled about the code starting from there. If a Ground object is found intersecting, it does not seem correct to give it 'shootingSpeed'; and to remove 'shootingSpeed' if no ground is detected.
DuckGod DuckGod

2016/10/20

#
well i gave it shootingspeed because I want the rocket to stop when it hits the ground and explode but i dont think i wrote it right. and on the
or this one
Well, maybe line 7 should be at line 3 in the 'spawnRocket' method. Or, on the other hand, maybe line 3 of the 'spawnRocket' method should read like this:
if (counter >= 20)
do i put it like this
public void spawnRocket()
    {
        if(counter >= 20)
        {
            counter = 0;
            addObject(new Rocket(), Greenfoot.getRandomNumber(getWidth()),0);
        }
    }
danpost danpost

2016/10/20

#
DuckGod wrote...
do i put it like this
Yes. That is fine.
DuckGod DuckGod

2016/10/20

#
ok im sorry this is takeing so long but it still dosent spawn rockets.
danpost danpost

2016/10/20

#
Alright, let us trace it out. Change the 'spawnRocket' method (temporarily) to this:
public void spawnRocket()
{
    if(counter >= 20)
    {
        System.out.println("Spawning rocket");
        counter = 0;
        addObject(new Rocket(), Greenfoot.getRandomNumber(getWidth()),0);
    }
}
and see if you get any terminal output.
There are more replies on the next page.
1
2
3