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

2015/1/12

Need Counter for how many Balloons a user collects with Ball

Dan123 Dan123

2015/1/12

#
Here is code for ball : 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(); checkballoon(); } 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 balloon */ private void checkballoon() { Actor balloon= getOneIntersectingObject(balloon.class); if (balloon !=null ) { getWorld().removeObject(balloon); } } 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(); } } } And here is code for balloon: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class balloon here. * * @author (your name) * @version (a version number or a date) */ public class balloon extends Actor { /** * Act - do whatever the balloon wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(15); setRotation(Greenfoot.getRandomNumber(540)); } } If you could please help me with this i'll be very thankful.
xFabi xFabi

2015/1/12

#
int count = 0;

 if (balloon !=null ) 
        
        {   
            getWorld().removeObject(balloon);
            count++;
            example EXAMPLE = new example();
        }
act()
{
......
          getWorld().addObject(example),x,y)
}
//Create a new class, example
act()
{
setImage((new GreenfootImage("Counter  : " + count,50,Color.GREEN,new Color(0,0,0,0))));
}
Dan123 Dan123

2015/1/13

#
Need help. Cant find where to put the code. Error coming up ( if (balloon !=null).
xFabi xFabi

2015/1/13

#
Actor balloon = getOneIntersectingObject(balloon.class)
put this in front of the if please always give detailed information about errors, and your codes, or we cant help properly
xFabi xFabi

2015/1/13

#
xFabi wrote...
public void whatever()
{
int count = 0; // <---- DECLARE THIS OUTSIDE OF THE METHOD
Actor balloon = getOneIntersectingObject(balloon.class)
 if (balloon !=null ) 
        
        {   
            getWorld().removeObject(balloon);
            count++;
        }
}
act()
{
......
          whatever();
          example EXAMPLE = new example();
          getWorld().addObject(example),x,y)
}
//Create a new class, example
act()
{
setImage((new GreenfootImage("Counter  : " + count,50,Color.GREEN,new Color(0,0,0,0))));
}
first part is a method, name it however you want, At the // part, ofc you have to create a subclass
You need to login to post a reply.