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

2017/6/8

Help for Game: Images

spnjh spnjh

2017/6/8

#
I'm trying to create a game that will increase the points of a counter if the object is placed in the correct category. My class "vegetables" is a subclass of the actor class "category." The image of the food (actor class) randomly changes between 5 images of objects that belong to 5 different categories. I want to make the code so that if the image of the object is equal to a certain image ("food-0.png", "food-1.png", etc.), 5 points will be added to the counter. In my coding, there seems to be an error and I'm not sure how to fix it. public class Vegetables extends Category { private Counter counter; private GreenfootImage food = getImage(Food.class); private GreenfootImage vegetables = new GreenfootImage("food-2.png"); private GreenfootImage grains = new GreenfootImage("food-1.png"); private GreenfootImage fruits = new GreenfootImage("food-0.png"); private GreenfootImage protein = new GreenfootImage("food-3.png"); private GreenfootImage dairy = new GreenfootImage("food-4.png"); /** * Create a new vegetable category with a reference to the counter in the game. */ public Vegetables(Counter pointCounter) { counter = pointCounter; } /** * Act - do whatever the Category wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor Food = getOneIntersectingObject(Food.class); if (Food!=null) { World GameWorld = getWorld(); GameWorld.removeObject(Food); checkFood(); } } /** * Check Food */ public void checkFood() { if (food == vegetables) { counter.add(5); createNewFood(); if (counter.getValue() >= 50) { gameWin(); } } else if (food != vegetables) { reset(); gameOver(); } }
danpost danpost

2017/6/8

#
This is what I see in your code: (1) a bunch of private GreenfootImage fields and no 'setImage' method calls anywhere (maybe the images are set somewhere else in the class code); (2) an invalid parameter for a 'getImage' method call, the calling of which is also premature (the reference to 'food' should be set in the class constructor -- or after the instance is created); and (3) the removal of any touching food before calling 'checkFood', which compares 'vegetables' to 'food', where the image (or no image) is set to 'food' when the Vegetables object is created (having nothing to do with the image of the previously intersecting Food object) What I would like to see also is the code to the 'createNewFood' method.
spnjh spnjh

2017/6/8

#
- In the food class, the image is set with this code: /** * Change the image of this food */ public void changeImage() { imageNumber = Greenfoot.getRandomNumber(5); setImage ("food-" + imageNumber + ".png"); } - I'm new to programming so I'm not sure what a valid parameter would be. - 'createNewFood' method: /** * Create New Food */ public void createNewFood() { Food food = new Food(); GameWorld gameworld = new GameWorld(); gameworld.addObject (food, 10, 250); }
danpost danpost

2017/6/8

#
Then, I would like to know where 'changeImage' is being called from. The 'getImage' method requires absolutely zero parameters and returns the image that is currently set to the actor. The 'setImage' line in the 'changeImage' method will not be the same as any of those in the GreenfootImage fields (they will be different instances of the image). Therefore you cannot compare them using '=='. Best is to set the image to one of the fields so that you can use '==' for comparison. However, to be able to do that, the fields will need to be class fields ('static' fields).
spnjh spnjh

2017/6/8

#
The 'changeImage' is being called from the food class. The reason why I did not use 'getImage' is because the method is in the vegetable subclass of the category class so I did not know how to access information from a separate class (food class) to be used in the vegetable class. If I use the class (static fields), would it be correct to use "public static final int" because when I tried to put the following method in the food class, there was an error because it could not override getImage() in greenfoot.Actor. public static final int getImage() { return image; }
danpost danpost

2017/6/8

#
As far as the given method: First, the image belongs to an instance of the class, so it cannot be a static method. Secondly, if the method returns an image, then why would you say it returns an int value (int should be GreenfoottImage). But, the method is already a part of the Actor class; you do not need to create, or re-create, it. In general: If the images are to be given to Food objects, then the fields should be located in the Food class and the Food class constructor should set one of the images to the created instances. The images can be put in an array to give each an index value that can be randomly chosen. Each of the food group classes can be given a class field to hold the particular image it should be looking for. You can use your world constructor to assign the appropriate image to each of them.
You need to login to post a reply.