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

2019/2/22

how to use another class method as a parameter in world

mrbrook mrbrook

2019/2/22

#
hey everyone, I just getting started with greenfoot couple weeks ago, and here's my code:
public class Obstacle extends Actor

public Obstacle(int speed,int damage,int spawntimer)
    {
        setspeed(speed);
        setdamage(damage);
    }
    public void setspawntimer(int spawntimer) {this.spawntimer = spawntimer;}
    public int getspawntimer(){return this.spawntimer;}

public class Stone extends Obstacle

    public Stone()
    {super(1,1,2);getImage().scale(40,40);}

//world is where the problem starts
public class MyWorld extends World

int stonetime = stone.getspawntimer();
int mushroomtime = mushroom.getspawntimer();

private boolean spawntimer(int s)
    {return time%(60*s) == 0;}

public void stage1()
    {
        if (spawntimer(stonetime))
        {  
            addObject(new Stone(), Greenfoot.getRandomNumber(600),20);
        }
    }
public void act()
    {
        time++;
        stage1();
    }

actually, all I'm trying to do is get that stage1 method to run correctly because it won't! due to an arithmetic exception. I really appreciate any help. thanks *sorry for the mess code
danpost danpost

2019/2/22

#
mrbrook wrote...
<< Code Omitted >> all I'm trying to do is get that stage1 method to run correctly because it won't! due to an arithmetic exception.
It makes sense that a Stone object would have a speed and a damage value. Each stone will have its own speed and amount of damage it can give. These are states that each object created from the class will have. It does not make any sense that each stone have a spawntimer. It is not really a state of a stone, nor does it deal with each specific stone at all. If your stones were to have chunks fly off it or split apart, then it would be reasonable to give the stones a state for that; but, that is not what you are doing. Your spawning of stones and mushrooms appear to be a behavior of your world which would then need timer states for spawning them. In other words, all your code for spawning them can be done solely in your MyWorld class. As a general rule, a class describes an object. It gives states (in fields) and behavior (in methods) to each object created from that class. This should be noted to determine where you should put some specific code.
mrbrook mrbrook

2019/2/22

#
alright, this really helps I completely understand just give me a try for that, anyway thanks for the advice! have a nice day
You need to login to post a reply.