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

2013/6/23

Problem with creating objects

Gevater_Tod4711 Gevater_Tod4711

2013/6/23

#
Hi guys, I got a problem with one of my classes in my Super Mario game. Specifically with constructing an instance of this object: When I try to create a new instance of this class by rightclicking on the class in the list of classes there is the problem that I can't create the object using a specific constructor: Switch(boolean, boolean). No mater what I tipe in it always says 'instance name has to be a java identifyer'. Because it's a boolean it should probably be 'true' or 'false' but both don't work. What seems realy strange to me is that when I use this constructor in my code it perfectly works. If I use a constructor for this class without any parameters (Switch()) it also works (although this constructor calles the not working constructor). So only if I try to create an instance of this one class using one of two specific constructors and create the instance manually (putting it into the world using the mouse) it doesn't work because 'true' or 'false' seem to be no java identifyers. This problem is not realy game relevant because I still can use the default constructor for this class but it would be very interesting why the hell this only occours in this one case. I realy don't get why this occours. May anyone else has got an idea. Here is the code of the class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Switch here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class Switch extends Object implements Activatable
{
    private int switchNumber;
    
    private boolean activated;
    private boolean disactivatable;
    private boolean foundHero;
    
    private LogicalOperator operator;
    
    public Switch() {
        this(false, true);
    }
    public Switch(boolean disactivatable) {
        this(false, disactivatable);
    }
    public Switch(boolean activated, boolean disactivatable) {
        this.activated = activated;
        this.disactivatable = disactivatable;
        updateImage();
    }
    
    public void addedToWorld(World world) {
        switchNumber = getWorld().getObjects(Switch.class).size();
    }
    
    public void run() {
        if (getOneIntersectingObject(Hero.class) != null) {
            if (!foundHero) {
                if (!activated) {
                    activated = true;
                }
                else {
                    if (disactivatable) {
                        activated = false;
                    }
                }
            }
            allertLogicalOperator();
            updateImage();
            foundHero = true;
        }
        else {
            foundHero = false;
        }
    }
    
    private void updateImage() {
        if (activated) {
            if (Settings.switchActivated != null) {
                setImage(Settings.switchActivated);
            }
        }
        else {
            if (Settings.switchDisactivated != null) {
                setImage(Settings.switchDisactivated);
            }
        }
    }
    
    public void allertLogicalOperator() {
        if (operator != null) {
            operator.allert(this);
        }
    }
    
    public String toString() {
        return "switch" + switchNumber;
    }
    
    public boolean isActivated() {
        return activated;
    }
    public void setActivated(boolean activated) {
        this.activated = activated;
        if (activated) {
            allertLogicalOperator();
        }
        if (Settings.activateSwitch != null) {
            GreenfootSound sound = new GreenfootSound(Settings.activateSwitch);
            sound.setVolume(Settings.soundVolume);
            sound.play();
        }
        updateImage();
    }
    
    public boolean isDisactivatable() {
        return disactivatable;
    }
    public void setDisactivatable(boolean disactivatable) {
        this.disactivatable = disactivatable;
    }
    
    public LogicalOperator getOperator() {
        return operator;
    }
    public void setLogicalOperator(LogicalOperator operator) {
        this.operator = operator;
    }
}
You need to login to post a reply.