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

2017/4/13

Adding a new enemy when the counter reaches a certain number

nanakh nanakh

2017/4/13

#
Hello all, I want to try and add a new meteor (enemy) when my counter reaches 5 and I want to do this with when the counter reaches 10, 20, 30, 40, 50, 60,70, 80 and 90. The counter adds a point each time you (the rocket) dodges the meteor and the meteor touches the left edge of the world. I'm a bit stuck with this and need some help. I am a beginner at Greenfoot and this is for a project in school. I tried looking everywhere and couldn't find much. please help and ask for any code needed.
Nosson1459 Nosson1459

2017/4/13

#
As of now I can't help you get the value of the counter since I don't know anything about it, but I can help you once you have it's value. To see if it's a multiple of ten you can do:
if (value % 10 == 0 || value == 5) {
    // if the remainder of the counter's value divided by 10 is 0
    // or if value is equal to five
    addObject(meteor, x, y); // add in your own parameter values
}
The above code will add a meteor when the value is 5 and with every multiple of 10 (not stopping at 90).
Super_Hippo Super_Hippo

2017/4/13

#
I think you also have to make sure that it is not 0.
Nosson1459 Nosson1459

2017/4/13

#
nanakh wrote...
The counter adds a point each time you (the rocket) dodges the meteor and the meteor touches the left edge of the world.
im not sure if you want to add a point every time you dodge a meteor, when a meteor hits the edge, or both. Here is coding that adds a point when the meteor hits the edge.
if(getX() <= -getWidth() /2 ) /*increment score*/;
this is a little more complicated so i'll document it it's for when you pass a meteor (all above coding here should be placed in the meteor class).\
//put this code in the world class.
private Rocket rocket=new Rocket();
addObject(rocket);//replace your line that adds a rocket with this.
public Rocket getRocket()
{
    return rocket;//getter for rocket.
}
//put this in the meteor.

private boolean addedScore=false; //make sure it only adds score once.
//puts this in the act.
World world=(/*name of world*/)getWorld();//get an instance of the world class.
if(getX()<=world.getRocket().getX()&&addedScore==false)//check if the meteor is passed the rocket and if the score was counted already
{
    addedScore=true;//make sure it only counts once
    //increment score
}
Nosson1459 Nosson1459

2017/4/13

#
Yehuda wrote...
Yehuda (1/2 of Nosson1459)
The first post by Nosson1459 is a different person than the second (this information has been mentioned before).
nanakh nanakh

2017/4/13

#
Thank you all for your replies. the a point is added when the meteor touches the left wall and i've done all that. whilst waiting for replies i tried doing something of my own in the act() method of the world subclass i added the following:
public void act()

[code]public void act()
{
    if (counter.getScore() == 1 )
     
    {
        hasCounterReachedNewLevel = true;
        counterReachedNewLevel();
    }
}

public void counterReachedNewLevel() 
{
            int x = 800;
            int y = Greenfoot.getRandomNumber(600);
            addObject(new meteor(), x, y);

        }
        
        
    }
nanakh nanakh

2017/4/13

#
sorry for my stupidity im quiet new at this. the above code is a bit wrong instead of spawning one new meteor it spawns millions
nanakh nanakh

2017/4/13

#
nevermind Nosson1459's solution worked perfectly and because my approach on adding a point when the meteor hits the left edge doesnt work ill try yours and come back with some results thank you all for your replies
You need to login to post a reply.