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

2011/6/28

Checking for values of things in range

1
2
3
4
kiarocks kiarocks

2011/6/28

#
I'm looking for a way to check the value of another actor that is in range. How would i do that??
Lildarkone Lildarkone

2011/6/28

#
The code is Actor actor = getOneObjectAtOffset( x, y, clss);
kiarocks kiarocks

2011/6/29

#
how do i use it?
danpost danpost

2011/6/29

#
If you are still working on 'Infect'
Person person = getOneObjectAtOffset(x, y, Person.class);
int mySex = person.getSex();
and the write the new method:
public int getSex()
{
    return sex;
}
That is, of course, unless this works (and I believe it will):
Person person = getOneObjectAtOffset(x, y, Person.class);
int mySex = person.sex;
Now, if you want to know how many of each are in range:
List<Person>persons = getObjectsInRange(2, Person.class);
int maleCt = 0;
int femaleCt = 0;
for (Person person: persons)
{
    if (person.infectionStage < 5)
    {
        if (person.sex == 0) { femaleCt++; } else { maleCt++; }
    }
}
kiarocks kiarocks

2011/6/29

#
says x is not public and what time is it for you now
DonaldDuck DonaldDuck

2011/6/29

#
The variables x and y that danpost posted should be replaced with the specific x and y coordinates you would like (relative to the current actor) Check out the sample "Balloons" scenario included with Greenfoot, specifically the Explosion class. The explosion does pretty much what you want to do - it checks for objects in a certain range (but removes them if they're there) It can be lightly modified to fit your needs.
kiarocks kiarocks

2011/6/29

#
ok
kiarocks kiarocks

2011/6/29

#
cannot find variable sex
DonaldDuck DonaldDuck

2011/6/29

#
Replace the variable "sex" with whatever variable decides the gender of your object.
kiarocks kiarocks

2011/6/29

#
thats the problem, sex IS that variable
danpost danpost

2011/6/29

#
The name of the variable is not the problem; again, I am going to guess that you are still in 'Person' class and that possibly the variable is not declared; that is:
private int sex;
is probably missing from your code. Also, make sure that all constructors for 'Person' assign a value to 'sex'.
danpost danpost

2011/6/29

#
kiarocks, I would like to show you what I have so far; but I do not think it should be on the site any longer than needed for you to get it. We will have to set up a time, where I can upload it, you can download it, and I can delete it. It should be around 3pm for you about now; let me know if 8pm (your time) is good for you :-) That would be 11pm for me. I still want to work on mine a bit -- set it up so that I can adjust some of the variables during run-time. Also, I do not like the way I have un-infecteds dying -- I want to work on that also.
kiarocks kiarocks

2011/6/29

#
yea that is fine and the private int sex is NOT missing
Lildarkone Lildarkone

2011/6/30

#
Did you import the java util at the top of person??? It is required for lists. import java.util.*; Check out my Ant Life Scenarios source code. It deals with lists and infection, as well as testing for male and female. http://greenfootgallery.org/scenarios/3081
kiarocks kiarocks

2011/6/30

#
yes, the problem is in this code
{
List person = getIntersectingObjects(Person.class);

get(person.sex);  
}
There are more replies on the next page.
1
2
3
4