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

2017/4/8

adding objects of the same class to the world with different variables set

rick_kik rick_kik

2017/4/8

#
So I'm making a breakout game for a school project, and I have made a brick that changes its color according to the value of the lives variable. If it's 4 the color is green, if its 3 yellow, 2 orange and 1 red. When the variable reaches a value of 0 the brick will be destroyed and removed from the game. Now I'm trying to add some bricks to the world with different starting values for the variable lives, so that some already spawn with the orange or yellow color. The problem is that I don't know how to do that. Can someone help me or show me some code of how to do it? This is what I have tried so far:
addObject(new Brick(lives = 100),300,3);
It gives the error: cannot find symbol - variable lives
Fifilein Fifilein

2017/4/8

#
Can you please Show your Worldclasscode?
Gaming_100 Gaming_100

2017/4/8

#
You want to know how to randomize the value of the brick?
Super_Hippo Super_Hippo

2017/4/8

#
//in world
Brick brick = new Brick();
addObject(brick, 300, 3);
brick.setLife(100);
//in Brick class
public void setLife(int newLife)
{
    life = newLife;
}
rick_kik rick_kik

2017/4/8

#
@Fifilein
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     */
    public int blockCount = 5;
    public Ball theBall;
    public platootje plato;
    public MyWorld()
    {
        super(600, 400, 1);
        plato = new platootje();
        addObject(plato,300,350);
        theBall = new Ball();
        addObject(new Brick(),300,3);
        addObject(new Brick(),300,50);
        addObject(new Brick(),400,150);
    }
    public Ball getBall() {
        return theBall;
    }
    public platootje getPlatootje(){
        return plato;
    }
}
THis is my world class code
rick_kik rick_kik

2017/4/8

#
@Super_Hippo thanks for helping. I assume that every individual brick needs these 3 lines of code to be summoned, different x, y and life values but this only seems to work if I only add one brick. I need to add a total of about 150 bricks over 3 worlds, so how can I do that?
rick_kik rick_kik

2017/4/8

#
@Gaming_100 No, I want to give the bricks a predetermined value for the lives variable.
Gaming_100 Gaming_100

2017/4/8

#
Ok
Super_Hippo Super_Hippo

2017/4/8

#
rick_kik wrote...
I assume that every individual brick needs these 3 lines of code to be summoned, different x, y and life values but this only seems to work if I only add one brick. I need to add a total of about 150 bricks over 3 worlds, so how can I do that?
Yes, every block needs these 3 lines of code (first 'Brick' in the first line has to be removed for every brick except the first one) if there is no pattern about how they should be added.
rick_kik rick_kik

2017/4/8

#
@Super_Hippo Ok thanks for your help! It's working great!
You need to login to post a reply.