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

2014/8/3

Passing a boolean value to subclass

1
2
3
4
5
GreenfootStars GreenfootStars

2014/10/26

#
I meant the boolean isTransparent :p
public boolean isTransparent()
    {
        if(getImage().getTransparency() == 100) 
        {
            return true;
        }
        else {
            return false;
        }
    }
Is this wrong?
Super_Hippo Super_Hippo

2014/10/26

#
You can also do it like this, yes. :)
GreenfootStars GreenfootStars

2014/10/26

#
I can't compile, it gives me an error in Mover. "cannot find symbol - method isTransparent()"
Super_Hippo Super_Hippo

2014/10/26

#
How are you trying to use it?
GreenfootStars GreenfootStars

2014/10/26

#
Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);

        if(ground != null)
        { 
            if(ground.isTransparent())
            {
                moveOutOfGround(ground);
                onGround = true;       
            }
        }    
Super_Hippo Super_Hippo

2014/10/26

#
Oh yes, right. This should do it.
Platform ground = (Platform) getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
GreenfootStars GreenfootStars

2014/10/26

#
It does! Woooooooooo! Thanks a bunch :) Care to explain why that change makes it work?
Super_Hippo Super_Hippo

2014/10/26

#
'ground' was an 'Actor' object. There is no method called 'isTransparent' in 'Actor'. Now 'ground' is a Platform object and there is the method 'isTransparent'. Actually it was also a Platform object before because you are checking for objects of the Platform class. But without writing it explicit like this (saying 'ground' is a Platform object and the object which is returned by 'getOneObjectAtOffset' is casted to a Platform), it is only handled as an Actor.
danpost danpost

2014/10/26

#
Again, the method 'isTransparent' is not needed as the following would work:
Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
if (ground != null)
{ 
    if (ground.getImage().getTransparency() == 255)
    {
        moveOutOfGround(ground);
        onGround = true;       
    }
}
Since this uses the Actor class method 'getImage()' instead of the Platform class method 'isTransparent', the typecasting to the Platform class is not necessary and the 'isTransparent' method can be removed.
GreenfootStars GreenfootStars

2014/10/26

#
Super_Hippo wrote...
Explanation
That's good to know ha :) thanks
danpost wrote...
Since this uses the Actor class method 'getImage()' instead of the Platform class method 'isTransparent', the typecasting to the Platform class is not necessary and the 'isTransparent' method can be removed.
True. I tried both options out of curiosity, but believe that my supervisors prefer if a boolean is used.
danpost danpost

2014/10/26

#
GreenfootStars wrote...
True. I tried both options out of curiosity, but believe that my supervisors prefer if a boolean is used.
In that case, you should probably correct the logic:
if ( ! ground.isTransparent()) // with " ! "
You want your girl to stop on the ground only if the ground is NOT transparent. The snippet can be simplified to this:
Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2 , Platform.class);
if(ground != null && !((Platform)ground).isTransparent())
{ 
    moveOutOfGround(ground);
    onGround = true;       
}
elupslin elupslin

2014/10/26

#
Hello everybody, I am constructing the same game. In the end a text message is supposed to appear when girl is at end of level. I have tryed to set an image wich is an actor but the world has no idea what an image is... is it possible? or do I have to write a "greenfoot" message? This is in my world and it works fine with the levels. I have tryed to put some code before Greenfoot stop(); but nothing seems to work..Any suggestions?? /** * Go to next level. */ public void nextLevel() { String level = levelCreator.createLevel(levelNumber ++); if(level == null) { Greenfoot.stop(); } else { this.removeObjects(getObjects(null)); buildLevel(level); } }
Super_Hippo Super_Hippo

2014/10/26

#
What is 'String level = levelCreator.createLevel(levelNumber ++);' supposed to do? The string won't be 'null', so the code there won't be executed.
elupslin elupslin

2014/10/26

#
It takes actor to next level in my level cerator, and it works...but I will check
elupslin elupslin

2014/10/26

#
It is if there are no more levels the game will stop
There are more replies on the next page.
1
2
3
4
5