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

2013/6/23

Problrms with my game...

derp2000 derp2000

2013/6/23

#
Hey, I am making a frogger game and the world is 800, 600 But i've found a code for spawning cars on the interner only I don't understand how I can adapt this to the size of my world..... This is the code for spawning cars
 private void spawnCars()
    {
        // Generate a random value to decide in which lane to spawn
        // a car
        int randVal = Greenfoot.getRandomNumber(3)+1;

        // Spawn a single car every 50 acts
        if (counter % spawnRate == 0)
        {
            if (randVal == 1)
                spawnY = 127;
            else if (randVal == 2)
                spawnY = 223;
            else
                spawnY = 320;
            addObject(new Car1(vehicleSpeed), 20, spawnY);
            autoCount++;
        }
        else if (counter % spawnRate == spawnRate / 2)
        {
            if (randVal == 1)
                spawnY = 78;
            else if (randVal == 2)
                spawnY = 173;
            else
                spawnY = 270;
            addObject(new Car2(vehicleSpeed), 578, spawnY);
            // keep track of how many cars on on the screen, so that
            // you can be sure they all leave the screen before the 
            // Frogger respawns after dying
            autoCount++;
        }
        counter++;
    }
derp2000 derp2000

2013/6/23

#
Can some one please help me with this?
Gevater_Tod4711 Gevater_Tod4711

2013/6/23

#
In the code you posted the variable spawnY is the height where the cars spawn (0 means the cars spawn at the top 600 means the cars spawn at the botom of your world). You can change the value of this variable like in the lines 11, 13, 15, ... in the code abouve. To make the cars spawn at the left hand side of your world you can change the x coordinate in line 16 to whatever you want (but 20 seems to be ok to me). To make the cars spawn at the right hand side of your world you can change the x coordinate in line 27. If your world's width is 800 you may use 780.
derp2000 derp2000

2013/6/23

#
I understand what you are saying but could you please give a example in a code? Because I don't get it where I have to put that in my code... ;p
Gevater_Tod4711 Gevater_Tod4711

2013/6/23

#
    private void spawnCars()  
       {  
           int randVal = Greenfoot.getRandomNumber(3)+1;  

           if (counter % spawnRate == 0)  
           {  
               if (randVal == 1)  
                   spawnY = 127;  //this value is the height where the cars spawn (0 -> top of your world; 600 -> bottom of your world);
               else if (randVal == 2)  
                   spawnY = 223;  //again the height;
               else  
                   spawnY = 320;  //again the height;
               addObject(new Car1(vehicleSpeed), 20, spawnY);  //in this line the 20 means that the cars spawn at the left hand side;
               autoCount++;  
           }  
           else if (counter % spawnRate == spawnRate / 2)  
           {  
               if (randVal == 1)  
                   spawnY = 78;  //again the height;
               else if (randVal == 2)  
                   spawnY = 173;  //again the height;
               else  
                   spawnY = 270;  //again the height;
               addObject(new Car2(vehicleSpeed), 578, spawnY);  //in this line the 578 is the x coordinate; change this to 780 to make the cars spawn at the right hand side of your world. screen before the
               autoCount++;  
           }  
           counter++;  
       }  
Hope this was understandable.
derp2000 derp2000

2013/6/23

#
thanks :D
You need to login to post a reply.