나는 가상의 애완 동물 게임을 만들고 있는데, 이제 나는 배고픔을 포함 시키려고 노력하고 있지만, 먹는 기능을 작동시키는 법을 모르겠습니다. 나는 음식 항목의 영양 가치의 추가를 통해 그것을 감소 시키려고 노력하고있다. 이 값은 arraylist에 저장된 객체에 있습니다.Arraylist of Objects에서 값 가져 오기 - 처리
int nutrition(int eaten) {
nutrition = nutrition - eaten;
return nutrition;
이
내가 이것을 사용하려고했습니다
http://www.java-forums.org/new-java/2370-how-return-object-arraylist.html
ArrayList items;
void setup() {
items = new ArrayList();
}
void draw() {
for (int i = items.size() - 1; i >=0; i --) {
Food food = (Food) items.get(i);
food.display();
if (food.finished()) {
items.remove(i);
}
}
}
void mouseClicked() {
items.add(new Food(mouseX, mouseY, 1));
}
((Food)items.get(i)).nutrition();
내부 (이후에 전달됩니다 먹 int)를 참조 할 수있는 방법이 있나요하지만 처리 나는 찾을 수 없다. 나는 이것이 수업에 존재하지 않기 때문에, 이것이 기본 스케치에서만 존재한다고 믿습니다. 이것이 그렇다면, 나는 방법으로 i를 두는 방법을 발견 할 것이다. 어쩌면 수익을 사용했을 수도 있습니다.
나는 누군가가 이것을하는 더 좋은 방법을 알고 있다면 지식을 높이 평가할 것입니다.
save.txt이 1,1,50,1,1와 txt 파일입니다 CODE
Creature creature;
ArrayList items;
Hand hand;
String data[];
int gameInfo[];
int tempData[];
boolean haveFood;
void setup() {
size(100, 100);
smooth();
noCursor();
String data[] = loadStrings("save.txt");
String[] tempData = split(data[0], ',');
gameInfo = int(tempData);
for (int i = 0; i < data.length; i++) {
creature = new Creature(gameInfo[0], gameInfo[1], gameInfo[2], gameInfo[3]);
haveFood = false;
hand = new Hand();
items = new ArrayList();
}
}
void draw() {
background(255);
for (int i = items.size() - 1; i >=0; i --) {
Food food = (Food) items.get(i);
food.display();
if (food.finished()) {
items.remove(i);
}
}
creature.whatWant();
creature.whatDo(haveFood);
hand.draw();
}
void mouseClicked() {
items.add(new Food(mouseX, mouseY, 1));
haveFood = true;
}
class Creature {
int hunger;
int age;
int gender;
int asleep;
boolean idle;
char want;
char request;
Creature(int _gender, int _age, int _hunger, int _asleep) {
gender = _gender;
age = _age;
hunger = _hunger;
asleep = _asleep;
idle = true;
}
void whatWant() {
if (hunger == 50) {
want = 'H';
}
}
void whatDo(boolean food) {
if (idle == true) {
switch(want) {
case 'H':
if (food == true) {
creature.eat();
}
else
request = 'F';
ask();
}
}
else
{
println("IDLE");
}
}
void ask() {
if (request == 'F') {
println("HUNGRY");
}
}
void eat() {
println("EAT");
((Food)items.get(i)).nutrition();
}
}
class Food {
float posX;
float posY;
int nutrition;
Food(float _posX, float _posY, int rating) {
posX = _posX;
posY = _posY;
nutrition = rating;
}
void display() {
rect(posX, posY, 10, 10);
}
boolean finished() {
if (nutrition < 0) {
return true;
}
else {
return false;
}
}
int nutrition(int eaten) {
nutrition = nutrition - eaten;
return nutrition;
}
}
class Hand {
int posX;
int posY;
Hand() {
posX = mouseX;
posY = mouseY;
}
void draw() {
rectMode(CENTER);
point(mouseX, mouseY);
}
}
.
실행 가능한 샘플을 게시 할 수 있습니까? –
물론 게시물이 편집됩니다. – glocka
과 save.txt는 아마도? :) –