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

2019/1/11

Incompatible types for boomWorld = getWorld();

Notted Notted

2019/1/11

#
Ok, so I'm trying to remove a bomb within an area defined by a class. That being bombArea. While the bombs work, there seems to be an incompatible types error with boomWorld = getWorld();. I'm not sure how to fix this.
private void bombBoom() 
    {
        Actor bomb;
        bomb = getOneObjectAtOffset(0, 0, Bomb.class);
        if (bomb != null)
        {
           MyWorld boomWorld;
           boomWorld = getWorld();
           boomWorld.removeObject(bomb);
        }
    }
The_Beast0007 The_Beast0007

2019/1/11

#
Notted wrote...
Ok, so I'm trying to remove a bomb within an area defined by a class. That being bombArea. While the bombs work, there seems to be an incompatible types error with boomWorld = getWorld();. I'm not sure how to fix this.
It is probably that when you declared your variable (boomWorld) you set it as the wrong variable identifier. Try removing the line boomWorld = getWorld(); and instead of boomWorld.removeObject(bomb); put getWorld().removeObject(bomb);. That's what I usually use. If it doesn't work then it's something outside the code you gave.
danpost danpost

2019/1/11

#
The getWorld method returns a World typed object -- not a MyWorld typed object (although the World object may be a MyWorld object). You cannot store a World object in a variable that is declared to hold a MyWorld object without first declaring that the World typed object is indeed a MyWorld object. Using what you have as an example:
MyWorld boomWorld; // declares a variable that is to hold a MyWorld object (specifically)
// then
boomWorld = getWorld(); // fails, as a World typed object cannot be assigned to a MyWorld variable
// however
boomWorld = (MyWorld)getWorld(); // works, as the World object is declared to be a MyWorld object
However, unless you need access to a member of the MyWorld class (which you do not, in this case), there is no need to have the world typed as a MyWorld object. The following is sufficient (to replace lines 7 through 9 in your code above):
getWorld().removeObject(bomb);
Notted Notted

2019/1/14

#
Thanks, guys.
You need to login to post a reply.