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
DuckGod DuckGod

2016/10/20

#
I want to make rockets fall out of the sky randomly across the X axis but not on the Y axis but nothing spawns.
public void spawnRocket()
    {
        if(counter == 20)
        {
            counter = 0;
            addObject(new Rocket(), Greenfoot.getRandomNumber(getWidth()),0);
        }
        counter++;
    }
But it dosen't work here is what is on my Rocket class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends shotManager
{
    private int frame = 1;
    private int animationCounter = 0;
    private int shootingSpeed = 8;
    private int counter = 0;
    private GreenfootImage Explosion1 = new GreenfootImage ("Explosion.png");
    private GreenfootImage Explosion2 = new GreenfootImage ("Explosion2.png");
    private GreenfootImage Explosion3 = new GreenfootImage ("Explosion3.png");
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX() + shootingSpeed, getY()+shootingSpeed);
        stop();
    }  
    
    public void stop()
    {
        if(!getIntersectingObjects(Grounds.class).isEmpty())
        {
            shootingSpeed=8;
        }
        else
        {
            shootingSpeed=0;
            animateExplodes();
            stopRemove();
        }
    }
    
    public void stopRemove()
    {
        if(counter % 16==0)
        {
             getWorld().removeObject(this);
        }
    }
    
    public void animateExplodes()
    {
        if(animationCounter % 4==0)
        {
            animateExplode();
        }
    }
    
    public void animateExplode()
    {
        if(frame == 1)
        {
            setImage (Explosion1);
        }
        else if (frame==2)
        {
            setImage(Explosion2);
        }
        else if(frame ==3)
        {
            setImage(Explosion3);
            frame = 1;
            return;
        }
        frame++;
    }
}
DuckGod DuckGod

2016/10/20

#
The spawnRocket command is in my world class.
danpost danpost

2016/10/20

#
DuckGod wrote...
The spawnRocket command is in my world class.
Where in your world class? Show the block of code where it is located.
DuckGod DuckGod

2016/10/20

#
import greenfoot.*;

/**
 * Write a description of class LvlOne2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LvlOne2 extends Platform
{
    private int counter = 0;
    /**
     * Constructor for objects of class LvlOne2.
     * 
     */
    public LvlOne2()
    {
    }
    public void act()
    {
        if(Greenfoot.isKeyDown("space"))
        {
             spawnRocket();
        }
    }
    public void setFields()
    {   
        map = new String[] {"             h          f",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "                         ",
                            "          cb             ",
                            "          ddbbb     bbbbb",
                            "ccbcbcbcbcdddddcbbbbddddd",
                            "ddddddddddddddddddddddddd",
                            "ddddddddddddddddddddddddd",
                            "ddddddddddddddddddddddddd"};
    }  
    public void spawnRocket()
    {
        if(counter == 20)
        {
            counter = 0;
            addObject(new Rocket(), Greenfoot.getRandomNumber(getWidth()),0);
        }
        counter++;
    }
    }

DuckGod DuckGod

2016/10/20

#
that right?
Nosson1459 Nosson1459

2016/10/20

#
try putting the counter in da act
DuckGod DuckGod

2016/10/20

#
still doesn't spawn anything.
danpost danpost

2016/10/20

#
DuckGod wrote...
still doesn't spawn anything.
Right click on your world while the scenario is stopped and click 'Inspect' to make sure that you have a 'LvlOne2' world active.
DuckGod DuckGod

2016/10/20

#
it says no fields
danpost danpost

2016/10/20

#
DuckGod wrote...
it says no fields
What does it say along the top edge about the object type and name?
DuckGod DuckGod

2016/10/20

#
I don't see it.
danpost danpost

2016/10/20

#
DuckGod wrote...
I don't see it.
You do not see it in the frame that says 'No fields'?
DuckGod DuckGod

2016/10/20

#
no
DuckGod DuckGod

2016/10/20

#
wait do you mean the class LvlOne2
danpost danpost

2016/10/20

#
DuckGod wrote...
no
Wait -- if it says there are no fields, then you could not have right-clicked on your world. You must have clicked on an Actor. The world would have a world width, world height, cell size and background image. You need to right click on your world and select 'Inspect'. You may need to just catch the edge of the world to do so (if your world is covered totally by some actor or actors).
There are more replies on the next page.
1
2
3