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

2017/5/17

for loops

ali18 ali18

2017/5/17

#
I have no idea what for loops are.. Can someone explain to me what for loops are and what they are used for? Also, can you give me other different types of loops?
danpost danpost

2017/5/17

#
See this page of the java tutorials for information on the 'for' statement.
ali18 ali18

2017/5/17

#
What can i use it for within my game? Am i able to use use a for loop for my lives? or counter?
danpost danpost

2017/5/17

#
ali18 wrote...
What can i use it for within my game? Am i able to use use a for loop for my lives? or counter?
The 'for' statement is a code structure that controls the flow of execution of your code. It does not have a value of it own -- so, using it for your lives or a counter just makes no sense. It can be used, possibly, to create the display of the number of lives left. The for loop would then repeatedly execute code to add one image icon to the world for each life. For example:
for (int i=0; i<lives; i++) addObject(new Life(), 50+i*30, 50);
With this, if the value off 'lives' was '3', then 3 Life objects (an image icon representing one remaining life) would be added to the world at locations (50, 50), (80 50) and (110, 50).
ali18 ali18

2017/5/23

#
So, could i use this for loop for my lives? I want it to show my lives, as my actor has three lives before it is game over? Where should i implement the code that you have given above in my game?
ali18 ali18

2017/5/25

#
The code that you have gave me, i have used it but it didn't come up as incorrect but it also didn't do any changes to my game as well?
Super_Hippo Super_Hippo

2017/5/25

#
Show how and where you implemented it.
danpost danpost

2017/5/25

#
Since the number of lives can change as the player loses them, the code may need to be executed multiple times. Therefore, placing the code in a separate method would be the thing to do. Have it remove any icons previously placed before adding the new number of lives. In the world class, you could have this:
public void adjustlives(int amount)
{
    lives += amount;
    removeObjects(getObjects(Life.class));
    for (int i=0; i<lives; i++) addObject(new Life(), 50+i*30, 50);
}
Then, in the world constructor, you would have:
adjustLives(3);
to initially display the number of lives remaining. In the world act method, you could have something like the following:
if (player.getWorld() != this)
{
    adjustLives(-1);
    if (lives < 0) gameOver(); else resetLevel();
}
ali18 ali18

2017/6/6

#
OK, I have added the top part of the code within my level1 class, but at the moment nothing has occurred. I don't know what you mean by world constructor. I don't know where i should implement the rest of the code. At the moment, as you can see below, i have only done this:
public class level1 extends levels
{
    int lives;
    private static final int GAP = 30; // This will allow a specific gap between each 'Person'
    private void createRows()
    {
        int y = 80; // This is the location the row will start from in the world from the y axis
        while(y <= 700) // This is to adjust how many rows of people will there be in the world
        {
            createRows(y); // This is to create how many row of person you want depending on the y axis
            y = y + 240 + GAP; // This is the calculation for the spaces between each Persons in the World
        }
    }
    
    private void createRows(int y)
    {
        int x = 50; // This is the location the row will start from in the world from the x axis
        while(x < 900) // if the ‘x’ are less than or equal to 900
        {
            addObject (new person(), x, y); // A new Person will be created at the coordinated that has been created at
            x = x + 150 + GAP; // This is the calculation for the spaces between each row of people in the World
        }
    }
    
    public void adjustlives(int amount)
{
    lives += amount;
    removeObjects(getObjects(HealthBar.class));
    for (int i=0; i<lives; i++) addObject(new HealthBar(), 50+i*30, 50);
}
danpost danpost

2017/6/6

#
Okay. First, let me correct a typo in what is now your line 25 above, which should be:
public void adjustLives(int amount)
Now, your world constructor should begin like this:
public level1()
You may or may not have some parameters between the parenthesis. Curiously, I am not sure why you named the class that creates objects that represent lives 'HealthBar'.
You need to login to post a reply.