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

2013/10/17

How to let an object know it has a weight?

13111876 13111876

2013/10/17

#
Hello, I want to let an object know it has weight, at the max weight the object will remove. How do I let the object know it has a weight. Is is something with gravity??
SPower SPower

2013/10/17

#
Just keep a variable:
// declare the instance variable:
private int weight;

// where you want to remove the object
if (weight >= limit) {
    getWorld().removeObject(this);
}
// where you need to replace 'limit' (without the '') with the maximum weight.

// to add something to the weight:
weight += toAdd;
// replace 'toAdd' (without '')
danpost danpost

2013/10/17

#
You will need to add two instance fields to the class of the object. One to track the actors current weight and one for the maximum weight (which can be a 'static' field if all actors from that class have the same maximum weight). How you determine what weight it has is up to you, but you can still ask for help in coding it the way you choose. The specifics on coding it cannot be established until you decide how you wish to assign these values. The code SPower provided should work. But the 'limit' field needs to be declared also.
private int limit;
13111876 13111876

2013/10/17

#
This helped me a lot, thank you very much! @SPower @danpost
You need to login to post a reply.