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

2011/12/6

"Incompatible types" Whazhappaning?

darkmist255 darkmist255

2011/12/6

#
Just a quick question (I hope :D). When I did this:
public class PlainRock extends Actor
{
    Room room = (Room)getWorld();
    Stats stats = room.stats;
    
    public void act() 
    {
        if(getIntersectingObjects(Character.class))
        {
            //make it damage player on collision (for a health test)
            stats.damagePlayer(1);
        }
    }    
}
It doesn't like "(Character.class)" and says "Incompatible types". What does it mean by that?
darkmist255 darkmist255

2011/12/6

#
Wait, I just realized that I can't put that in an if statement since it doesn't return a boolean value. I changed it to:
if(getIntersectingObjects(Character.class) != null)
Should work, I'll see what happens :D. **update** It just drains the player's health extremely fast (10 health, so in 10 act cycles I assume). Wonder what's going on?
danpost danpost

2011/12/7

#
Remember, that 'getIntersectingObjects' returns a List object. I do not believe that an empty list will ever return null.
darkmist255 darkmist255

2011/12/7

#
Should I instead try "getOneIntersectingObject"? I would assume the difference is that this is the same, just not a list? **update** My assumption was correct, it works :D. Thanks for the help!
mjrb4 mjrb4

2011/12/7

#
You're correct in that for this purpose getOneIntersectingObject() is most likely what you want. However, for future bear in mind that danpost is correct as to why your previous method didn't work the getIntersectingObjects() method never returns null, it always returns a list (it's just empty when there's no intersecting objects, so you'd check for that instead.)
darkmist255 darkmist255

2011/12/9

#
Ok so this is a new problem arising from my "simple wall" post, but this is more the place to post it. To work with the angled view illusion I'm going for in my game, I'm trying this for my collision detection
public class GroundMover extends Actor
{
nonPassable object;
    public void detectCollision(int moveSpeed, int xMomentum, int yMomentum)
    {
        if(getOneIntersectingObject(nonPassable.class) != null)
        {
            object = getOneIntersectingObject(nonPassable.class);
            if(object.getY() >= (getY() - 5)) //gives angled illusion
            {
                if(xMomentum > 0) //moving right
                {
                    setLocation((getX() - moveSpeed), getY());
                }
                if(xMomentum < 0) //moving left
                {
                    setLocation((getX() + moveSpeed), getY());
                }
                if(yMomentum > 0) //moving down
                {
                    setLocation(getX(), (getY() - moveSpeed));
                }
                if(yMomentum < 0) //moving up
                {
                    setLocation(getX(), (getY() + moveSpeed));
                }
            }
        }
    }
}
But at the line "object = getOneIntersectingObject(nonPassable.class);" it says incompatible types. Now, is this because nonPassable is a class that has many sub-classes? Or does that not affect it? I can't think of why else it isn't working, since virtually the same thing works a line earlier. This code works without the "object" parts.
Builderboy2005 Builderboy2005

2011/12/9

#
It is because the method getOneIntersectingObject() returns an Actor, not a NonPassable, and so Java is complaining. A quick and simple fix is to cast the returned actor into a NonPassable like so:
object = (NonPassable) getOneIntersectingObject(NonPassable.class);
This tells Java to try to turn the returned object into a NonPassable (which will error if it can't) and then store the NonPassable into object. Also, you might want to get into the habit of capitalizing the first letter of all your class names, as it makes your code a little bit easier to read for other Java coders.
darkmist255 darkmist255

2011/12/9

#
Ahh, yes I remember casting, so it works for objects/actors too? And all my other classes start with a capital letter, for some reason this one doesn't :D.
Builderboy2005 Builderboy2005

2011/12/10

#
Casting does indeed work for actors, as well as most classes. And that's good to hear about your class name :P
You need to login to post a reply.