我在做一个虚拟宠物游戏,现在我想包括饥饿,但是我不知道如何让吃的功能的工作。我试图使它通过增加食物项目的营养价值降低。这个值是在被存储在数组列表中的对象。
I'm making a virtual pet game, and I am now trying to include hunger, however I'm not sure how to make the eat function work. I am trying to make it decrease through the addition of the nutrition value of an item of food. This value is in an object that is stored in an arraylist.
有没有引用一个办法
int nutrition(int eaten) { nutrition = nutrition - eaten; return nutrition;(INT食用会通过后面)
(int eaten will be passed in later)
在
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();HTTP://www.java- forums/new-java/2370-how-return-object-arraylist.html 我试图用这个,但处理无法找到我。我相信这是因为我没有在课堂上存在的,只有在主草图。如果是这样的话,我会找到办法的地方,我到方法。也许使用的回报。
www.java-forums/new-java/2370-how-return-object-arraylist.html I've tried to use this, but Processing is unable to find i. I believe this to be because i does not exist in the class, only in the main sketch. If this is so, I will find a way to place i into the method. Maybe using return.
我想AP preciate知识,如果有人知道一个更好的办法来做到这一点。
I would appreciate the knowledge if someone was aware of a better way to do this.
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); } }在哪里save.txt的是1,1,50,1,1一个txt文件。
Where save.txt is a txt file with 1,1,50,1,1.
推荐答案可惜我不能提供一个很好的详细的解答。有相当与你的项目结构的几个棘手的事情,它是code。
Unfortunately I can't provide a nice detailed answer. There are quite a few confusing things with the structure of your project and it's code.
在此期间,您曾在生物的语法错误吃()功能。试试这个:
In the meantime, you had a syntax error in the Creature's eat() function. Try this:
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); } }以上的作品,因为项目在顶部声明,所以通过了整个草图(换句话说全球)的范围可见。我不知道这是否是有意的还是你的生物与食品之间的计划有什么关系。
The above works because items is declared at the top and therefore visible through out the scope of the whole sketch(global in other words). I'm not sure if this is intentional or what relationship you plan between creatures and food.
上面可以写在一个懒惰的(但不常见/显而易见的)的方式,像这样:
The above can be written in a lazier(but less common/obvious) way like so:
void eat() { println("EAT"); //((Food)items.get(i)).nutrition(); for(Object yummy : items) ((Food)yummy).nutrition(hunger); }我假定生物将根据有多饿就喂。在某些时候,这种生物应该是充满我想,所以你也将根据营养更新生物的饿属性它得到从食物等。
I assume the creature will be fed based on how hungry it. At some point the creature should be full I suppose, so you would also update the creature's hungry property based on the nutrition it gets from food, etc.
此外,只是为了测试,我已经添加了非常有营养的食物:P
Also, just to test, I've added very nutritious food :P
items.add(new Food(mouseX, mouseY, 10000));更多推荐
从对象的ArrayList获取价值
发布评论