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

2019/11/28

How do I access an object from another class?

NB111 NB111

2019/11/28

#
I want to make it so that if I click on a certain object in the world, then the image for that object changes, however I can't seem to find a way to access the object that's in the world class from my other class. Here's the code I have so far World code:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    public Blank blank1;
    public Blank blank2;
    public Blank blank3;
    public Blank blank4;
    public Blank blank5;
    public Blank blank6;
    public Blank blank7;
    public Blank blank8;
    public Blank blank9;
    public Blank blank10;
    public Blank blank11;
    public Blank blank12;
    public Blank blank13;
    public Blank blank14;
    public Blank blank15;
    public Blank blank16;
    public Blank blank17;
    public Blank blank18;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        populateWorld();
    }
    
    public void populateWorld()
    {
        blank1 = new Blank();
        addObject(blank1, 50, 200);
        blank7 = new Blank();
        addObject(blank7, 150, 200);
        blank18 = new Blank();
        addObject(blank18, 250, 200);
        blank2 = new Blank();
        addObject(blank2, 350, 200);
        blank11 = new Blank();
        addObject(blank11, 450, 200);
        blank5 = new Blank();
        addObject(blank5, 550, 200);
        
        blank3 = new Blank();
        addObject(blank3, 50, 50);
        blank8 = new Blank();
        addObject(blank8, 150, 50);
        blank17 = new Blank();
        addObject(blank17, 250, 50);
        blank15 = new Blank();
        addObject(blank15, 350, 50);
        blank4 = new Blank();
        addObject(blank4, 450, 50);
        blank16 = new Blank();
        addObject(blank16, 550, 50);
        
        blank13 = new Blank();
        addObject(blank13, 50, 350);
        blank14 = new Blank();
        addObject(blank14, 150, 350);
        blank9 = new Blank();
        addObject(blank9, 250, 350);
        blank6 = new Blank();
        addObject(blank6, 350, 350);
        blank12 = new Blank();
        addObject(blank12, 450, 350);
        blank10 = new Blank();
        addObject(blank10, 550, 350);
    }
    
    public Blank getBlanks()
    {
        return blank13;
    }
}

Class code:

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Blank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Blank extends Actor
{  
    public Blank()
    {
        
    }
    
    public void act() 
    {
        hasMatch();
    }
    
    public void hasMatch()
    {   
        MyWorld w = (MyWorld) getWorld();
        Blank blank = w.getBlanks();
        
        if(Greenfoot.mouseClicked(blank13))
        {
            setImage("3spades.png");
        } 
    }
}

Nosson1459 Nosson1459

2019/11/28

#
on line 27 of the Blank class you're using the variable name of the world class (blank13) when in your Blank class you named it blank
Nosson1459 Nosson1459

2019/11/29

#
Also just by way of suggestion you can cut your code by at least half if you use an Array, and For loops to place them in the world. like this:
private Blank[] blank=new Blank[17];
to instantiate them use a For loop like this:
for(i=0;i<18;i++)
{
    blank[i]=new Blank();
}
to add them in to the world use this loop
int n=0;
for(j=50;j<351;j+=150)
{ 
    for(int i=50; i<551; I+=100)
    {
        addObject(blank[n],i,j);
        n++;
    }
}
danpost danpost

2019/11/29

#
Nosson1459 wrote...
Also just by way of suggestion you can cut your code by at least half if you use an Array, and For loops to place them in the world. like this:
private Blank[] blank=new Blank[17];
to instantiate them use a For loop like this: << Code Omitted >> to add them in to the world use this loop << Code Omitted >>
The quoted line of code should be:
private Blank[] blank = new Blank[18];
and I would have done the rest with:
for (int i=0; i<blank.length; i++)
{
    blank[i] = new Blank();
    addObject(blank[i], 50+100*(i%6), 50+150*(i/6));
}
You need to login to post a reply.