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

2015/11/10

combine two for loops together

davemib123 davemib123

2015/11/10

#
Hi All, having a little difficultly here, how can I combine these two for loops together:
1
2
3
4
5
6
for(int i=514 ; i<650 ; i+=60) {
            addObject(new Coin(), i, 304);
        }
        for(int i=548 ; i<650 ; i+=60) {
            addObject(new Coin(), i, 259);
        }
Many thanks!
danpost danpost

2015/11/10

#
Well, let us look at where you will be placing coins: (514, 304), (574, 304), (634, 304) -- from the first loop (548, 259), (608, 259) -- from the second loop Now, it seems with the two rows of coins that one row is not evenly spaced with the other (548-514 = 34 and 574-548 = 26). I am sure there are ways to combine the for loops with the original locations I gave above; but, it would be much easier to do it when the coins were evenly spaced with each other. For example:
1
for (int i=0; i<5; i++) addObject(new Coin(), 514+30*i, 304-55*(i%2));
To keep them unevenly spaced to the original locations, change '514+30*i' to '514+30*i+4*((i+1)%2)'.
davemib123 davemib123

2015/11/12

#
great. just what i wanted :)
lordhershey lordhershey

2015/11/12

#
Have you thought of using a file to inject objects into the world? It could make world construction a bit easier.
davemib123 davemib123

2015/11/12

#
i've had an attempt at making a tile engine, which seems to run fine. except for when i want to position items to a particular unit.
lordhershey lordhershey

2015/11/12

#
I saw a trick someone used making a world map using an image and I thought it was a great solution. I have used it in the platform space and helicopter games.
davemib123 davemib123

2015/11/12

#
i know i've seen your games, i'm not a your level of java understanding yet. But i'm slightly better than I was before :)
You need to login to post a reply.