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

2015/1/9

Adding text once the ball reaches the end

pactz pactz

2015/1/9

#
Basically i have made the class for the text, all i want it to do is to appear when the ball reaches the win(void win section) target could someone add the code so that the text appears when reaches the void win section of the goldenball here's the ball code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GoldenBall here. * * @author (your name) * @version (a version number or a date) */ public class GoldenBall extends Actor { /** * Act - do whatever the GoldenBall wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { key(); win(); checkStarfish(); } public boolean canMove(int x, int y) { Actor sand; sand=getOneObjectAtOffset(x,y,sandroad.class); //the section below checks if there is a block you can move to // if there is it sets sand to a vlaue otherwise it says null // The errors are in this section boolean flag=false; if (sand !=null) { flag=true; } return flag; } /** * Check if ball hits starfish */ private void checkStarfish() { Actor Starfish= getOneIntersectingObject(Starfish.class); if (Starfish !=null ) { getWorld().removeObject(Starfish); } } public void key() { //Note 1: Down the page increase the y value and going to the right increases the x value //Note 2: Each block is 60 pixels wide and high int leftChange=-60; int rightChange=60; int upChange=-60; int downChange=60; if (Greenfoot.isKeyDown("left")) { if (canMove(leftChange, 0)==true){ setLocation(getX()+leftChange, getY()) ;} } if (Greenfoot.isKeyDown("right")) { if (canMove(rightChange, 0)==true){ setLocation(getX()+rightChange, getY()) ;} } if (Greenfoot.isKeyDown("up")) { if (canMove(0, upChange)==true){ setLocation(getX(), getY()+upChange) ;} } if (Greenfoot.isKeyDown("down")) { if (canMove(0, downChange)==true){ setLocation(getX(), getY()+downChange) ;} } } public void win() { Actor win; win=getOneObjectAtOffset(0,0,Goal.class); if (win !=null) { Greenfoot.stop(); Greenfoot.playSound("W.wav"); } } } heres the code for the text: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.*; /** * Write a description of class text here. * * @author (your name) * @version (a version number or a date) */ public class text extends Actor { /** * Act - do whatever the text wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public text() { GreenfootImage img = new GreenfootImage (300,300); img.drawString("congratulations ", 2,20); setImage(img); } } *thanks*
danpost danpost

2015/1/9

#
Just add a line in the win block to create and add a text object into the world:
getWorld().addObject(new text(), /** wherever */);
pactz pactz

2015/1/9

#
Thank you very much
You need to login to post a reply.