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

2014/5/3

please will someone look at my code

1
2
nc-munk nc-munk

2014/5/3

#
this is the third time I post this so please help I am making a game for a school project do for sunday, so please take a look. I need help with a random spawn function it is should spawn an asteroid in top for the screen and move down and i cant make them spawn
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot) 

public class ScrollWorld extends World 
{ 
    private static final GreenfootImage bgImage = new GreenfootImage("space1.gif");
    private static final int scrollSpeed = -4;

    private GreenfootImage scrollingImage;
    private int scrollPosition = 0;
    private int startAsteroid = 1;
    public int pause = 100;

    public ScrollWorld()
    {
        super(400, 600, 1);

        GreenfootImage background = new GreenfootImage(400, 600);
        scrollingImage = getScrollingImage(400, 600);
        background.drawImage(scrollingImage, 0, 0);
        setBackground(background);

        addObject(new SpaceShip(), 200, 570);
        //addAsteroids(startAsteroid);
        prepare();
    }
    
    public void addAsteroid()
    {
        Asteroid asteroid = new Asteroid();
        if (pause>0)
        {
            pause--;
        }
        if (pause ==0)
        {
            int x = Greenfoot.getRandomNumber(400);
            int y = (2);
            addObject(new Asteroid(), x, y);
            //asteroid.setRotation();
        }
    }
    
    public void act()
    {
        if(scrollSpeed < 0 && scrollPosition <= 0) 
        {
            scrollPosition = getHeight();
        }
        if(scrollSpeed < 0 && scrollPosition >= getHeight()) 
        {
            scrollPosition = 0;
        }
        scrollPosition -= scrollSpeed;
        paint(scrollPosition);
    }

    /**
     * Paint scrolling image at given position and make sure the rest of
     * the background is also painted with the same image.
     */
    private void paint(int position)
    {
        GreenfootImage bg = getBackground();
        bg.drawImage(scrollingImage, 0, position);
        bg.drawImage(scrollingImage, 0, position - scrollingImage.getWidth());
    }

    /**
     * Returns an image with the given dimensions.
     */
    private GreenfootImage getScrollingImage(int width, int height)
    {
        GreenfootImage image = new GreenfootImage(width, height);
        for(int x = 0; x < width; x += bgImage.getWidth()) 
        {
            for(int y = 0; y < height; y += bgImage.getHeight()) 
            {
                image.drawImage(bgImage, x, y);
            }
        }
        return image;
    } 

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
    }
}
my code look in line 27 to 42
danpost danpost

2014/5/3

#
Move lines 34 through 40 up inside the previous 'if' block (or move the curly bracket at line 33 to line 41). You only want that block to execute when the counter decrements to zero; not at all times it is zero. Also, you need to reset the 'pause' timer back to '100' within this block you are moving up.
nc-munk nc-munk

2014/5/3

#
Thanks for the reply but I dont really understand what you want me to change
danpost danpost

2014/5/3

#
Change your 'addAsteroid' method to this:
public void addAsteroid()
{
    Asteroid asteroid = new Asteroid();
    if (pause > 0)
    {
        pause--;
        if (pause == 0) // at time 'pause' reaches '0'
        {
            int x = Greenfoot.getRandomNumber(400);
            int y = (2);
            addObject(new Asteroid(), x, y);
            pause = 100; // add this line
            //asteroid.setRotation(?int?);
        }
    }
}
nc-munk nc-munk

2014/5/3

#
Ahh okay I think I gets it now thanks
nc-munk nc-munk

2014/5/3

#
for some reason it wont add an asteroid, but it could be because of the world image i scroling it overlap the image of the asteroid
danpost danpost

2014/5/3

#
If you are using Actor object(s) for you background image, you may want to add a 'setPaintOrder' statement in your world class constructor.
nc-munk nc-munk

2014/5/3

#
I dont it is all in the code above
danpost danpost

2014/5/3

#
Remove line 3 from my code post above. Then add 'addAsteroid();' to your act method (you need to call the method for it to work).
nc-munk nc-munk

2014/5/3

#
sorry but how do you call a method
danpost danpost

2014/5/3

#
addAsteroids();
This line calls the 'addAsteroids' method within the class. Adding that line to your 'act' method will have that method execute its code once each act cycle.
nc-munk nc-munk

2014/5/3

#
maybe I am a little stupid but I dont get it. I dont have a line with addAsteroid besides public void addAsteroid ()
danpost danpost

2014/5/3

#
nc-munk wrote...
maybe I am a little stupid but I dont get it. I dont have a line with addAsteroid besides public void addAsteroid ()
That is my point. You need to ADD the line, as I posted above, within your 'public void act()' code block (without the 's' at the end of the method name).
nc-munk nc-munk

2014/5/3

#
wow sorry. but still nothing will work
danpost danpost

2014/5/3

#
Post your ScrollWorld class code as it is now.
There are more replies on the next page.
1
2