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

2019/5/7

Attempting to spawn actors on a timer

HappyDragon78 HappyDragon78

2019/5/7

#
In trying to create code that spawns actors every few seconds, I get the error "Can't find symbol - method mainEnemy" This is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MainPlayer here.
 * 
 * @author (Danny) 
 * @version (1.0)
 */
public class MainPlayer extends Actor
{
    int speed = 3;
    
    public void act()
    {
        moveAround();
        checkObstacle();
        throwBall();
        
    }

private void enemySpawn()
    {
        spawnTimer = (spawnTimer+10)%600;
        
        if (spawnTimer == 0)
        {
            
            MainEnemy mainEnemy = getWorld().getObjects(MainEnemy.class).get(0);
            
            getWorld().addObject(mainEnemy(), 0, 0);
        }
    }
}
I assume I need to set MainPlayer equal to something, but I haven't been able to find a solution.
Super_Hippo Super_Hippo

2019/5/7

#
"mainEnemy()" is a method, "mainEnemy" is a variable. In your case, you set "mainEnemy" to a "MainEnemy" object in the world. You want to spawn new actors though. So you can remove line 28 and change "mainEnemy()" in line 30 to "new MainEnemy()". You need to add a call to the method from the act method. I suggest to add the "enemySpawn" method into your world subclass instead, though.
You need to login to post a reply.