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

2018/10/24

Add an object once score reaches certain point?

ananyagarg ananyagarg

2018/10/24

#
I am making a snake game and I want to add a rock into the world every 10 points. This is the code in my Rock class:
public class Rock extends Actor
{
    public int currentscore;
    public Rock(){
        GreenfootImage img = getImage();
        img.scale(img.getWidth()*1/14, img.getHeight()*1/14);
        setImage(img);
    }
    /**
     * Act - do whatever the Rock wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       MyWorld world = (MyWorld)getWorld();
       Actor snake =  getOneIntersectingObject(SnakeHead.class);
       if (snake !=null){
           world.die();
        }
        addRocks();
    }
    public void addRocks(){
        MyWorld world = (MyWorld)getWorld();
        Score score = score.getScore();
 
        if (score == 10){
            world.addObject(new Rock(), 20,10);
        }
        if (score == 20){
            world.addObject(new Rock(), 3,6);
        } 
    }
I know the code is wrong, because it is showing an error, but I dont know what to change. I'm unsure how to call the getScore() method from my Score class
danpost danpost

2018/10/24

#
First off, your rocks do nothing on their own. They, at least, so far, just exist. They perform no actions and therefore no act method should be in the Rock class. Your entire Rock class can be simply this:
public class Rock extends greenfoot.Actor {}
The stuff you are trying to accomplish in the act method of the class can be done via the act method in your World subclass (class MyWorld). That is where all general game rules/controls should be implemented. Sidenote: the error is because the Score class does not contain a getScore method (at least, to my knowledge). You have one in your MyWorld class that returns a Score object.
Jeph Jeph

2018/10/24

#
ohk so i have a sugguestion with adding a rock after every 10th point int x={20,3,... } inty={10,6,...} public void addRocks(){ MyWorld world = (MyWorld)getWorld(); Score score = score.getScore(); for(int i=0;i<x.length;i++) { if (score%10==0) { world.addObject(new Rock(), x,y); } break; }
danpost danpost

2018/10/24

#
@Jeph, I am quite sure there is a problem with this line:
Score score = score.getScore();
Also, not only will your loop add a rock at all location at one time, but the if condition will most probably be true for many act cycles in a row -- meaning rocks will be added in an abundance. Again, I urge that the adding of rocks dependent on the score be done in the MyWorld class act method. The "dying" of the world should be implemented there also. Without knowledge of the number of rocks initially in the world and if any are ever removed, it would be difficult to lead in the best way to control the adding of rocks into the world. A second limiting condition will be necessary, but it is unclear if an extra field will be.
You need to login to post a reply.