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

2016/6/16

changing what an AI actor does each level

kinghunter kinghunter

2016/6/16

#
I'm currently making a game in which a spaceman has to avoid the bullets fired by an alien ship and advances to the next level. I'm stumped how do i change what the alien does, specifically rate of fire and direction in my new levels instead of the same thing each time... any help is greatly appreciated
danpost danpost

2016/6/16

#
kinghunter wrote...
I'm currently making a game in which a spaceman has to avoid the bullets fired by an alien ship and advances to the next level. I'm stumped how do i change what the alien does, specifically rate of fire and direction in my new levels instead of the same thing each time... any help is greatly appreciated
You could do something like this:
if (getWord() instanceof Level1) { << set values for level 1 >> }
if (getWorld() instanceof Level2) { << set values for level 2 >> }
// etc.
for example. The problem is that you cannot do this until the actor is added into a world. Therefore, you could override the 'addedToWorld' method of the Actor class and do it there:
protected void addedToWorld(World world)
{
    if (world instanceof Level1) { ... }
    if (world instanceof Level2) { ... }
    // etc.
}
Still this is a bulky way to code it. Better might be to have each world tell the alien what to do when that world creates it (the world knows what level it is anyway). You could pass the values to the alien using parameters in the constructor or call a method to set the values after creating the alien. One other option is to have a 'static' level number field in the alien class that each world can set a different value to when the world is created (in the world constructor).
You need to login to post a reply.