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

#
okay here
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;
    public int pause = 10;

    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);
        prepare();
    }
    
    public void addAsteroid ()
    {
        addAsteroid();
        if (pause>0)
        {
            pause--;
            if (pause ==0)
            {
                int x = Greenfoot.getRandomNumber(400);
                int y = (2);
                addObject(new Asteroid(), x, y);
                pause = 100;
                //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()
    {
    }
}
danpost danpost

2014/5/3

#
move line 27 to line 44. That line goes in the 'act' method.
nc-munk nc-munk

2014/5/3

#
thanks. btw how do you change the direction for an moving actor
danpost danpost

2014/5/3

#
nc-munk wrote...
thanks. btw how do you change the direction for an moving actor
Please post the code for that actor.
nc-munk nc-munk

2014/5/3

#
never mind I have fix it already, but thanks for the help
You need to login to post a reply.
1
2