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

2012/5/28

How to end the game once a co-ordinate has been reached

1
2
3
DarrenM2012 DarrenM2012

2012/5/28

#
Ah thanks, thanks to everyone who's helped yet again much appreciated.
DarrenM2012 DarrenM2012

2012/5/28

#
Back again lol, I was just testing the game, it all works expect it actually doesn't display the "you win". I may have missed something as I couldn't keep up with the posts.
danpost danpost

2012/5/28

#
What code do you have for the 'You Win' actor?
DarrenM2012 DarrenM2012

2012/5/28

#
The code is currently in my character class using the following, which gets the you win from the world subclass.
if(getX() == 17 && getY()==15)      
       {      
          ((background)getWorld()).board();     
          Greenfoot.stop();      
       }      
         else if(getX() == 18 && getY()==15)      
         {      
         ((background)getWorld()).board();     
         Greenfoot.stop();      
         }   
and then the world subclass code
public void board()      
        {      
                 Font font = getBackground().getFont();      
                 font = font.deriveFont(FONT_SIZE);      
                 getBackground().setFont(font);      
                 getBackground().setColor(Color.red);      
                 getBackground().drawString("You win!",getWidth()/2,getHeight()/2);      
              
        }    
MatheMagician MatheMagician

2012/5/28

#
My mistake (sorry about drawString). I am used to the cell size being pixel size. Try getBackground().drawString("You win!",100,100); instead of getBackground().drawString("You win!",getWidth()/2,getHeight()/2);.
DarrenM2012 DarrenM2012

2012/5/28

#
Thanks working now, just wondering is there a way to move where it displays?
danpost danpost

2012/5/28

#
You probably want the text centered, so the easiest way is to create a GreenfootImage and draw the Image onto the background. Change your board() method in the world class to:
public void board()      
{
    GreenfootImage youwin = new GreenfootImage("You win!", FONT_SIZE, Color.red. new Color(0, 0, 0, 0));
    getBackground().drawImage(youwin, (getWidth() - youwin.getWidth()) / 2, (getHeight() - youwin.getHeight()) / 2);
}
derp2000 derp2000

2013/6/16

#
if(getX() == 17 && getY()==15) { ((background)getWorld()).board(); Greenfoot.stop(); } else if(getX() == 18 && getY()==15) { ((background)getWorld()).board(); Greenfoot.stop(); } If i put this in my character class, it says illegal start of type. What do i have to do to make this right??
danpost danpost

2013/6/16

#
@derp2000, please show the complete method that this code comes from.
derp2000 derp2000

2013/6/16

#
i copied everything from here but this is wat is in mine charachter code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; //Imports Color Class from the java library. /** * Write a description of class Poppetje here. * * @author Harmen * @version (a version number or a date) */ public class Poppetje extends Actor { /** * Act - do whatever the Poppetje wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+5); } if (Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-5); } if (Greenfoot.isKeyDown("left")) { setLocation(getX()-5, getY()); } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+5, getY()); } } public void wonGame() { if ( getY() < 15 ) { getWorld().removeObjects(getWorld().getObjects(Car1.class)); getWorld().removeObjects(getWorld().getObjects(Car2.class)); getWorld().removeObjects(getWorld().getObjects(Leraar.class)); getWorld().removeObjects(getWorld().getObjects(Leraar2.class)); GreenfootImage bg = getWorld().getBackground(); bg.setColor(Color.blue); bg.drawString("u heeft gewonnen!", 300, 400); Greenfoot.stop(); } } public void eat() { Actor Car1; Car1 = getOneObjectAtOffset(0, 0,Car1.class); if (Car1 != null) { World world; world = getWorld(); world.removeObject(Car1); } } if(getX() == 17 && getY()==15) { ((background)getWorld()).board(); Greenfoot.stop(); } else if(getX() == 18 && getY()==15) { ((background)getWorld()).board(); Greenfoot.stop(); } }
danpost danpost

2013/6/16

#
The code starting at 'if(getX() == 17 && getY()==15)' is not within a method (your act method ends just above that line with the closing curly bracket).
You need to login to post a reply.
1
2
3