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

2018/7/24

Can't Add Scoring

jjboy jjboy

2018/7/24

#
This is the world code

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    counter counter = new counter();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(420, 780, 1); 
        
        
        prepare();
    }

    public counter getCounter()
    {
        return counter;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        block0 block0 = new block0();
        addObject(block0,218,765);
        block0.setLocation(211,750);
        block1 block1 = new block1();
        addObject(block1,211,704);
        block1.setLocation(211,692);
        counter counter = new counter();
        addObject(counter,334,751);
        counter.setLocation(321,751);
    }
}
This is block 1 code

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class block1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class block1 extends Actor
{
    public int score;
    /**
     * Act - do whatever the block1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        
        Actor block0 = getOneIntersectingObject (block0.class);
        if(block0!=null)
        {
          World myWorld = getWorld();
            Space space = (Space)myWorld;  
         counter counter = space.getCounter();
         counter.addScore();
        }
    }    

    public block1()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() + 30 , image.getHeight() + 45);
        setImage(image);
    }
    
    
    public void move()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            if (isTouching(block0.class))
            {
              score=score+1;
            }
            else
            {
                Greenfoot.stop();
            }
           
            
        }
        else

        {
            move(1);
        }
    }
}
Super_Hippo Super_Hippo

2018/7/24

#
Remove line 45 in your world. This line:
counter counter = new counter();
jjboy jjboy

2018/7/24

#
thank you so much, now it works!!
You need to login to post a reply.