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

2014/5/6

Not So Good With Instances Yet

Tentacle_Panther Tentacle_Panther

2014/5/6

#
What I'm going for with this is to give the testhero object that I'm putting in a name, so I may then refer to the instance named "red" and thus have a program.
public class Battleground extends World
{
    public TestHero red;
    /**
     * Constructor for objects of class Battleground.
     * 
     */
    public Battleground()
    {    
        // Create a new world with 20x20 cells with a cell size of 32x32 pixels.
        super(20, 20, 32);
        setBackground("Soil.jpg");
        TestHero red = new TestHero();
        Governor chief = new Governor();
        addObject (chief, 0, 0);
        addObject (red, 2, 2);
        setPaintOrder(Hero.class, Hospital_Tent.class, TestEnemy.class);
    }
    public TestHero getTestHero()
    {
        return red;
    }
}
I've cobbled this together after reading a few threads here, but it's still just spawning a TestHero named TestHero that holds a private TestHero red value of null. Assistance would be greatly appreciated.
Zamoht Zamoht

2014/5/6

#
You declare a public reference to a TestHero object and call it red. The problem is that you declare the same thing in the constructor, but they are not the same reference so to speak. I'm not sure if that makes any sense, but anyways. To fix your problem you have to remove "TestHero" in the Battleground constructor at line 13. Leaving you with this line instead "red = new TestHero();".
danpost danpost

2014/5/6

#
This is called 'shadowing'. In this case, you are creating a local variable on line 13 with the same name as the instance field declared on line 3. Line 16 will add the object stored in the local variable to the world because the one declared in the enclosing scope is now being shadowed. There is a section on this in the Java tutorials near the bottom of this page.
Tentacle_Panther Tentacle_Panther

2014/5/7

#
Thank you, I am now very aware and much more educated about what I was doing wrong and how I was doing it. (Shadowing seems like a very useful tool to have at one's disposal) Now I'd much appreciate if someone could point me in the right direction to get to what I'm going for. My goal here is to create a TestHero object and be able to refer to it specifically from other classes. (spawn "steve" in the world constructor and then write enemy AI to move blindly towards the current location of "Steve") This seems like a pretty core concept, so a link to the proper tutorial would be appreciated as well. (or instead.)
danpost danpost

2014/5/7

#
Well, using 'getObjectsInRange' using a large enough radius should find him. You would extract 'steve' from the list returned and turn toward his location. This would be done in the class of the enemy, with code similar to the following:
java.util.List<TestHero> heros = getObjectsInRange(500, TestHero.class);
if (!heros.isEmpty())
{
    TestHero steve = heros.get(0);
    turnTowards(steve.getX(), steve.getY());
}
move(2);
Tentacle_Panther Tentacle_Panther

2014/5/8

#
Looks like what I was trying to do originally is not actually possible. Alright, I'll just call that a learning experience. My goal now is to make the game sort of turn-based. My plan to do this is to remove the code from act methods in favor of individual methods which will be called conditionally from a parent class's act method. My problem is that I cannot get the parent class to call these methods at all because it will not acknowledge the existence of the instances.
java.util.List<TestHero> heroes = getObjectsInRange(500, TestHero.class); 
               if (!heroes.isEmpty())
              {
                TestHero red = heroes.get(0);
              }
                 
            red.setImage("Hospital_Tent.png");
The setImage command (which has no real significance, it's just there for a visible indication of working code) causes a NullPointerException, even though the if statement is running. I'm pretty much lost at this point.
danpost danpost

2014/5/9

#
Line 4 declares an variable to hold an object reference to a TestHero object. It is within the 'if' block. Its scope is therefore limited to within the 'if' block. Declare the variable before the 'if' block and it will then recognize it after the 'if' block:
TestHero red = null;
if (!heroes.isEmpty()) red = heroes.get(0);
if (red != null) red.setImage("Hospital_Tent.png");
davmac davmac

2014/5/9

#
Looks like what I was trying to do originally is not actually possible. Alright, I'll just call that a learning experience.
It certainly is. To access from another actor class you'd just:
TestHero red = ((BattleGround) getWorld()).getTestHero();
(Shadowing seems like a very useful tool to have at one's disposal)
No, in general it's a problem which you should avoid! :)
Tentacle_Panther Tentacle_Panther

2014/5/9

#
Thank you muchly! Though I was frustrated and pouty yesterday, with your help I have today completed what I'm going to call a pre-alpha version of my game!
You need to login to post a reply.