I'm trying to code a game of Frogger, and I have everything just perfect, really... except for the constant flow of objects; cars, logs. I need to know how to make a constant stream of cars and logs.
code info: world dimensions are 572x572, each square measuring 44x44 (13 squares by 13 squares)
The Car and Log Actor classes do not have much code in them, doubt inserting their code would help much (just movement and stuff)
The layout of the world (graphic only, not individually classed) looks like:
Y-coord
Grass (end goal) 0-44
Water 44-88
Water 88-132
Water 132-176
Water 176-220
Grass 220-264
Road 264-308
Road 308-352
Road 352-396
Grass 396-440
Road 440-484
Road 484-528
Grass (start) 528-572
If you need any more details, let me know.
/**MyWorld*/
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
Frog frog = new Frog();
Car car1 = new Car(1);
Car car2 = new Car(2);
Car car3 = new Car(3);
Car car4 = new Car(3);
Car car5 = new Car(4);
Log log6 = new Log(2);
Log log7 = new Log(3);
Log log8 = new Log(5);
Log log9 = new Log(4);
ObstacleDespawn despawn = new ObstacleDespawn();
/** the lanes. lanes 1-5 spawn cars, lanes 6-9 spawn logs*/
public int laneDelay1 = 5;
public int laneDelay2 = 4;
public int laneDelay3 = 4;
public int laneDelay4 = 3;
public int laneDelay5 = 2;
public int laneDelay6 = 1;
public int laneDelay7 = 2;
public int laneDelay8 = 4;
public int laneDelay9 = 6;
public int lanes = 0;
private int speed;
public int i;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(572, 572, 1);
/** Each square measures 39p x 44p (width x height) */
/** setBackgroundImage("Background template.jpg"); */
prepare();
/**
* below was an attempt I made at making Logs and Cars spawn infinitely without crashing Greenfoot. I've learnt to never mess with infinite
* Java while loops.
*/
for (i=1;i<10000;i++){
spawnObjects();
if (Greenfoot.isKeyDown("up")){
i = 1;
}
}
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
addObject(frog,286,550);
addObject(despawn,0,286);
addObject(car1,572,506);
addObject(car2,572,462);
addObject(car3,572,374);
addObject(car4,572,330);
addObject(car5,572,286);
addObject(log6,572,198);
addObject(log7,572,154);
addObject(log8,572,110);
addObject(log9,572,66);
}
/** My attempt to constantly spawn Obstacles (superclass of Log and Car), but it triggers once and doesn't spawn anything upon triggering*/
public void spawnObjects()
{
if (laneDelay1 == 0){
addObject(car1,572,506);
laneDelay1 = laneDelay1 + 5;
}
if (laneDelay2 == 0){
addObject(car2,572,462);
laneDelay2 = laneDelay2 + 4;
}
if (laneDelay3 == 0){
addObject(car3,572,374);
laneDelay3 = laneDelay3 + 4;
}
if (laneDelay4 == 0){
addObject(car4,572,330);
laneDelay4 = laneDelay4 + 3;
}
if (laneDelay5 == 0){
addObject(car5,572,286);
laneDelay5 = laneDelay5 + 2;
}
if (laneDelay6 == 0){
addObject(log6,572,198);
laneDelay6 = laneDelay6 + 1;
}
if (laneDelay7 == 0){
addObject(log7,572,154);
laneDelay7 = laneDelay7 + 2;
}
if (laneDelay8 == 0){
addObject(log8,572,110);
laneDelay8 = laneDelay8 + 4;
}
if (laneDelay9 == 0){
addObject(log9,572,66);
laneDelay9 = laneDelay9 + 6;
}
delayTimer();
}
public void delayTimer(){
laneDelay1--;
laneDelay2--;
laneDelay3--;
laneDelay4--;
laneDelay5--;
laneDelay6--;
laneDelay7--;
laneDelay8--;
laneDelay9--;
}
}
