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

2014/1/23

Health doesn't work!!!

Bean4221 Bean4221

2014/1/23

#
I'm trying to make a health thing for my game. When I try to compile it says "bad operand types for binary "*"" Also "first type greenfoot.GreenfootImage; second type int." here's the code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Health here. * * @author (your name) * @version (a version number or a date) */ public class Health extends Actor { int health = 100; /** * Act - do whatever the Health wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage (82, 17)); GreenfootImage health = getImage(); health.setColor(Color.WHITE); health.drawRect(0, 0, 81, 16); health.setColor(Color.RED); int rectWidth = health * 1; health.fillRect(1, 1, rectWidth, 15); } }
bourne bourne

2014/1/23

#
Yes within the scope, health is defined as a GreenfootImage Object. It is not a number there. What you can do is this (to access the int health field): int rectWidth = this.health * 1; Or you can use a different name for your GreenfootImage
Bean4221 Bean4221

2014/1/23

#
ok thanks
You need to login to post a reply.