问题描述
我正在制作一个虚拟宠物游戏,我现在正在尝试包括饥饿,但是我不确定如何使吃功能起作用.我试图通过增加一种食物的营养价值来减少它.该值位于存储在数组列表中的对象中.
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;
(吃完后会传入)
内部
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.org/new-java/2370-how-return-object-arraylist.html我试过使用它,但处理无法找到 i.我相信这是因为我不存在于课堂中,只存在于主草图中.如果是这样,我会想办法将 i 放入方法中.也许使用返回.
http://www.java-forums.org/new-java/2370-how-return-object-arraylist.htmlI'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.
如果有人知道更好的方法来做到这一点,我将不胜感激.
I would appreciate the knowledge if someone was aware of a better way to do this.
代码
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.
推荐答案
很遗憾,我无法提供一个很好的详细答案.您的项目结构和代码有很多令人困惑的地方.
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.
与此同时,您在 Creature 的 eat()
函数中出现语法错误.试试这个:
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);
}
我假设该生物会根据它的饥饿程度而被喂食.我想在某个时候这个生物应该是饱的,所以你也可以根据营养更新生物的 hungry
属性它来自食物等.
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 nutritionit gets from food, etc.
另外,为了测试,我添加了非常有营养的食物:P
Also, just to test, I've added very nutritious food :P
items.add(new Food(mouseX, mouseY, 10000));
这篇关于从对象的 Arraylist 中获取值 - 处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!