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

2012/9/14

Object interaction Problem

Mikeson Mikeson

2012/9/14

#
I have object (A) that requires to be clicked with the mouse. when this happens I have a seperate object (B) (class) that is spose to act that is where Im having the problem. For (B) i have this in the act section if(Greenfoot.mouseClicked(switchLight.class)) { methods that work go here} Am I on the right track ? I.e Probly the same as clicking a switch and then something else happens.
danpost danpost

2012/9/14

#
It appears that you are on the right track. Does it not work that way? or, are you having a problem getting it to work that way?
Gevater_Tod4711 Gevater_Tod4711

2012/9/14

#
I think a simple way to do that would be if object a could call object B's method. This should help:
//in class A:
public class A ...

private B b; // B is the classname of the object you called B.

public void act() {
    if (b == null) {
        if (getWorld().getObject(B.class).size() != 0) {
            b = (B) getWorld().getObjects(B.class).get(0); // this gives the reference to the first object from type B the method getObjects has found.
            
        }
    }
    else if (Greenfoot.mouseKlicked(this)) {
        b.methodYouWantToExecute();
    }
}
SPower SPower

2012/9/14

#
Some advice:
Mikeson wrote...
if(Greenfoot.mouseClicked(switchLight.class))
I see that your switchLight class starts with a lowercase, but it should start with an Uppercase, so your class should be called SwitchLight. Java won't complain if you don't do that, but you confuse others.
Mikeson Mikeson

2012/9/15

#
Ive uploaded it so if you want to much around with it plz do so and reply the fix http://www.greenfoot.org/scenarios/5943
danpost danpost

2012/9/15

#
I appears your switch was working fine. The problem is that 'thatHouse' in the 'Armed' class was not set to anything (was still null). Before I give the solution to that problem, let me explain something. You have a class called 'Armed' and a sub-class of that one called 'Unarmed'. 'Armed' and 'unarmed' are states of the same object and should be controlled by a field (variable) within the object's class. If you must have seperate classes for them, they should be both sub-classes of the same object's class (sister classes, instead of parent-child classes). Because you have it set up the way you do, it will require a little work to get it to do what you want. First off, since 'Unarmed' will 'act' and 'triger' using 'Armed' methods, we have to over-ride them. The 'Unarmed' class should look like this:
import greenfoot.*;

public class Unarmed extends Armed
{
    public void act() // to over-ride 'act' in 'Armed'
    {
    }
    
    public void triger() // to over-ride 'triger' in 'Armed'
    {
    }
}
You were getting the error message because they were not over-riden as such. Now, also because 'Unarmed' is a sub-class of 'Armed', we need a constructor in the 'Armed' class that it can use, as well as a constructor that recieves the House object that is to explode. So, add this to the 'Armed' class:
public Armed() // for use by 'Unarmed'
{
}

public Armed(House explodingHouse) // for use by your sub-class of World
{
    thatHouse = explodingHouse;
}
Finally we must send 'Armed' the house that is to explode. Therefore, the House object must be created first. In the 'grass' sub-class of World, replace the pertinent code with:
House house = new House();
addObject(house, 311, 258);
Armed armed = new Armed(house); // notice the parameter
addObject(armed, 78, 421);
That should fix your problems. If any trouble, post back.
danpost danpost

2012/9/15

#
I played around with it a little: - deleted the 'Armed' and 'Unarmed' classes - created 'Plunger' class (sub-class of Actor) to replace them with (gave it the image you had for 'Armed' -- "explosivePlungerArmed.png") - replaced 'Armed' and 'armed' in the 'grass' world class with 'Plunger' and 'plunger' - used the following code in the 'Plunger' class
import greenfoot.*;

public class Plunger extends Actor
{
    GreenfootImage plungedImage = new GreenfootImage("explosivePlungerUnarmed.png");
    boolean armed = true;
    House explodingHouse = null;
    
    public Plunger(House inHouse)
    {
        explodingHouse = inHouse;
    }

    public void act() 
    {
        if(armed && Greenfoot.mousePressed(this))
        {
            setImage(plungedImage);
            setLocation(getX(), getY() + 5);
            armed = false;
            explodingHouse.explode(); 
        }
    }    
}
The end result is no different.
danpost danpost

2012/9/15

#
Just realized that in my first post, the Unarmed class does not need to over-ride the 'triger' method. Only because as long as 'act' is over-riden, the 'triger' method is not called from this class. That means that you do (did) not have to make any changes to the 'Unarmed' class. That means that the NullPointerException error was due to the fact that 'thatHouse' was never given a House object to hold.
You need to login to post a reply.