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

2016/5/24

Character not spawning after countdown

xlRenn xlRenn

2016/5/24

#
Hello, I implemented a counter in my enemy class. It's supposed to respawn itself at intervals. I have:
public void act() 
    {
        respawn --;
        countdown --; //prevents character from getting stuck at the edge
        move(movespeed);
        check();
        addthis();
        
        }
and the respawn is implented in addthis()
public void addthis()
    {
        if(respawn == 0)
        {
            getWorld().addObject(this, Greenfoot.getRandomNumber(540)+20, 287);
            respawn = 10;
        }
    }
I have this in my class;
private int respawn = 10;
Any idea as to why this is not making the character spawn? I'm pretty sure it worked for my past worm game, so I'm confused as to why it's not working here.
danpost danpost

2016/5/24

#
You are trying to add 'this' into the world, which is the enemy that is already in the world. Anytime you want to create a new object of a class, you need to call the constructor of the class -- this is done by using the 'new' keyword on the class name. For example:
Enemy enemy = new Enemy();
Then you can add 'enemy' (the newly created object) into the world.
xlRenn xlRenn

2016/5/24

#
Ah, thank you! That makes sense. What if I wanted to do this mechanism through World? Is it the same thing I'm missing? Because I realized if I put the code in the enemy class itself, the enemy population simply grows exponentially and will cease to grow if all the enemies are killed. I tried doing the same thing in world (taking out the getWorld() command), but it's not spawning enemies. I have the increment countdown in public myWorld(); and calling the spawning mechanism in a separate method.
xlRenn xlRenn

2016/5/24

#
I think it has something to do with the countdown not working in world. Not sure though. Here it is:
public MyWorld()
    {    
        super(600, 400, 1); 
        spawnSabs --;
        prepare();
        spawnSableyes();
    }
and the method;
public void spawnSableyes()
    {
        if(spawnSabs == 0)
        {
            Sableye sableye = new Sableye();
            addObject(sableye, Greenfoot.getRandomNumber(540)+20, 287);
            spawnSabs = 90;
        }
    }
and defined as
private int spawnSabs = 90;
SPower SPower

2016/5/24

#
You're calling spwanSabs-- only in the constructor, which gets excecited only once, namely when the world is created. You need to move that code to the act method.
xlRenn xlRenn

2016/5/24

#
That makes sense, but I don't think the world has an act method? So basically I have to do the mechanism through an actor.
SPower SPower

2016/5/24

#
It most certainly does have an act method! If you look at the greenfoot documentation for World, you can see it has an act method that you can overwrite. It's a frequent problem that people forget that a world can act too, probably because it's not shown by default the way it's shown in an Actor subclass (where it's there for you to fill in, while in World subclasses it only shows the constructor).
xlRenn xlRenn

2016/5/24

#
OMG, thank you so much!!! Never knew that!
SPower SPower

2016/5/24

#
In that case, I encourage you to look at the Greenfoot documentation in more detail some time. It's easy to learn stuff by just looking at an API, so if you've got the time, definitely check it out :)
You need to login to post a reply.