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

2017/3/9

method in actor class called by specific object in world class

Telko Telko

2017/3/9

#
Good evening guys, I have a problem with the game im programming for school. At the moment im trying to make it that the cars for my traffic spawn with a distance. Therefore I created a Actor subclass "SpawnChecker" with a transparent pic the size of the cars im spawning and a boolean method.
public boolean canSpawn() {
        if (isTouching(traffic.class)) {
            return false;
        } else { 
            return true ;
        }
    }
This method im trying to access in a world class. But because I have 4 lanes on my street there a 4 SpawnCheckers. In the prepare method of my world I declare them with numbers.
 SpawnChecker spawnchecker2 = new SpawnChecker();
        addObject(spawnchecker2, 477, 305);
Now, I cant figure out how to run this. This is my if condition:
if (lane == 2 && spawnchecker2.canSpawn()==true) { "spawn a car" }
Iam geting the Error: cannot find symbol - variable spawnchecker2. Probably it has to be SpawnChecker.canSpawn() but I dont know how to combine it with the specific part of code. Thanks you very much.
danpost danpost

2017/3/9

#
The location of the following line:
SpawnChecker spawnchecker2 = new SpawnChecker();
is important. As is, 'spawnchecker2' only has scope to the end of the block it is contained in. Later, when you try to call 'canSpawn' on it, the variable does not exist anymore. You need to expand the scope of the variable by moving it outside the block it is currently in.
Telko Telko

2017/3/9

#
This was the mistake, it compiles now and I can start it, thank you. But there is an error occuring after a short time the scenario runs. "java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:711) at greenfoot.Actor.isTouching(Actor.java:972) at SpawnChecker.canSpawn(SpawnChecker.java:21) at Welt.spawnVerkehr(Welt.java:95) at Welt.act(Welt.java:38) at greenfoot.core.Simulation.actWorld(Simulation.java:610) at greenfoot.core.Simulation.runOneLoop(Simulation.java:545) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)" As I understand, it is because there is no Object in the world that the boolean method can check on. So there I have to change something, but I dont know how the code would look like. Thanks for youre support.
danpost danpost

2017/3/9

#
I think the problem is quite similar to what was explained above. Post the code you have in your Welt class. (use code tags).
Telko Telko

2017/3/9

#
This is the code
public class Welt extends Scroll
{
    
    public int lane = 0;
    public int ctype = 0;
    
    
    /**
     * Constructor for objects of class Welt.
     * 
     */
    public Welt()
    {    
        super(479, 652, 1, false);
        // the background image
        GreenfootImage image = new GreenfootImage("VierSp.jpg");
        image.scale(getWidth(), getHeight());
        setBackground(image);
        // set scrolling fields
        setDirection(0);
        setScrollSpeed(0);

        prepare();
    }

    public void act()
    {
        scrollAll();
         if (Spieler.getPlayerSpeed() > 200) {
            spawnVerkehr();
        }
        setScrollSpeed(Spieler.getPlayerSpeed()) ;
        
    }
    
    
    public void spawnVerkehr() {
       lane = Greenfoot.getRandomNumber(700) ;
       ctype = Greenfoot.getRandomNumber(3) ;
       SpawnChecker spawnchecker1 = new SpawnChecker();
       SpawnChecker spawnchecker2 = new SpawnChecker();
       SpawnChecker spawnchecker3 = new SpawnChecker();
       SpawnChecker spawnchecker4 = new SpawnChecker();
       
       if (lane == 0 && spawnchecker4.canSpawn()==false ) {
            if (ctype == 0 ) {
               c1 c1 = new c1();
               addObject(c1, 477, 83);
            } else if (ctype == 1) {
                c2 c2 = new c2();
                addObject(c2, 477, 83) ;
            } else if (ctype == 2) {
                c3 c3 = new c3() ;
                addObject(c3, 477, 83);
            }
       }
       if (lane == 1 && spawnchecker3.canSpawn()==false) {
           if (ctype == 0 ) {
               c1 c1 = new c1();
               addObject(c1, 477, 188);
            } else if (ctype == 1) {
                c2 c2 = new c2();
                addObject(c2, 477, 188) ;
            } else if (ctype == 2) {
                c3 c3 = new c3() ;
                addObject(c3, 477, 188);
            }
       }
       if (lane == 2 && spawnchecker2.canSpawn()==false) {
           if (ctype == 0 ) {
               c1 c1 = new c1();
               addObject(c1, 477, 305);
            } else if (ctype == 1) {
                c2 c2 = new c2();
                addObject(c2, 477, 305) ;
            } else if (ctype == 2) {
                c3 c3 = new c3() ;
                addObject(c3, 477, 305);
            }
       }     
       if (lane == 3 && spawnchecker1.canSpawn()==false) {
           if (ctype == 0 ) {
               c1 c1 = new c1();
               addObject(c1, 477, 411);
            } else if (ctype == 1) {
                c2 c2 = new c2();
                addObject(c2, 477, 411) ;
            } else if (ctype == 2) {
                c3 c3 = new c3() ;
                addObject(c3, 477, 411);
            }
       }     
    } 
Btw: youre scroll support class is amazing and works perfect for me
danpost danpost

2017/3/9

#
Move lines 40 through 43 to line 6.
Telko wrote...
Btw: youre scroll support class is amazing and works perfect for me
Why have you not "liked" it, then??
danpost danpost

2017/3/9

#
Where are you adding your spawn checkers into the world at (where is the 'addObject(spawnchecker#...)' lines in your code)? where is the 'prepare' method? It will probably need to be fixed as well.
Telko Telko

2017/3/9

#
It still crashes. I checked that it happens when 1 of the 4 if condition are about to be executed.
danpost danpost

2017/3/9

#
Telko wrote...
It still crashes. I checked that it happens when 1 of the 4 if condition are about to be executed.
Please show your 'prepare' method in the Welt class.
Telko Telko

2017/3/9

#
private void prepare()
    {

        Spieler spieler = new Spieler();
        addObject(spieler, 160, 306);
        Counter counter = new Counter();
        addObject(counter,49,532);
        counter.setLocation(88,535);
        StartScreen startscreen = new StartScreen();
        addObject(startscreen, 234,300);
        
        SpawnChecker spawnchecker1 = new SpawnChecker();
        addObject(spawnchecker1,447,411);
        SpawnChecker spawnchecker2 = new SpawnChecker();
        addObject(spawnchecker2,447,305);
        SpawnChecker spawnchecker3 = new SpawnChecker();
        addObject(spawnchecker3, 447, 188);
        SpawnChecker spawnchecker4 = new SpawnChecker();
        addObject(spawnchecker4,447,83);
        
        
    }
danpost danpost

2017/3/9

#
Remove lines 12, 14, 16 and 18.
Telko Telko

2017/3/9

#
Thank you very much, it works now.
You need to login to post a reply.