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

2012/5/2

Subclass variables from another object

Gazzzah Gazzzah

2012/5/2

#
I hoped to make an actor communicate to another actor using the getOneIntersectingObject method. For example, if 'projectile' collides with a 'ship' then it will 'inflict(damage);' on the ship based on the projectiles value for damage. What I've gone with is as follows: (This is in the class 'thing', it's my main parent class)
public thing hitThing;

    public boolean hit(Class clss)
    {
        hitThing = getOneIntersectingObject(clss);
        return hitThing != null;
    }
So then presumably I could tell projectile (which extends thing)
    if (hit(ship.class))
    {
        hitThing.inflict(damage); //damage being a pre-defined variable
        getWorld.removeObject(this);
    }
But the problem is that in the 'hit' method, it complains that getOneIntersectingObject applies to Actor. If I change it from 'thing' to 'Actor' then when I try to use 'inflict' it says it cannot find the 'inflict' method. Any ideas?
SPower SPower

2012/5/2

#
You need to do this in hit(Class clss):
hitThing = (thing) getOneIntersectingObject(clss);
Because the getOneIntersectingObject returns an Actor object, you can't put it in an object from the type thing. You need to put "(thing)" before it, so you convert it into a thing object.
SPower SPower

2012/5/2

#
And if you would change hitThing to an Actor, you haven't got the inflict method, because it's stored in the thing class, not in the Actor class. And I give you a tip: start your class names with a uppercase, that's standard in java. Java won't complain if you don't do that, but you confuse other programmers.
SPower SPower

2012/5/2

#
And if you would change hitThing to an Actor, you haven't got the inflict method, because it's stored in the thing class, not in the Actor class. And I give you a tip: start your class names with a uppercase, that's standard in java. Java won't complain if you don't do that, but you confuse other programmers.
SPower SPower

2012/5/2

#
Sorry for double post :(
Gazzzah Gazzzah

2012/5/2

#
No worries, thanks! That's helpful, and thanks for the pro tip.
You need to login to post a reply.