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

2013/5/17

help

Avik Avik

2013/5/17

#
List<Object> objects = getObjects(coin.class); for (Object obj : objects) { coin coin = (coin)obj; coin.setLocation(coin.getX()+3);(coin.getY()+3); } it says not a statement after I compile. how do I change this
danpost danpost

2013/5/17

#
Please show the code for the class that this snippet is in.
Avik Avik

2013/5/17

#
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.List; /** * A class that has a scrolling background image * * @author avik */ public class ScrollWorld extends World { private static final GreenfootImage bgImage = new GreenfootImage("Picture1.png"); private static final int scrollSpeed = 1; private GreenfootImage scrollingImage; private int scrollPosition = 0; public ScrollWorld() { super(800, 400, 1); GreenfootImage background = new GreenfootImage(800, 400); scrollingImage = getScrollingImage(800, 400); background.drawImage(scrollingImage, 0, 0); setBackground(background); addObject(new balloon(), 350, 200); prepare(); } public void act() { if(scrollSpeed > 0 && scrollPosition <= 0) { scrollPosition = getWidth(); } if(scrollSpeed < 0 && scrollPosition >= getWidth()) { 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, position, 0); bg.drawImage(scrollingImage, position - scrollingImage.getWidth(), 0); } /** * 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; List<Object> objects = getObjects(coin.class); for (Object obj : objects) { coin coin = (coin)obj; coin.setLocation(coin.getX()+3);(coin.getY()+3); } } /** * 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

2013/5/17

#
You placed the code after a 'return' statement that will always execute. Therefore the snippet is actually unreachable where it is placed. The compiler was not expecting code there; hence, the error. Your 'scrollPosition' appears to change by one every act cycle. In fact, being 'scrollSpeed' is 'final' (never changes), you can change your act method to:
public void act()
{
    scrollPosition = (scrollPosition-scrollSpeed+getWidth())%getWidth();
    paint(scrollPosition);
    for (Object obj : getObjects(Coin.class))
    {
        Coin coin = (Coin)obj;
        coin.setLocation(coin.getX()+scrollSpeed, coin.getY());
    }
}
This should make your coins go with the background. If going the wrong direction, just change the sign from '+' to '-' in the last statement.
You need to login to post a reply.