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

2014/10/25

#
GreenfootStars, were you in the classroom when mary made bricks transparent? If so just do the same here....
Super_Hippo Super_Hippo

2014/10/25

#
    private void activatePlatform()
    {
        for (Object obj : getWorld().getObjects(GreenTrans.class))
        {
            getWorld().addObject(new GreenPlatform(), obj.getX(), obj.getY());
            getWorld().removeObject(obj);
        }
        getWorld().removeObject(this);
    }
GreenfootStars GreenfootStars

2014/10/25

#
elupslin wrote...
GreenfootStars, were you in the classroom when mary made bricks transparent? If so just do the same here....
No :/ Was it recorded?
Super_Hippo wrote...
    private void activatePlatform()
    {
        for (Object obj : getWorld().getObjects(GreenTrans.class))
        {
            getWorld().addObject(new GreenPlatform(), obj.getX(), obj.getY());
            getWorld().removeObject(obj);
        }
        getWorld().removeObject(this);
    }
This doesn't compile for me.. * cannot find symbol - method getX() * cannot find symbol - method getY() * method removeObject in class greenfoot.World cannot be applied to given types; required: greenfoot.Actor; found: java.lang.Object;
Super_Hippo Super_Hippo

2014/10/25

#
Yeah, right, sorry... Try to change 'Object' in line 3 to 'Actor'. If this doesn't work, insert the following line before line 5 instead:
GreenTrans gt = (GreenTrans) obj;
//Change all 'obj' in lines 5 and 6 to 'gt'
GreenfootStars GreenfootStars

2014/10/25

#
Super_Hippo wrote...
Try to change 'Object' in line 3 to 'Actor'.
Gives "incompatible types" and highlights (GreenTrans.class) on line 3.
Super_Hippo wrote...
GreenTrans gt = (GreenTrans) obj;
//Change all 'obj' in lines 5 and 6 to 'gt'
Complies but gives an error every time "private void activatePlatform()" runs.
java.lang.NullPointerException
	at GreenTrans.activatePlatform(GreenTrans.java:28)
	at GreenTrans.act(GreenTrans.java:16)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
GreenTrans code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class GreenTrans here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GreenTrans extends TransparentPlatform
{
    public void act() 
    {
        if(((Girl)getWorld().getObjects(Girl.class).get(0)).isTouchingGreenBlob())
        {
            activatePlatform();
        }
    }    

    /**
     * If Girl is touching a green blob, replace all green transparent blocks with green platforms.
     */
    private void activatePlatform()
    {  
        for (Object obj : getWorld().getObjects(GreenTrans.class))
        {  
            GreenTrans gt = (GreenTrans) obj;
            getWorld().addObject(new GreenPlatform(), gt.getX(), gt.getY());  
            getWorld().removeObject(gt);  
        }  
        getWorld().removeObject(this);  
    }
}
Super_Hippo Super_Hippo

2014/10/25

#
Oh, this code is in the GreenTrans class... This should be enough then.
getWorld().addObject(new GreenPlatform(), getX(), getY());
getWorld().removeObject(this);
GreenfootStars GreenfootStars

2014/10/25

#
Haha that was easy, thanks a lot! If you have time, do you mind helping with this? \/ Say I have two classes, GreenTrans and BlueTrans, that both act the same. How do I add this type of code into a superclass? Like a method that replaces all Transparent Blocks of a certain color with Normal Blocks of the same color?
Super_Hippo Super_Hippo

2014/10/25

#
If they act the same, I would not have them separated into two classes. Instead, use one class and save the color in a field. Then you can access this field when replacing them. Maybe you could also think about having another field which saves the current state of being normal or transparent. Then you would only need to change the field instead of creating and removing blocks.
GreenfootStars GreenfootStars

2014/10/25

#
Basically this is what I have to do I've managed to do as you've said regarding colors of Platforms and Smokeclouds. I'm not sure how to handle the transparency though.. do I save it in a string or int?
Super_Hippo Super_Hippo

2014/10/25

#
In generally, I use ints for almost everything because they are easy to handle. As it only has two possible states, you can also use a Boolean for this. In your moving code, when checking for a block, you have to check if this block is transparent then instead of checking for a non-transparent block class.
GreenfootStars GreenfootStars

2014/10/25

#
Been trying and trying, but I can't figure it out.. not easy when you're new to programming :( If you have a little time, do you think you could email me? allish__@hotmail.com thanks
danpost danpost

2014/10/25

#
I would start by creating one Platform class to create all the platforms. The only state (states are stored in fields)that is not already being tracked (via the Actor class) is the color of the platforms (this is true for the blobs also). The platforms also have a behavior (behaviors are given by way of methods)-- that of changing their image transparencies. One method can be used for that with an int parameter to indicate either the exact transparency value to set or a boolean parameter to indicate opaque or translucent (programmer preference). At this point, we have a Platform class that creates platforms with a color field and a method to change the transparency of its image. That is all that is needed for the Platform class. When the girl intersects a blob, we have a world-changing event -- all platforms of the same color as the blob should turn opaque and any other platform that can be translucent should turn translucent. The reason I called it a world-changing event is because this change should be done by way of a method in the subclass of World (the girl should call this method in the subclass of World when a blob is detected, passing either the color of the blob or a reference to the blob object itself). The method in the subclass of world should iterate through all the platforms and adjust the transparencies of their images as needed. The girl should only be allowed to stand on a platform if its image is opaque.
GreenfootStars GreenfootStars

2014/10/25

#
This is my Platform class so far.. I'm guessing it's wrong since transparency is set in the constructor, eeeh..
public class Platform extends Actor
{   
    private String _color;              // color of Platform
    private boolean isTransparent;
    
    /**
     * Constructs a Platform.
     */
    public Platform(String color, boolean isTransparent)
    {
        _color = color;
        setImage(new GreenfootImage("platform_" + color + ".png")); 
        
        if(isTransparent == true)
        {
            getImage().setTransparency(100);
        }
    }
}
danpost danpost

2014/10/25

#
GreenfootStars wrote...
This is my Platform class so far.. I'm guessing it's wrong since transparency is set in the constructor, eeeh.. < Code omitted >
I do not think you will get any errors with that; however, 'isTransparent' would certainly be 'false' when the platform is constructed and the platform would stay opaque. You should not need an 'isTransparent' field as you can determine whether an image is opaque by the condition 'getImage().getTransparency() == 255'. This is all that is necessary in the Platform class:
import greenfoot.*;

public class Platform extends Actor
{
    private String _color;

    public Platform(String color)
    {
        _color = color;
        setImage(new GreenfootImage("platform_"+color+".png"));
    }

    public String getColor()
    {
        return _color;
    }
}
This is actually less than I stated was needed before. The method to adjust the transparency is not needed as there already is a method provided by the Actor class to get the image and one in the GreenfootImage class to change the transparency. The Blob class can be also be reduced to similar code. Everything else is done outside these classes (the girl detects the blobs, or the blobs detect the girl, passes color to world method to adjust the image transparencies of the platforms in the world; also, the world method should be called from the world constructor to initialize the platforms).
GreenfootStars GreenfootStars

2014/10/25

#
I do not think you will get any errors with that; however, 'isTransparent' would certainly be 'false' when the platform is constructed and the platform would stay opaque.
Well this is my World subclass, where I shove in color-values to platforms & activators (blobs) and true/false for the transparency. I'll remove the "true/false" values, just thought you'd have a look at it first (at how unexperienced I am!).
public class Level extends World
{
    public Level()
    {         
        super(800, 600, 1); 
        populateWorld();
    }
    
    private void populateWorld()
    {
        //Create the ground
        for(int i = 0; i < 16; i ++)
        {
            addObject(new Platform("red", false), 25 + i * 50, 575);
        }   
        
        //Add a Girl that can move around in the world
        addObject(new Girl(), 630, 400);
        
        //Add some platforms that can be jumped onto
        addObject(new Platform("green", true), 150, 475);
        addObject(new Platform("green", true), 200, 475);
         addObject(new Platform("blue", true), 350, 375);
         addObject(new Platform("blue", true), 400, 375);
        
        addObject(new Activator("blue"), 700, 525);             // blue blob
        addObject(new Activator("green"), 150, 425);            // green blob
        
        addObject(new Door(), 25, 525);
               
        setPaintOrder(Girl.class);
    }
}
So: 1. Girl detecs blob of color blue (method in Girl class?). 2. Passes color to world method to adjust the image transparencies of the platforms (how to pass the color to the world?). And thank you so much for being so incredibly helpful and patient! I'm really not good at this (yet), sorry!
There are more replies on the next page.
1
2
3
4
5