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

2014/2/7

My lives code doesn't wants to work i don 't know what is wrong can anyone Help mee

Gerrit Gerrit

2014/2/7

#
hello , i have put a live method in to mygame ,but it doesn't work always good. the method says if one actor hits another actor or the wall it has to take one life each time . but when i run it it just works for one time , after that it doesn't take a life at all can anyone help me i stuck with this
danpost danpost

2014/2/7

#
Please, please, please, -- any you start a discussion, please show the code in question. We cannot help without a full rundown on the life (from creation to point of fault) of your live methods/objects/fields/etc.; plus any code anywhere in your project around which it may be affected.
Gerrit Gerrit

2014/2/7

#
yeah you are right i am sorry i won't do that anymore , i am a little stressed out because i can't figure this . this is my car class
public class Car extends Cars
{
    private boolean atStation;
    private int pauseTimer;
    
    private Counter score_;
    private int count= 3;
    private Lives myLife;
    
    public Car(Lives live,Counter points)
    {
        myLife= life;
        score_= points;
        
    }
       
    public void act()
    {
        
        if (pauseTimer == 0)
        {
           followPath();
           placeGround();
        } 
        isInStation();
	station2();
        finishGass();
	checkLevel();
	
        
        carAccident();
        houseAccident();
        
      
    }
   
   
    
        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))  
        {  
            
            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");
                
                Greenfoot.playSound("fueled.wav");
                
                
            }  
        }  
    }
    
    private void isInStation()  
    {
        Actor station = getOneIntersectingObject(Station.class) ;
        if (!atStation && pauseTimer == 0 && station != null)
        {
            atStation = true;
            pauseTimer = 150;
	    score_.add(10);
        } 
    }
private void station2()  
    {
        Actor station = getOneIntersectingObject(Station.class) ;
        if (!atStation && pauseTimer == 0 && station != null)
        {
            atStation = true;
            pauseTimer = 150;
	    score_.add(15);
        } 
    }
 
  
 
   
   
    public void carAccident()
    {
        Actor carr;
        carr = getOneObjectAtOffset(0, 0, Car.class);
        if (carr != null)
        {
            getWorld().removeObject(carr);
            Greenfoot.playSound("MenuSound.wav");
	    count--;
            myLife.setText("lives: " + count);
           
        }
    }            
    
    public void houseAccident()
    {
        Actor hous;
        hous = getOneIntersectingObject( House.class);
        if (hous != null||  checkEdges())
        
        {
            getWorld().removeObject(this);
            count--;
            myLife.setText("lives: " + count);
            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/7

#
public class Livess extends Actor
{
   public Livesns(String text)
   {
       GreenfootImage img= new GreenfootImage(text.length()*20,30);
       img.drawString(text, 2, 20);
       setImage(img);
    }
   
   public void setText(String text)
   {
       GreenfootImage img= getImage();
       img.clear();
       img.drawString(text, 2, 20);
    } 
Gerrit Gerrit

2014/2/7

#
sorry typed it wrong this is my lives class
public class Lives extends Actor
{
   public Lives(String text)
   {
       GreenfootImage img= new GreenfootImage(text.length()*20,30);
       img.drawString(text, 2, 20);
       setImage(img);
    }
   
   public void setText(String text)
   {
       GreenfootImage img= getImage();
       img.clear();
       img.drawString(text, 2, 20);
    } 
Gerrit Gerrit

2014/2/7

#
and in my constructor is also wrong typed is should say public Car(Lives live,Counter points) { myLife= live; score_= points;
danpost danpost

2014/2/7

#
So..really, your Lives class is basically a Text class (a class that creates objects to display some text). How are you re-adding your Car object back into the world after it is removed (say, by 'houseAccident')? (or any other way it gets removed)
Gerrit Gerrit

2014/2/7

#
And this is myworld class
public class CarWorld extends World
{
    private int speed_;
    private int timer = 200;
    Lives live= new Lives(" lives: 3");
    Counter score_= new Counter();
    

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

        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/7

#
really that's exactly what i thought but i really don't know how to put that in mycode
Gerrit Gerrit

2014/2/7

#
what sould i do? i tried the whole day but i only get errors i want to have a live and if lives is 0 it has to go to another object that is Game Over class that only has an image with text in it but i get stuck in this lives code
danpost danpost

2014/2/7

#
If 'count' in the Car class is supposed to hold the number of lives (which it appears that is indeed what it is by the code of changing the text of the Lives object), then every time you create a new Car it is given a 'count' field initialized at 3. Move the count field to the Lives class, add a method to 'add' or 'takeAway' and a 'get' method for its value. Then, make sure you pass the count value of the Lives object from one world to the next.
Gerrit Gerrit

2014/2/7

#
Can you show me how to do that , i don't know.
danpost danpost

2014/2/7

#
You basically want to make the Lives class a Counter class. So, just use the Counter class you already have: I do not know what your Counter class looks like but you would need to change line 20 in your CarWorld class to something like the following:
Counter live = new Counter("Lives", 3);
Then, use the methods included in that class to adjust the value. As an alternative (and this may help you in your learning process), you could look over the Counter class code and take what you might need from it and apply it to your Lives class to make it work like a modified counter.
Gerrit Gerrit

2014/2/7

#
it stilll doesn't work shall i show you all my classes at the moment because i really don't know how i should fix this
You need to login to post a reply.