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

2012/12/17

Fixing player 1 wins problem?

bbwf bbwf

2012/12/17

#
Hey guys I have a game where the Alligator tries to catch all of the frogs before the Camel can reach him. Right now I have it set so if the Camel hits the Alligator the Camel disappears and it is replaced with an image saying "player 2 wins!" I want to make it if the Alligator hits all of the frogs than the Alligator will disappear and it will be replaced with an image that says "Player 1 wins!" I keep running into an error tho, here is my code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Aligator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Aligator extends Mover
{
    int shootdelay=0;
    int mindelay=5;
     
    public void act() 
    {
        //Sets the rocket as a smaller image.
       GreenfootImage img = getImage();
      setImage(img);
      img.scale(120,32);
        shootdelay++;
         if(getWorld().getObjects(Frog.class) == null || getWorld().getObjects(Frog.class).isEmpty()) 
        
        {   
           { getWorld().removeObject(this);
            setImage(new GreenfootImage("player1.jpg"));  
          setLocation(425, 275);  
        //  GreenfootImage img = getImage();  
           img.scale(850,550);  
            Greenfoot.stop();
        }
       }  
        if(Greenfoot.isKeyDown("up"))
        {
            move(5);
             int w=getWorld().getWidth();
             int h=getWorld().getHeight();
             setLocation((getX()+w)%w, ((getY()+h)%h));
        }
         
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
        
        if(Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        if(Greenfoot.isKeyDown("space"))
        {  
            shoot();
        }
      
    }
//Shoots the bullet
    public void shoot()
    {
        if(shootdelay >= mindelay)
        {
            int rot = getRotation()-10;
            int xOffset = (int)(40 *Math.cos(Math.toRadians(rot)));
            int yOffset = (int)(40 *Math.sin(Math.toRadians(rot)));
            Bullet b = new Bullet(getRotation());

            getWorld().addObject(b, getX()+xOffset, getY()+yOffset);
            shootdelay = 0;
 
        }       
    }
}
Gevater_Tod4711 Gevater_Tod4711

2012/12/17

#
the problem is that you remove the object from the world (line 24) and then you try to set it's location in the world. If you first set the location and then remove the object or write a return; after the removing the programm should work.
bbwf bbwf

2012/12/17

#
This works but now I'm running into a problem where once the object is removed it won't change the picture and set the location.
Gevater_Tod4711 Gevater_Tod4711

2012/12/17

#
why should the object change the picture when it's removed? You can't see it then. If you set the location before removing it it shluld be at the right position. Or if you just don't want to see the object set it's image to a new image with only transparentz
danpost danpost

2012/12/17

#
Your problem arises from using the image of the player to show the gameover screen. If you are not going to create a new actor class to display the gameover screen, then you are going to need a flag in the player class to signify which image is currently being used. It can be a basic boolean called something like 'usingGameOverImage'. Add the following to be the very first statement in the 'act' method:
if(usingGameOverImage) return;
You also need to set the value of 'usingGameOverImage' to 'true' when you change the image to the gameover image.
You need to login to post a reply.