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

2013/5/22

loops help

Robert2.0 Robert2.0

2013/5/22

#
I trying to make a platform game I need to add loops. Can people give me examples of how to integrate loops in a platform game.
danpost danpost

2013/5/22

#
What do you want the loops to do?
Robert2.0 Robert2.0

2013/5/22

#
danpost wrote...
What do you want the loops to do?
I don't know. That's why I open this discuss. I wondering at what situation would I use a loop.
danpost danpost

2013/5/23

#
Maybe you can come up with some ideas after reviewing what they are and how they can be used. The Java tutorials has a section on Control flow statements. The linked page breaks down the different type of flow (branching/looping/etc). Review the several pages dealing with the looping statements (for/while/do). You can traverse the pages using the 'Next'/'Previous' tags or via the page links on the left side of the page.
Robert2.0 Robert2.0

2013/5/23

#
danpost wrote...
Maybe you can come up with some ideas after reviewing what they are and how they can be used. The Java tutorials has a section on Control flow statements. The linked page breaks down the different type of flow (branching/looping/etc). Review the several pages dealing with the looping statements (for/while/do). You can traverse the pages using the 'Next'/'Previous' tags or via the page links on the left side of the page.
Thanks for that, but I'm still a little confused on "for" statement. Could you give me example?
danpost danpost

2013/5/23

#
There are two syntax structures for the 'for' statement: (1) for (index, condition, increment) Lets say you wanted to add all the numbers from one to ten. You would start the index at one, increment by one, and continue adding as long as the index was not greater than 10.
int sum = 0; // initial sum field
for (int index = 1; index <= 10; index++)
{
    sum += index; // add the number 'index' holds to the sum field
}
The result will be that 'sum' will hold the value of fifty-five.
Robert2.0 Robert2.0

2013/5/23

#
danpost wrote...
There are two syntax structures for the 'for' statement: (1) for (index, condition, increment) Lets say you wanted to add all the numbers from one to ten. You would start the index at one, increment by one, and continue adding as long as the index was not greater than 10.
int sum = 0; // initial sum field
for (int index = 1; index <= 10; index++)
{
    sum += index; // add the number 'index' holds to the sum field
}
The result will be that 'sum' will hold the value of fifty-five.
What would be a game like scenario for this statement?
danpost danpost

2013/5/23

#
There are two syntax structures for the 'for' statement: (1) for (index, condition, increment) Lets say you wanted to add all the numbers from one to ten. You would start the index at one, increment by one, and continue adding as long as the index was not greater than 10.
int sum = 0; // initial sum field
for (int index = 1; index <= 10; index++)
{
    sum += index; // add the number 'index' holds to the sum field
}
The result will be that 'sum' will hold the value of fifty-five. (2) for (Object item : itemList) The loop will execute on each item of the list. Lets say you wanted to find which actor in the world has the lowest 'getY' value (from a world class).
Actor nearestTop = null; // to hold the closest object found so far (if any)
for (Object obj : getObjects(null)) // for each object (actor) in the world
{
    Actor actor = (Actor)obj; // cast object to Actor
    if (nearestTop == null || nearestTop.getY() > actor.getY())
    { // if we do not yet have an actor held as close or if the actor held as close is not as close as this actor from the list
        nearestTop = actor; // hold this actor from the list
    }
}
'nearestTop' will either contain 'null' if no actors are in the world or the actor whose center is closest to the top edge of the world.
You need to login to post a reply.