2013-02-22 2 views
0

나는 가상의 애완 동물 게임을 만들고 있는데, 이제 나는 배고픔을 포함 시키려고 노력하고 있지만, 먹는 기능을 작동시키는 법을 모르겠습니다. 나는 음식 항목의 영양 가치의 추가를 통해 그것을 감소 ​​시키려고 노력하고있다. 이 값은 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); 
    } 
} 

.

+0

실행 가능한 샘플을 게시 할 수 있습니까? –

+0

물론 게시물이 편집됩니다. – glocka

+0

과 save.txt는 아마도? :) –

답변

0

불행히도 저는 훌륭한 답변을 드릴 수 없습니다. 프로젝트의 구조와 코드가 상당히 혼란 스럽습니다.

그동안 생물의 eat() 함수에 구문 오류가 있습니다. 이 시도 :

void eat() { 
    println("EAT"); 
    //((Food)items.get(i)).nutrition(); 
    for(int i = 0 ; i < items.size(); i++){ 
     Food yummy = (Food)items.get(0); 
     yummy.nutrition(hunger); 
    } 
    } 

위의 작품 항목이 맨 위에 선언 (즉 글로벌) 전체 스케치의 범위 밖으로 통해 때문에 볼 수 있기 때문이다. 이것이 의도적인지 또는 당신이 생물과 음식 사이에서 어떤 관계를 맺을 지 확신 할 수 없습니다.

은 위의 때문에 같은 lazier (미만 일반/명백한) 방식으로 작성 될 수

void eat() { 
    println("EAT"); 
    //((Food)items.get(i)).nutrition(); 
    for(Object yummy : items) ((Food)yummy).nutrition(hunger); 
    } 

은 내가 얼마나 배고픈 기반으로 생물이 공급됩니다 가정합니다. 어떤 점에서 생물 내가 그래서 당신은 또한 영양을 기반으로 생물의 hungry 속성을 업데이트 할, 가정 는 음식에서 얻는 등

을 또한, 단지 테스트 가득합니다, 나는 매우 영양가 추가 한 음식 : P

items.add(new Food(mouseX, mouseY, 10000));