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
Nubbit Nubbit

2014/8/3

#
Hi again community, i've got another noob-question for you This time i'm trying to pass a boolean value from a superclass to one of its subclasses, through tests with Greenfoot.stop(); I've concluded that the program runs the boolean in the superclass and returns true, but when i try to fetch it in my subclass it simply doesn't work (either the boolean isn't true anymore or something of that nature, because the program doesn't start the if-statement that i want it to.) Here's some of my code :
public boolean checkTouchedBlob() //this is from the Superclass 
    {
        
        boolean isActive = false;
        
        if(isTouching(GreenBlobb.class))
        {   
           
            isActive = true;
            
        }
        if(isTouching(BlueBlobb.class))
        {
            isActive = true;
        }
            
        return isActive;       
    }

The code posted above is in my superclass and i've set it in a superclass because the player is supposed to run in to a blob which makes other objects physical. So when the player touches the blob the method is supposed to return true which would activate a series of other methods in it's subclasses such as the following code :
 @Override
    public void act() //this is the subclass :)
    {
      
      
      if(checkTouchedBlob()==true) 
      // i've also tested: if(checkTouchedBlob()) (i don't know if it's a difference between the two)
      {
          activateBrick();         
      }
    }   

    public void activateBrick()
    {
        Platform platform = new GreenPlatform();
        getImage().setTransparency(255);
        getWorld().getObjects(GreenBrick.class);
        getWorld().addObject(platform, getX(), getY());
        getWorld().removeObject(this);
        
      
    }
So my question is hopefully obvious ; what am I doing wrong? do I have to create some kind of reference to my superclass to be able to fetch the result from the boolean or .. ? If this is what I have to do, whould someone be kind enough to supply me with atleast a hint of what it should look like, so far I've only seen references to the world class and I haven't really figured out what a non-world reference should look like :p Why does the code compile if the above is true, the code obviously recognizes the method and so on.. ? Thanks in advance <3 Nubbit
NikZ NikZ

2014/8/3

#
So the Boolean in the super class should be created like this:
protected boolean x;
NikZ NikZ

2014/8/3

#
Protected means it can be shared with its subclasses.
davmac davmac

2014/8/3

#
Looking at what you've said, it doesn't seem to me that you've done anything wrong. Maybe post the complete code.
danpost danpost

2014/8/3

#
The 'activateBrick' method is very confusing: * line 15 creates a new green platform that line 18 adds into the world at the location of the player * line 16 gets the transparency value of the image of the player which is immediately lost * line 17 gets a list of all green brick objects which is immediately lost * line 19 removes the player from the world How does any of this do anything to activate any bricks? Something else that seems amiss -- you are indicating that you have a superclass between the player and the Actor class that the boolean method 'checkTouchedBlobb()' is in. I wonder why you would even do that (if that is indeed what you did).
Nubbit Nubbit

2014/8/4

#
danpost, you've missunderstood. The subclass is the greenbrick itself. Line 15 creates new greenplatforms where it previously was greenbricks. Line 16 sets the transparency of these platforms to 255 Line 17 gets a list of the old bricks that I want to get rid of Line 19 removes these bricks (?) atleast thats what the code does and what I want it to do. :) i have the following classes : Mover (the superclass that we've been discussing here) Platform (underclass to mover, I'm a complete noob when it comes to code, but my reasoning is that this makes it easier to call methods between the two classes and thats why i've set it as an underclass even though its not supposed to move) GreenPlatform (underclass to platform) BluePlatform (same) BlueBrick (Underclass to mover, the reasoning being the same as above) GreenBrick(same) Blobb (underclass to mover, once again the same reasoning) BlueBlobb (underclass to blobb) GreenBlobb (same) Girl (underclass to mover, and the character controlled by the player) The boolean that im trying to pass is from superclass Mover to subclass GreenBrick. I'll try to post more of the code later :)
danpost danpost

2014/8/4

#
First, having the Platform class extend the Mover class does not make it any easier to acquire any of the field values belonging to other instances of the class. Please note how I phrased that -- the fields do not belong to the class itself; they are created by the class for each instance created from the class (please refer to the trail onClasses and Objects of the Java tutorials. The Classes section has a page on Declaring Member Variables and the More on Classes section has one on Understanding Class Members; both of which you should look at. Next, if this code is in the GreenBrick class, then what your act method is doing when getting 'checkTouchedBlob' is finding out if this greenbrick object is touching a blue or green blob (the Girl object is not involved in any of this process).
sengst sengst

2014/8/4

#
danpost is right. the code will not do anything like what you think it will. i dont see any problems with the boolean method call, however the code after doesn't make sense. the code does/will do what danpost said: * line 15 creates a new green platform that line 18 adds into the world at the location of the player * line 16 gets the transparency value of the image of the player which is immediately lost * line 17 gets a list of all green brick objects which is immediately lost * line 19 removes the player from the world to do what you want it to, you would have to make a List with the greenplatforms. so: List<GreenPlatform> platforms = getWorld().getObjects( GreenPlatform.class ); for ( GreenPlatforms Gp: platforms ) { Gp.getImage().setTransparency( 255 ); } getWorld().addObject(platform, getX(), getY()); List<GreenBricks> bricks = getWorld().getObjects( bricks.class ); for ( GreenBlocks Gb: bricks ) { if ( Gb != this ) getWorld().removeObject( Gb ); } getWorld().removeObject( this ); So this gets all GreenPlatforms and sets their transparency to 255, then gets all of the GreenBricks and removes them. From what i understood, this is what you wanted. hope this helped! : )
danpost danpost

2014/8/4

#
sengst wrote...
i dont see any problems with the boolean method call
Except that the method should be in the Girl class, since the Girl object is the only one that is of interest in touching a blob. Nubbit has indicated that it is being called from the greenBrick class (which is the reason it returns 'false' when 'true' was expected). @Nubbit, let me know if I am mistaken: It appears that what is wanted is for all greenBricks to be removed and new greenPlatforms are to replace them (and I would presume the reverse when a different blob is encountered, coded in the Platform classes).
Nubbit Nubbit

2014/8/5

#
@danpost that's absolutely correct! The reason to why i dont have checkTouchedBlob in girlclass is (as i said earlier) that i was under the impression that it would be easier to communicate the answere to other classes if it was in their shared superclass, originally checkTouchedBlob was in the Girl class, and from what I get of this conversation, I should get it back in there. :P As it is now, checkTouchedBlob is in the superclass Mover, and is only called from the Girl class, but I take it that its better to put it back in to the girl class? Thanks for all the answers guys, much appriciated, keep it going ! :)
danpost danpost

2014/8/6

#
To get the Boolean value you want from the brick class, you will need to use a line like this:
if (((Girl)getWorld().getObjects(Girl.class).get(0)).checkTouchedBlob())
This can be broke down as follows
World world;
world = getWorld(); // get world
java.util.List<Girl> girls;
girls = world.getObjects(Girl.class); // get list of girls in world
Girl girl;
girl = girls.get(0); // get first world girl listed
boolean blobTouched;
blobTouched = girl.checkTouchedBlob(); // get state of girl touching blob
if (blobTouched) ...
Nubbit Nubbit

2014/8/6

#
@danpost It works perfectly! Thanks so much for all your help, this last piece of code you posted also helped me to solve a whole lot of other problems (deactivating the bricks for example), Thank you very much! <3
GreenfootStars GreenfootStars

2014/10/24

#
Nubbit wrote...
@danpost It works perfectly! Thanks so much for all your help, this last piece of code you posted also helped me to solve a whole lot of other problems (deactivating the bricks for example), Thank you very much! <3
Hey Nubbit! Did you manage to finish this project? I'm working on it aswell but am not as succesful :(
GreenfootStars GreenfootStars

2014/10/24

#
sengst wrote...
to do what you want it to, you would have to make a List with the greenplatforms. so: List<GreenPlatform> platforms = getWorld().getObjects( GreenPlatform.class ); for ( GreenPlatforms Gp: platforms ) { Gp.getImage().setTransparency( 255 ); } getWorld().addObject(platform, getX(), getY()); List<GreenBricks> bricks = getWorld().getObjects( bricks.class ); for ( GreenBlocks Gb: bricks ) { if ( Gb != this ) getWorld().removeObject( Gb ); } getWorld().removeObject( this ); So this gets all GreenPlatforms and sets their transparency to 255, then gets all of the GreenBricks and removes them. From what i understood, this is what you wanted. hope this helped! : )
This is a bit difficult to grip... What if I want to replace all green bricks with green platforms when touching a green blob? Basically: if (touching green blob = true) { add Green Platforms at location of all Green Bricks; remove all Green Bricks; }
GreenfootStars GreenfootStars

2014/10/24

#
Okay so I just did this which works, but honestly I barely know what I'm doing.
private void activatePlatform()
    {
        List<GreenTrans> gtrans = getWorld().getObjects(GreenTrans.class);
        for(int i = 0; i < 1; i++)
        {
            getWorld().addObject(new GreenPlatform(), getX(), getY());
        }
        getWorld().removeObject(this);
    }
There are more replies on the next page.
1
2
3
4