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

2018/2/14

Calling an object from another object

1
2
Xolkiyr Xolkiyr

2018/2/14

#
Okay, I'm trying to streamline my different ores by making a parent class called ore with all the common functions. I'm testing it using my copper class so I can revert it back if things don't go well. How can I call two+ different classes from the same function in the parent class of ore? If any of that did not make any sense, I apologize. If you need clarification feel free to ask, I'd be more than happy to further explain. Ore
/**
      * Tell us how much ore is left in this vein.
      */
        public void checkAmount(String oreType, Object oreType2)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(ore > 999){
                oreDisp = String.format("%.02f",(ore/1000)) + "k";
            }else{
                oreDisp = Integer.toString((int) ore);
            }
            if (checkHitBox(oreType2) && info == null)
            {
                info = new Label("\n  " + oreType + ": " + oreDisp + "  \n ", 24);
                getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
            }
            else if (checkHitBox(oreType2) == false)
            {
                getWorld().removeObject(info);
                info = null;
            }
            else if (Greenfoot.isKeyDown("f") && checkHitBox(oreType2))
            {
                getWorld().removeObject(info);
                info = null;
                info = new Label("\n  " + oreType + ": " + oreDisp + "  \n ", 24);
                getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
            }
        }
        
    /**
     * Making sure the mouse is within the bounds of the ore.
     */
    private boolean checkHitBox(Object oreType2){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse == null){
             return false;
        }
        if(ore < 11){
            return false;
        }
        if(
            mouse.getX() >= iron.getX()-(SIZE/2) && 
            mouse.getY() >= iron.getY()-(SIZE/2) &&
            mouse.getX() <= iron.getX()+(SIZE/2) && 
            mouse.getY() <= iron.getY()+(SIZE/2)
        ){
            return true;
        }else{
            return false;
        }
    }
Copper
/**
     * Tell us how much ore is left in this vein.
     */
    public void act(){
        super.checkAmount("Copper", this);
    }
danpost danpost

2018/2/14

#
Xolkiyr wrote...
How can I call two+ different classes from the same function in the parent class of ore?
You do not "call a class", so it is not clear as to what you are actually asking. Specifics usually help in clarifying what is wanted.
Xolkiyr Xolkiyr

2018/2/14

#
Basically, I'm trying to pass the child as an object variable to the parent without having to write new code for each ore because there will be 6 in total. iron.getX() and all subsiquent references to iron are supposed to be oreType2, not iron. That was old code.
danpost danpost

2018/2/14

#
The class/subclass relationship is not a parent/child relationship. The word "extends" means exactly that -- a class becomes an extension of another class. Any object of any subclass of World is still a World object, regardless of which subclass it is created from.
Xolkiyr wrote...
Basically, ...
"Basically" will not cut it. Be specific. What exactly are you trying to accomplish? Explain what you have, where you have it and the purpose for having it. Show codes. Details will make things go a whole lot quicker. I really think that you under some kind of misconception here and maybe the correct code, when given, will help clear things up a bit.
Vercility Vercility

2018/2/15

#
Xolkiyr wrote...
Basically, I'm trying to pass the child as an object variable to the parent without having to write new code for each ore because there will be 6 in total. iron.getX() and all subsiquent references to iron are supposed to be oreType2, not iron. That was old code.
Idk maybe I'm just stupid but I don't think that made sense From what I understand you're trying to pass a reference of a subclass instance to the main clasgs? U can do that by using World.getObjects(classname.class)
Xolkiyr Xolkiyr

2018/2/15

#
I figured out an easier way to do it. I eliminated the Iron and Copper subclasses of Ore and just made the original call:
addObject(new Ore('iron'), Greenfoot.getRandomNumber(1180), Greenfoot.getRandomNumber(550)+50);
addObject(new Ore('copper'), Greenfoot.getRandomNumber(1180), Greenfoot.getRandomNumber(550)+50);
Instead of
addObject(new Iron(), Greenfoot.getRandomNumber(1180), Greenfoot.getRandomNumber(550)+50);
addObject(new Copper(), Greenfoot.getRandomNumber(1180), Greenfoot.getRandomNumber(550)+50);
And now it differentiates without having to have separate subclasses for each.
danpost danpost

2018/2/15

#
You can probably keep the Ore class and subclass it with the Iron and Copper classes. Common methods and fields can be placed in the Ore class instead of the subclasses. You should not run into problems in doing so. Anything specific to a particular type of ore should go in its specific class. Not saying you should go that route -- just saying that you should not have had issues in doing so.
Xolkiyr Xolkiyr

2018/2/15

#
The only thing that was different was coloring and name and what inventory slot it goes into. It was causing me to have to write multiple versions of the same function in the player.class, aka Mine(Iron iron), Mine(Copper copper), etc etc. This way I only have one class to pass. The only issue now is I fudged something up and even when I pass "Copper" to the Ore Class upon creation, it comes out looking like Iron, but being called Copper and when I mine it, it's count changes but doesn't add to Inventory. On the flip side, Iron is fine except that when I mine Iron I get both Iron AND copper in my inventory. Ore.class
import greenfoot.*;

import java.awt.Color;
import java.util.Random;
import java.util.*;

/**
 * This is how Ore is supposed to be.
 * 
 * @author Christopher Kilroy
 * @version 0.4
 */
public class Ore extends Actor
{
	private static final int SIZE = 50;
    private static final int HALFSIZE = SIZE / 2;
    private Color color1 = new Color(160, 160, 160);
    private Color color2 = new Color(80, 80, 80);
    private Color color3 = new Color(10, 10, 10);

	public String oreType;

	private static final Random randomizer = new Random();
    
    private float ore;
    
    private Label info;
    
    String oreDisp;

    /**
     * Create a vein of ore with an image depicting the amount.
     */
    public Ore(String oreType2, int oreUpdate)
    {
        ore = (Greenfoot.getRandomNumber(345)+oreUpdate)/100;  // number of chunks of ore in this vein.
		if(oreType2 == "Iron"){
			Color color1 = new Color(160, 160, 160);
    		Color color2 = new Color(80, 80, 80);
    		Color color3 = new Color(10, 10, 10);
		}else if(oreType2 == "Copper"){
			Color color1 = new Color(124, 102, 22);
    		Color color2 = new Color(62, 51, 11);
    		Color color3 = new Color(10, 10, 10);
		}
		oreType = oreType2;
        updateImage();
    }
    
    /**
     * Create a vein of ore with an image depicting the amount. Default Amount
     */
    public Ore(String oreType2)
    {
        ore = (Greenfoot.getRandomNumber(345)+364789)/100;  // number of chunks of ore in this vein.
		if(oreType2 == "Iron"){
			Color color1 = new Color(160, 160, 160);
    		Color color2 = new Color(80, 80, 80);
    		Color color3 = new Color(10, 10, 10);
		}else if(oreType2 == "Copper"){
			Color color1 = new Color(124, 102, 22);
    		Color color2 = new Color(62, 51, 11);
    		Color color3 = new Color(10, 10, 10);
		}
		oreType = oreType2;
        updateImage();
    }
    
    /**
     * Tell us how much ore is left in this vein.
     */
    public void act(){
        checkAmount();
    }

    /**
     * Tell us how much ore is left in this vein.
     */
    private void checkAmount(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(ore > 999){
            oreDisp = String.format("%.02f",(ore/1000)) + "k";
        }else{
            oreDisp = Integer.toString((int) ore);
        }
        if (checkHitBox() && info == null)
        {
            info = new Label("\n  " + oreType + ": " + oreDisp + "  \n ", 24);
            getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
        }
        else if (checkHitBox() == false)
        {
            getWorld().removeObject(info);
            info = null;
        }
        else if (Greenfoot.isKeyDown("f") && checkHitBox())
        {
            getWorld().removeObject(info);
            info = null;
            info = new Label("\n  " + oreType + ": " + oreDisp + "  \n ", 24);
            getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
        }
    }
    
    /**
     * Remove some ore from this chunk of ore.
     */
    public void takeSome()
    {
        ore = ore - 10;
        if (ore <= 0) {
            getWorld().removeObject(this);
        }
        else {
            updateImage();
        }
    }

    /**
     * Update the image
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(SIZE, SIZE);

        for (int i = 0; i < (ore/10); i++) {
            int x = randomCoord();
            int y = randomCoord();

            image.setColorAt(x, y, color1);
            image.setColorAt(x + 1, y, color1);
            image.setColorAt(x + 2, y, color1);
            
            image.setColorAt(x, y + 1, color1);
            image.setColorAt(x + 1, y + 1, color1);
            image.setColorAt(x + 2, y + 1, color1);
            
            image.setColorAt(x, y + 2, color1);
            image.setColorAt(x + 1, y + 2, color1);
            image.setColorAt(x + 2, y + 2, color1);
            
            image.setColorAt(x + 3, y, color3);
            image.setColorAt(x + 3, y + 1, color3);
            image.setColorAt(x + 3, y + 2, color3);
            image.setColorAt(x, y + 3, color3);
            image.setColorAt(x + 1, y + 3, color3);
            image.setColorAt(x + 2, y + 3, color3);
            
            image.setColorAt(x + 3, y + 3, color3);
        }
        setImage(image);
    }

    /**
     * Returns a random number relative to the size of the food pile.
     */
    private int randomCoord()
    {
        int val = HALFSIZE + (int) (randomizer.nextGaussian() * (HALFSIZE / 2));
        
        if (val < 0)
            return 0;

        if (val > SIZE - 4)
            return SIZE - 4;
        else
            return val;
    }   
    
    /**
     * Making sure the mouse is within the bounds of the ore.
     */
    private boolean checkHitBox(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse == null){
             return false;
        }
        if(ore < 11){
            return false;
        }
        if(
            mouse.getX() >= this.getX()-(SIZE/2) && 
            mouse.getY() >= this.getY()-(SIZE/2) &&
            mouse.getX() <= this.getX()+(SIZE/2) && 
            mouse.getY() <= this.getY()+(SIZE/2)
        ){
            return true;
        }else{
            return false;
        }
    }
}
Player.class
import greenfoot.*;

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /** Setting up the Animated Player Images */
    /**   Idle Images */
    /**     Up */
    private GifImage upIdle = new GifImage("Player_Up_Idle.gif");
    /**     Right */
    private GifImage rightIdle = new GifImage("Player_Right_Idle.gif");
    /**     Down */
    private GifImage downIdle = new GifImage("Player_Down_Idle.gif");
    /**     Left */
    private GifImage leftIdle = new GifImage("Player_Left_Idle.gif");
    /**   Running Images */
    /**     Up */
    private GifImage upRun = new GifImage("Player_Up_Run.gif");
    /**     Right */
    private GifImage rightRun = new GifImage("Player_Right_Run.gif");
    /**     Down */
    private GifImage downRun = new GifImage("Player_Down_Run.gif");
    /**     Left */
    private GifImage leftRun = new GifImage("Player_Left_Run.gif");
    /**   Mining Images */
    /**     Up */
    private GifImage upMine = new GifImage("Player_Up_Mining.gif");
    /**     Right */
    //private GifImage rightMine = new GifImage("Player_Right_Mining.gif");
    /**     Down */
    //private GifImage downMine = new GifImage("Player_Down_Mining.gif");
    /**     Left */
    //private GifImage leftMine = new GifImage("Player_Left_Mining.gif");
    
    /** Set Direction [1= right] [2= down] [3= left] [4= up] */
    private int playerDir = 1;
    
    /** Set Player State [0= Idle] [1= Moving] [2= Mining] */
    private int playerState = 0;
    private int playerMiningCooldown = 75;
    
    /** Set Player Stats and Inventory Variables */
    private int playerHP = 100;
    private int playerIron = 0;
    private int playerCopper = 0;
    private int playerGold = 0;
    private int playerSilver = 0;
    private int playerCoal = 0;
    
    /** General Variables */
    private int steps = 1;
    private String key;
    private String text;
    private int sfx_hold = 1;
    
    /** Initiate Labels for Inventory and Status */
    private Counter playerHealth = null;
    private Counter playerO2 = null;
    private Counter inventoryLabel = null;
    private Counter playerIronC = null;
    private Counter playerCopperC = null;
    
    /**
     * Setup the Player
     */
    public void Player(){
        
    }
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getPlayer();
        pMove();
        if(playerHealth == null){
            playerHealth = new Counter("  Health: ", 100);
            getWorld().addObject(playerHealth, 155, 10);
            playerHealth.setValue(100);
            playerHealth.setValue2(100);
        }
        if(playerO2 == null){
            playerO2 = new Counter("  Oxygen: ", 100, " kPa");
            getWorld().addObject(playerO2, 155, 26);
            playerO2.setValue(49);
        }
        if(inventoryLabel == null){
            inventoryLabel = new Counter("  Inventory", 500);
            getWorld().addObject(inventoryLabel, 460, 10);
            inventoryLabel.setValue(-1);
        }
        if(playerIronC == null){
            playerIronC = new Counter("  Iron: ", 75);
            getWorld().addObject(playerIronC, 247, 26);
        }
        if(playerCopperC == null){
            playerCopperC = new Counter("  Copper: ", 100);
            getWorld().addObject(playerCopperC, 335, 26);
        }
    }
    
    /**
     * Decide Movements
     */
    private void pMove(){
        if(!Greenfoot.isKeyDown("f")){
            playerMiningCooldown = 75;
            if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")){
                playerDir = 1;
                playerState = 1;
                setLocation(getX() + steps,getY());
            }else if(Greenfoot.isKeyDown("s") || Greenfoot.isKeyDown("down")){
                playerDir = 2;
                playerState = 1;
                setLocation(getX(),getY() + steps);
            }else if(Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")){
                playerDir = 3;
                playerState = 1;
                setLocation(getX() - steps,getY());
            }else if(Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")){
                playerDir = 4;
                playerState = 1;
                setLocation(getX(),getY() - steps);
            }else{
                playerState = 0;
            }
        }else if(Greenfoot.isKeyDown("f")){
            playerState = 2;
            playerMiningCooldown--;
            if(playerMiningCooldown == 0){
                checkOre();
                playerMiningCooldown = 75;
            }
        }else{
            playerState = 0;
        }
        if("z".equals(checkKey())){
            playerHP -= 20;
            playerHealth.setValue(playerHP);
        }
    }
    
    /**
     * Is there any ore here where I am? If so, mine some!.
     */
    public void checkOre()
    {
        Ore oreObj = (Ore) getOneIntersectingObject(Ore.class);
        if (oreObj != null) {
            Mine(oreObj);
        }
    }
    
    /**
     * Mine some Ore from an ore vein.
     */
    public void Mine(Ore oreObj)
    {
        oreObj.takeSome();
		if(oreObj.oreType == "Iron"){
        	playerIron += 10;
        	playerIronC.setValue(playerIron);
    	}
		if(oreObj.oreType == "Iron"){
			playerCopper += 10;
        	playerCopperC.setValue(playerCopper);
        }
        playMiningSound();
    }
    
    public void playMiningSound()
    {
        if(sfx_hold == 1){
            int rsnd = Greenfoot.getRandomNumber(5);
            if(rsnd == 0){
                Greenfoot.playSound("mining-ore-1.mp3");
            }else if(rsnd == 1){
                Greenfoot.playSound("mining-ore-2.mp3");
            }else if(rsnd == 2){
                Greenfoot.playSound("mining-ore-3.mp3");
            }else if(rsnd == 3){
                Greenfoot.playSound("mining-ore-4.mp3");
            }else{
                Greenfoot.playSound("mining-ore-5.mp3");
            }
            sfx_hold = 0;
        }else{
            sfx_hold++; 
        }
    }
    
    /**
     * Figure out image
     */
    private void getPlayer(){
        if(playerState == 0){                       //Idle
            if(playerDir == 1){                     //Right
                setImage(rightIdle.getCurrentImage());
            }
            if(playerDir == 2){                     //Down
                setImage(downIdle.getCurrentImage());
            }
            if(playerDir == 3){                     //Left
                setImage(leftIdle.getCurrentImage());
            }
            if(playerDir == 4){                     //Up
                setImage(upIdle.getCurrentImage());
            }
        }
        if(playerState == 1){                       //Moving
            if(playerDir == 1){                     //Right
                setImage(rightRun.getCurrentImage());
            }
            if(playerDir == 2){                     //Down
                setImage(downRun.getCurrentImage());
            }
            if(playerDir == 3){                     //Left
                setImage(leftRun.getCurrentImage());
            }
            if(playerDir == 4){                     //Up
                setImage(upRun.getCurrentImage());
            }
        }
        if(playerState == 2){                       //Mining
            if(playerDir == 1){                     //Right
                setImage(rightIdle.getCurrentImage());
            }
            if(playerDir == 2){                     //Down
                setImage(downIdle.getCurrentImage());
            }
            if(playerDir == 3){                     //Left
                setImage(leftIdle.getCurrentImage());
            }
            if(playerDir == 4){                     //Up
                setImage(upMine.getCurrentImage());
            }
        }
    }
        
    /**
     * Arbitrary GetKey Function to get a single keypress.
     */
    public String checkKey()
    {
        String key = Greenfoot.getKey();
        return key;
    }
}
EDIT: Figured out why it was adding to both Copper and Iron, I copied and pasted code and didn't change the second "Iron" to "Copper"
Super_Hippo Super_Hippo

2018/2/15

#
In the Ore constructors, you create new color variables instead of using the fields you already have. Remove all instances of "Color" at the beginning of these lines.
danpost danpost

2018/2/15

#
You should not compare possible like String objects using conditional comparison operators, like '=='. With objects, '==' means "is the same object as", not "is equivalent to". Better would be to use the 'equals' comparison method:
if ("Iron".equals(oreObj.oreType))
Always put the new String object (a string in quotes IS a new String object) first when possible as you cannot call a method on a 'null' value (in case the referenced String has a 'null' value). In your case it probably does not matter; but, it is a good practice to be in the habit of doing.
Xolkiyr Xolkiyr

2018/2/15

#
Super_Hippo wrote...
In the Ore constructors, you create new color variables instead of using the fields you already have. Remove all instances of "Color" at the beginning of these lines.
I did that and now it's throwing up a "cannot find symbol - variable color1" error. Thanks again for all your help too danpost. You're always a big help.
Vercility Vercility

2018/2/15

#
Well the error is already telling u whats wrong, the compiler cant find your "color1" variable. Either you misspelled one of its uses or its not visible where u call it. If u're changing code inbetween errors provide us with the new one atleast tho >.< By the way, your first case in the ore constructor is unnecessary since your colors already have these values initially
danpost danpost

2018/2/15

#
Vercility wrote...
By the way, your first case in the ore constructor is unnecessary since your colors already have these values initially
Actually, there are two sets of colors -- one for iron and one for copper, which need to be set depending on the ore. The assignments in the field declaration lines can be removed. These colors can be set by the constructors of the subclasses (but the fields should remain in the Ore class). If not using subclasses, make sure you set the field values while not creating local variables with the same name.
Vercility Vercility

2018/2/15

#
danpost wrote...
Vercility wrote...
By the way, your first case in the ore constructor is unnecessary since your colors already have these values initially
Actually, they are two sets of colors -- one for iron and one for copper. These colors can be set by the constructors of the subclasses (but the fields should remain in the Ore class). If not using subclasses, make sure you set the field values while not creating local variables with the same name.
Edit: Ye nvm, basically what i meant.
Xolkiyr Xolkiyr

2018/2/15

#
Updated Code for Ore.Class and no subclasses anymore.
import greenfoot.*;

import java.awt.Color;
import java.util.Random;
import java.util.*;

/**
 * A Class to describe how ore is interacted with 
 * and interacts with the world and other objects.
 * 
 * @author Christopher Kilroy 
 * @version 0.4
 */
public class Ore extends Actor
{
	private static final int SIZE = 50;
    private static final int HALFSIZE = SIZE / 2;
    //private Color color1 = new Color(0,0,0);
    //private Color color2 = new Color(0,0,0);
    //private Color color3 = new Color(0,0,0);

	public String oreType;

	private static final Random randomizer = new Random();
    
    private float ore;
    
    private Label info;
    
    String oreDisp;

    /**
     * Create a vein of ore with an image depicting the amount.
     */
    public Ore(String oreType2, int oreUpdate)
    {
        ore = (Greenfoot.getRandomNumber(345)+oreUpdate)/100;  // number of chunks of ore in this vein.
		if("Iron".equals(oreType2)){
			Color color1 = new Color(160, 160, 160);
            Color color2 = new Color(80, 80, 80);
            Color color3 = new Color(10, 10, 10);
			oreType = "Iron";
		}else if("Copper".equals(oreType2)){
			Color color1 = new Color(124, 102, 22);
            Color color2 = new Color(62, 51, 11);
            Color color3 = new Color(10, 10, 10);
			oreType = "Copper";
		}
        updateImage();
    }
    
    /**
     * Create a vein of ore with an image depicting the amount. Default Amount
     */
    public Ore(String oreType2)
    {
        ore = (Greenfoot.getRandomNumber(345)+364789)/100;  // number of chunks of ore in this vein.
		if("Iron".equals(oreType2)){
			Color color1 = new Color(160, 160, 160);
            Color color2 = new Color(80, 80, 80);
            Color color3 = new Color(10, 10, 10);
			oreType = "Iron";
		}else if("Copper".equals(oreType2)){
			Color color1 = new Color(124, 102, 22);
            Color color2 = new Color(62, 51, 11);
            Color color3 = new Color(10, 10, 10);
			oreType = "Copper";
		}
        updateImage();
    }
    
    /**
     * Tell us how much ore is left in this vein.
     */
    public void act(){
        checkAmount();
    }

    /**
     * Tell us how much ore is left in this vein.
     */
    private void checkAmount(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(ore > 999){
            oreDisp = String.format("%.02f",(ore/1000)) + "k";
        }else{
            oreDisp = Integer.toString((int) ore);
        }
        if (checkHitBox() && info == null)
        {
            getWorld().removeObject(info);
            info = null;
			info = new Label("\n  " + oreType + ": " + oreDisp + "  \n ", 24);
            getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
        }
        else if (checkHitBox() == false)
        {
            getWorld().removeObject(info);
            info = null;
        }
        else if (Greenfoot.isKeyDown("f") && checkHitBox())
        {
            getWorld().removeObject(info);
            info = null;
            info = new Label("\n  " + oreType + ": " + oreDisp + "  \n ", 24);
            getWorld().addObject(info, mouse.getX(), mouse.getY()-20);
        }
    }
    
    /**
     * Remove some ore from this chunk of ore.
     */
    public void takeSome()
    {
        ore = ore - 10;
        if (ore <= 0) {
            getWorld().removeObject(this);
        }
        else {
            updateImage();
        }
    }

    /**
     * Update the image
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(SIZE, SIZE);

        for (int i = 0; i < (ore/10); i++) {
            int x = randomCoord();
            int y = randomCoord();

            image.setColorAt(x, y, color1);
            image.setColorAt(x + 1, y, color1);
            image.setColorAt(x + 2, y, color1);
             
            image.setColorAt(x, y + 1, color1);
            image.setColorAt(x + 1, y + 1, color1);
            image.setColorAt(x + 2, y + 1, color1);
             
            image.setColorAt(x, y + 2, color1);
            image.setColorAt(x + 1, y + 2, color1);
            image.setColorAt(x + 2, y + 2, color1);
             
            image.setColorAt(x + 3, y, color3);
            image.setColorAt(x + 3, y + 1, color3);
            image.setColorAt(x + 3, y + 2, color3);
            image.setColorAt(x, y + 3, color3);
            image.setColorAt(x + 1, y + 3, color3);
            image.setColorAt(x + 2, y + 3, color3);
             
            image.setColorAt(x + 3, y + 3, color3);
        }
        setImage(image);
    }

    /**
     * Returns a random number relative to the size of the food pile.
     */
    private int randomCoord()
    {
        int val = HALFSIZE + (int) (randomizer.nextGaussian() * (HALFSIZE / 2));
        
        if (val < 0)
            return 0;

        if (val > SIZE - 4)
            return SIZE - 4;
        else
            return val;
    }   
    
    /**
     * Making sure the mouse is within the bounds of the ore.
     */
    private boolean checkHitBox(){
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse == null){
             return false;
        }
        if(ore < 11){
            return false;
        }
        if(
            mouse.getX() >= this.getX()-(SIZE/2) && 
            mouse.getY() >= this.getY()-(SIZE/2) &&
            mouse.getX() <= this.getX()+(SIZE/2) && 
            mouse.getY() <= this.getY()+(SIZE/2)
        ){
            return true;
        }else{
            return false;
        }
    }
}
There are more replies on the next page.
1
2