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

2014/1/30

My changeLevel () doesn't work i can't figure out what s wrong can anyone help me

Gerrit Gerrit

2014/1/30

#
i want get to a other level if my score is at a certain value. but my method does n t work
joeschmoe joeschmoe

2014/1/30

#
Well we cant really help you if you dont post the code
Gerrit Gerrit

2014/1/30

#
This is the method i got in a class
public void checkLevel()
    { 
        if (score_.getValue()== 30)
        {
            MyWorld world = new SecondWorld();
            Counter w1counter = (Counter)getWorld().getObjects(Counter.class).get(0);  
            Counter w2counter = (Counter)world.getObjects(Counter.class).get(0); 
            w2counter.setValue(w1counter.getValue());
            Greenfoot.setWorld(world);
        } 
    }  
danpost danpost

2014/1/30

#
Is the SecondWorld class a subclass of MyWorld or should line 5 be the following:
SecondWorld world = new SecondWorld();
Gerrit Gerrit

2014/1/30

#
oh sorry typed it wrong but the code i have compiles bit right when it rech the points in score it chang e for second and than the whole world get stuck and i get error
danpost danpost

2014/1/30

#
What error are you getting? Copy/paste the message (every line) here.
Gerrit Gerrit

2014/1/30

#
Sorry i thought getting an error but it;s like the world does change but than it get like stuck the actors don t move
danpost danpost

2014/1/30

#
Post the code of the world that is being set active when the problem occurs.
Gerrit Gerrit

2014/1/30

#
public class SecondWorld extends MyWorld
{
    
   
    public SecondWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super();
        setSpeed(getSpeed()+5);
    }
   
}
Gerrit Gerrit

2014/1/30

#
actually i got to problems in main problems in my game -this one above - and my car has to lose life when hits another car .
danpost danpost

2014/1/30

#
Ok. I will need to see the code for the MyWorld class (since it is what the SecondWorld class extends).
Gerrit Gerrit

2014/1/30

#
public class MyWorld extends World
{
    private int speed_;
   
    public void act(){
        
       
    }

    public MyWorld()
    {  
        super(1200,700 ,1);
        prepare();
        speed_ = 45;
        Greenfoot.setSpeed(speed_);
       
        
    }
    
    private void prepare()
    {
        Lives live= new Lives();
        addObject(live,500,50);
        Counter points = new Counter();
        addObject(points,50, 50);
        Car carr = new Car(live,points);
        addObject(carr, 62, 249);
        
        Gates gate = new Gates() ;
        addObject(gate,1027,388);
    }
    
    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_ ;
    }
}
danpost danpost

2014/1/30

#
So, basically a SecondWorld world is just a MyWorld world with the speed increased by 5. All the actors are 'renewed' when the SecondWorld world is created and the score is copied over to the SecondWorld world. When the score is brought over, the condition to instantiate a new SecondWorld world becomes true and is executed again and again creating an infinite loop. You need to qualify further when a new SecondWorld world is instantiated. Maybe 'if (score_.getValue() == 30 && ((MyWorld)getWorld()).getSpeed() == 45).
danpost danpost

2014/1/30

#
It appears you really do not need a 'SecondWorld' class. You could change your 'checkLevel' method to this:
public void checkLevel()
{ 
    MyWorld mw = (MyWorld)getWorld(); // current world
    if ((score_.getValue()== 30 && mw.getSpeed() == 45) ||
        (score_.getValue() == 60 && mw.getSpeed() == 50) ||
        (score_.getValue() == 90 && mw.getSpeed() == 55)) // etc.
    {
        MyWorld mw2 = new MyWorld(); // next world
        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); // increases the speed each level
        Greenfoot.setWorld(mw2);
    } 
}
danpost danpost

2014/1/30

#
The 'if' condition, as I have it, can be simplified to this:
if (score_.getValue() == (mw.getSpeed()-40)*6)
You need to login to post a reply.