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

2020/6/2

Help with for loops

Mattzh Mattzh

2020/6/2

#
How would I be able to use a for loop to create a 5x10 set of a specified actor in the prepare() of myWorld? An example of code and explanation would be greatly appreciated.
danpost danpost

2020/6/2

#
Mattzh wrote...
How would I be able to use a for loop to create a 5x10 set of a specified actor in the prepare() of myWorld? An example of code and explanation would be greatly appreciated.
What have you tried? How is it different from what you want?
Mattzh Mattzh

2020/6/2

#
As of now, I have 5x6 (height x length) set of actors located at the top that are not centered. I am attempting to create a 5x10 set of actors that are centered at the top. I repeated the code 5 times at different y-values, but I believe I am doing something wrong as I am thinking that there is a shorter way to do it.
private void prepare()
    {
        spaceship spaceship = new spaceship();
        addObject(spaceship,300,550);
        for(int i=4; i<=9; i++) 
        {
            alien alien = new alien();
            addObject(alien,30+40*i, 15);
        }
        for(int i=4; i<=9; i++) 
         {
            alien alien = new alien();
            addObject(alien,30+40*i, 45);
            
        }
        for(int i=4; i<=9; i++) 
        {
            alien alien = new alien();
             addObject(alien,30+40*i, 75);
        }
        for(int i=4; i<=9; i++) 
        {
            alien alien = new alien();
            addObject(alien,30+40*i, 105);
        }
        for(int i=4; i<=9; i++)
        {
            alien alien = new alien();
            addObject(alien,30+40*i, 135);
        }
    }
danpost danpost

2020/6/2

#
With the following variables:
int apr = 10; aliens per row
int gap = 40; // distance between aliens along a row
int midX = getWorld()/2; // mid-world x-coordinate
you can compute the x-coordinate of the left-most alien in a row:
int x0 = midX-(apr-1)*gap/2;
Then a double loop can be constructed to add the aliens in the world, centered:
for (int row=0; row<5; row++) // for each row
{
    for (int n=0; n<apr; n++) // for each alien in a row
    {
        addObject(new alien(), x0+gap*n, 15+30*row);
    }
}
Mattzh Mattzh

2020/6/2

#
It works, thank you! While testing the game, I found an error for a part that is unrelated to the for loops, and was wondering how I could fix these. The error says " java.lang.NullPointerException at alien.displayScore(alien.java:50) corresponds to line 42 below at alien.act(alien.java:20) corresponds to line 12 below at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) "
public class alien extends Actor
{
    public static int score = 0;
    /**
     * Act - do whatever the alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       alienShoot();
       destroyAlien();
       displayScore();
    }  
    public boolean canSee(Class otherActor)//Actor detects other actors
    {
        Actor actor = getOneObjectAtOffset(0,0,otherActor);
        
        if(actor != null)
        {
            return true;
        }
        
        else
        {
            return false;
        }
        
    
    }
    public void destroy(Class otherActor)//Actor deletes the actor from the map
    {
        Actor actor = getOneObjectAtOffset(0,0,otherActor);
        
        if(actor != null)
        {
            getWorld().removeObject(actor);
        }
        
    }
    public void displayScore() //Score is shown
    {
        getWorld().showText("Aliens Destroyed: "+score, 125,30);

    }
    public void destroyAlien()
    {
        if(canSee(laser1.class))
        {
            removeTouching(laser1.class);
            getWorld().removeObject(this);
            score++;
        }
    }
    private void alienShoot()
    {
        if (Greenfoot.getRandomNumber(1000)>=999) //1 in 1000 chance that an alien will generate and shoot a laser
        {
            laser2 laser2 = new laser2();
            getWorld().addObject(laser2, getX(),getY());
        }
    }
}
danpost danpost

2020/6/2

#
Remove line 12. Move to and modify the displayScore method to be in your World subclass. Call it from act method in that class.
Mattzh Mattzh

2020/6/2

#
I am having trouble doing what you had suggested - I'm getting an error with the +score part of showText("Aliens Destroyed: "+score, 125,30); in myWorld, because the score variable is located in the alien class. I'm not sure how to fix this.
danpost danpost

2020/6/3

#
Mattzh wrote...
I am having trouble doing what you had suggested - I'm getting an error with the +score part of showText("Aliens Destroyed: "+score, 125,30); in myWorld, because the score variable is located in the alien class. I'm not sure how to fix this.
'score' should be modified to 'alien.score'.
Mattzh Mattzh

2020/6/3

#
It works now; appreciate your time helping out!
You need to login to post a reply.