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

2013/5/12

coins

Avik Avik

2013/5/12

#
I have placed coins in my game, there is a hot air balloon moving up and down while the background is scrolling along. but my coins are moving with the background meaning the balloon can't get the coins. I want the coins to stay in place but the background to still scroll. how do I do this?
Avik Avik

2013/5/12

#
actually I need the coins to move with the background
danpost danpost

2013/5/12

#
You will need to post the code you have for scrolling the background.
Gevater_Tod4711 Gevater_Tod4711

2013/5/12

#
Therefore your coins would have to move the same distance as the world every time the world is moved. If you know how much your world is moved in x and y direction you can do it like this:
public void moveCoins(int dx, int dy) {
    List<Coin> coins = getObjects(Coin.class);
    for (Coin coin : coins) {
        coin.setLocation(coin.getX() + dx, coin.getY() + dy);
    }
}
To use this method you have to declare it in a world subclass and you have to import java.util.List;
Avik Avik

2013/5/12

#
it says illegal start of expression
Avik Avik

2013/5/12

#
what do I do know?
danpost danpost

2013/5/12

#
Please show the area around which you placed this code. Also, you will probably need to type cast what is being assigned to the List
Avik Avik

2013/5/12

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class coin here. * * @author (your name) * @version (a version number or a date) */ public class coin extends Actor { /** * Act - do whatever the coin wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { public void moveCoins(int dx, int dy) { List<Coin> coins = getObjects(Coin.class); for (Coin coin : coins) { coin.setLocation(coin.getX() + dx, coin.getY() + dy); } } } }
danpost danpost

2013/5/12

#
That code was supposed to go in your world class (and as a separate method called when you scroll your background.
Avik Avik

2013/5/16

#
post me the code
danpost danpost

2013/5/16

#
Remove what code you have already tried for moving the coins with the background and add the following in with the code that does scroll the background.
// with changes in scroll amount of 'dx' and 'dy'
for (Object obj : getObjects(Coin.class)) {
    Coin coin = (Coin)obj;
    coin.setLocation(coin.getX()+dx, coin.getY()+dy);
}
You need to login to post a reply.