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

2014/2/4

My spawnCars method doesn't work correctly i can 't figure out whyyy can anyone help me

Gerrit Gerrit

2014/2/4

#
hello , cars should enter the world depending on the timer . but the problem is that some times there are coming cars in the world and sometimes there coming only 2 cars and then it stops . i don t know whats wrong with my timer or my addCars method . i thought maybe because of my other methods , like my addscore or sub lives ,because for example each time the car bumps in to another car or sides of another object it must substract one live . that works for the first two cars after that for some reason sometimes the world doesnt create new cars more .. i don't know what i am doing wrong i tried every scenario
danpost danpost

2014/2/4

#
Cannot fix code when code is not given. You mentioned a timer -- what code! you mentioned a 'spawnCars' method (or is that an 'addCars' method)-- what code! you mentioned 'addscore' and 'sublives' -- what code! We do not know what you are doing wrong unless we see what you are doing.
Gerrit Gerrit

2014/2/4

#
yeah of course ,sorry. here is my car class
public class Car extends Cars
{
    private boolean atStation;
    private int pauseTimer;
    private int timer = 300;
    private Counter score_;
    private Text test;
    private Live lives;
    
    public VrachtSchip(Live live,Counter points)
    {
        lives= life;
        score_= points;
        
    }
       
    public void act()
    {
        moveElement();
        carClicked();
        checkTimer();
        if (pauseTimer == 0)
        {
            move(2);
        } 
        isInStation();
        finishGass();
        checkLevel();
        carAccident();
        houseAccident();
        checkLives();
      
    }
   
    public void checkLives()
    {
        if (lives.getValue()==0)
        {
           
            Greenfoot.setWorld(new MenuWorld());
            
        }
    }
    
        public void checkLevel()  
    {   
        CarWorld mw = (CarWorld)getWorld(); 
        if ((score_.getValue() == 20 && mw.getSpeed() == 40) ||  
            (score_.getValue() == 40 && mw.getSpeed() == 45)||
            (score_.getValue() == 60 && mw.getSpeed() == 50))  
        {  
            Text testt= new Text();
            getWorld().addObject(testt,500,500);
            testt.nextLevelTekst();
            CarWorld mw2 = new CarWorld();  
            Counter w1c=(Counter)mw.getObjects(Counter.class).get(0);    
            Counter w2c=(Counter)mw2.getObjects(Counter.class).get(0);   
            w2c.setValue(w1c.getValue());  
            mw2.setSpeed(mw.getSpeed()+5);  
            Greenfoot.setWorld(mw2);
            
        }   
    }  
    
    private void finishGass()  
    {
        if (pauseTimer > 0)  
        {
            pauseTimer--;  
            if (pauseTimer == 0)  
            {  
                setImage("fueledcar.png");
                score_.add(10);
                Greenfoot.playSound("fueled.wav");
                
                
            }  
        }  
    }
    
    private void isInStation()  
    {
        Actor station = getOneIntersectingObject(Station.class) ;
        if (!atStation && pauseTimer == 0 && station != null)
        {
            atStation = true;
            pauseTimer = 150;
        } 
    }
 
    private void checkTimer()
    {
        if (timer > 0)
        {
            timer--;
            if( timer == 0)
            {
                createNewCar();
                timer = 200;
            } 
           
        } 
    }
 
    public void createNewCar()
    {
        World myWorld;
        myWorld = getWorld();
        int worldHeight = myWorld.getHeight();
        int y = Greenfoot.getRandomNumber(worldHeight);
        myWorld.addObject(new Car(lives,score_),100, y);
    }
   
    public void carAccident()
    {
        Actor carr;
        carr = getOneObjectAtOffset(0, 0, Car.class);
        if (carr != null)
        {
            getWorld().removeObject(carr);
            lives.add(-1);
            Greenfoot.playSound("MenuSound.wav");
           
        }
    }            
    
    public void houseAccident()
    {
        Actor hous;
        hous = getOneIntersectingObject( House.class);
        if (hous != null||  checkEdges())
        
        {
            getWorld().removeObject(this);
            lives.add(-1);
            Greenfoot.playSound("MenuSound.wav");
           
        }
    } 
    
    public boolean checkEdges() 
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
        {
            return true;
        }
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
Gerrit Gerrit

2014/2/4

#
the constructor in stead should be Car
danpost danpost

2014/2/4

#
Because you put the timer/createNewCar code in the Car class, you will first need a car in the world to spawn new cars. If you had two cars and they are both removed from the world, then there are no cars in the world to create new cars from. Also, the more cars you have, the faster they will spawn. Move the code to spawn new cars into the world class to create a constant rate of spawning cars.
Gerrit Gerrit

2014/2/4

#
okej i have put it in the world but now if i run after cuple second i get an error Nulpointerexpection something with checkLevel
danpost danpost

2014/2/4

#
What does the 'checkLevel' method have to do with spawning new cars? Only the code used to regularly spawn cars was to be moved to the world class. That would be the 'timer' field, the call to 'checkTimer' in the act method, and the 'checkTimer' and 'createNewCar' methods. The 'createNewCar' method will need slight modification to work in the World subclass (removal of 'getWorld' calls, mainly).
Gerrit Gerrit

2014/2/4

#
yeah this is my world class now
public class CarWorld extends World
{
    private int speed_;
    private int timer = 200;
    Live lives;
    Counter score_;
    

	public void act()
    {
        checkTimer();
        
       
    }
    
    public CarWorld()
    {  
        super(1200,700 ,1);
        prepare();
        speed_ = 40;
        Greenfoot.setSpeed(speed_);
        Greenfoot.playSound("background.wav");
        
    }
    
    public void createNewCar()
    {
        
        int worldHeight = getHeight();
        int y = Greenfoot.getRandomNumber(worldHeight);
        addObject(new Car(lives,score_),100, y);
    }
    
    private void checkTimer()
    {
        if (timer > 0)
        {
            timer--;
            if( timer == 0)
            {
                createNewCar()
                timer = 200;
            } 
           
        } 
    }
 
    private void prepare()
    {
        Live livess= new Live();
        addObject(livess,500,50);
        Counter score = new Counter();
        addObject(score,50, 50);
        Car one = new Car(lives,score_);
        addObject(one, 62, 249);
      

        Station docks = new Station();
        addObject(docks, 1048, 328);
        Station docks2 = new Station();
        addObject(docks2, 1100, 381);
       
        House boom = new House();
        addObject(boom, 606, 429);
        House boom2 = new House();
        addObject(boom2, 595, 418);
        
    }
    
    public void setSpeed (int speed)
    {
        if (speed > 100)
        {
            speed_ = 100;
        }
        else if (speed < 1)
        {
            speed_ = 1;
        }
        else
        {
            speed_ = speed;
            Greenfoot.setSpeed(speed_);
        }
    }

    public int getSpeed()
    {
        return speed_ ;
    }
Gerrit Gerrit

2014/2/4

#
i get directly an exception now
danpost danpost

2014/2/4

#
You are missing references to your 'Score' and 'Live' objects ('score_' and 'lives'). You need to add instance fields here and they should be set when you create them in the prepare method.
Gerrit Gerrit

2014/2/4

#
Nice it works I was trying to code something like my cars companied by a motorcycle that brings him to the station. so when the car entered the world it should follow the motorcycle , the motorcycle must bring the car to the station and when it get to the station. the motorcycle must get the other cars as quick as possible . i have no idea how i should code this
You need to login to post a reply.