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

2011/12/12

Game crash need help!

FatCat FatCat

2011/12/12

#
I'm working on my first sidescrolling game and I need some help with crashing. Everything's working perfect so far, moving, collecting objects, adding/removing score. Objects fly from left to right and they dissapear when they hit the opposite end of the world. Now I'm trying to add a minus score if the object hits the opposite side but the game crashes. How should I fix this?
public void act() 
    {   
        setLocation(getX() -2, getY());
        if (getX() == 0) { 
           // ((Game) getWorld()).addScore(-20); 
            getWorld().removeObject(this);             
        }       
darkmist255 darkmist255

2011/12/13

#
I don't think you need the (Game) cast, this should work:
getWorld().addScore(-20);
Assuming that in your world you have the method addScore(int/double/whatever), that should work.
FatCat FatCat

2011/12/13

#
I don't know why but it worked right after I posted to this forum and I didn't change the code, lol... So thanks anyway. I have another question now. I want to make 3 "lifes" for player (3 slips of object to the opposite side of the screen).
public class Krof  extends Actor
{
    
    /**
     * Act - do whatever the Krof wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setLocation(getX() - 5, getY());
        if (getX() == 0) {
            
            ((Game) getWorld()).konecIgre(3);        
            getWorld().removeObject(this);           
            
        }
    }    
}
public void konecIgre(int ziv) 
    {           
        if(ziv == 0)
        {
        addObject(new ScoreBoard(score.getValue()), getWidth()/2, getHeight()/2);       
        Greenfoot.stop();
        }
      ziv--;   
    }
It works (stops the game after first slip) if i remove the "if" statement. Am I looking at this from a wrong perspective?
darkmist255 darkmist255

2011/12/13

#
I think I see the problem. In your Krof actor you are always saying that ziv is 3. It would probably be easier to do something like this:
public class Krof  extends Actor  
    {  
int ziv
        public void act()   
        {  
           setLocation(getX() - 5, getY());  
            if (getX() == 0)
            {        
                damage();          
                setLocation(somewhere);             
                  
            }  
           konecIgre()
        }      

//this method below should be in the actor class.
public void konecIgre() 
{ 
   if(ziv <= 0) 
   { 
   getWorld().addObject(new ScoreBoard(score.getValue()), getWidth()/2, getHeight()/2);
   Greenfoot.stop();
   } 
}

//so should this
   public void damage()
   {
   ziv = ziv - 1;
   }
}
FatCat FatCat

2011/12/13

#
I've tried that but some other things are bound to "score" so it made a lot of errors. Is there any easier way? Is it possible to call "ziv" from World -> Game to Actor -> Krof as variable and not as value (3)?
darkmist255 darkmist255

2011/12/13

#
Maybe call getWorld().konecIgre(someVariable) and have someVariable be the number?
You need to login to post a reply.