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

2013/1/18

Destructor

1
2
moobe moobe

2013/1/19

#
I have published the source code of my game. You can have a look at it ;)
moobe moobe

2013/1/19

#
@danpost: Of course my Snowman knows in which level he is. The variable in my world is not the same "level" variable, but that's why I write
lev = level;
In the constructor of my levels. I know that it isn't nice to read, but the reanimate method should work. I know that it isn't written in a sophisticated way, but it should work. So a soulution could be to give the level variable to the fireball and then let him get the reference of snowman. This could be something like
if (level == 1)
{
Level1 level1 = (Level1) getWorld();
level1.reanimateSnowman();
}
else if (level == 2)
{
.
.
.


But as I said is this a stupid way of programming, cause I have to write the method into all worlds then -.- That's why I'd like to have that destructor :)
moobe moobe

2013/1/19

#
Good news: The "destructor" works, but it's only executed when Snowman is being killed at the same method. Check out the new version of my game and fall into a hole. Then Snowman will be reanimated with only 2 lifes. It's because I've added something in the "checkDeath" method :
 public void checkDeath()
    {
        if (getY() == getWorld().getHeight() - 1) //This is the new part. it's written before it executes the destructor.
        {
            getWorld().removeObject(this);
        }
        if (getWorld() == null)         
        {
            leben--;
            reanimation();
        }        
    }
So there is still the problem with the reference. Fireball and dragon don't have the reference to execute reanimation. I'm so confused right now, because I know a few solutions, but they all probably won't work..
Gevater_Tod4711 Gevater_Tod4711

2013/1/19

#
Ok I have a suggestion for improvement. If you change your fall method like this:
    public void fall()
    {
        setLocation ( getX(), getY() + yGeschwindigkeit);
        yGeschwindigkeit = yGeschwindigkeit + ORTSFAKTOR; //darf nicht zu hoch werden!
        if (yGeschwindigkeit > 30) {
            yGeschwindigkeit = 30;
        }
    }
the yGeschwindigkeit will not be so high that the snowman moves trought the botom. (like you already said in your coment in this method) To your problem with the fireball: Well this should not realy be a problem. The method removeObject in world needs a reference to the actor you want to remove. So you already got a reference.
moobe moobe

2013/1/19

#
Gevater_Tod4711 wrote...
To your problem with the fireball: Well this should not realy be a problem. The method removeObject in world needs a reference to the actor you want to remove. So you already got a reference.
Thanks for your help, it works fine now ;) But is there a way to get reference to snowman without using the world? So that I could write
snowman.executeAnyMethodIWant;
The variable "snowman" is an actor, but to execute one of snowman's methods, snowman needs to be a variable from type Snowman. I could create a new variable from Type Snowman, but then I would get a "NullPointerException".
danpost danpost

2013/1/19

#
Modifications to your Feuerball class:
// remove line 12, which is
Actor snowman; // remove this (line 12)

// change 'killSnowman' method to
private void killSnowman()
{
    Snowman snowman=(Snowman)snowmanHitten();
    if(snowman != null)
    {
        getWorld().removeObject(snowman);
        snowman.checkDeath();
        getWorld().removeObject(this);
    }
}

// change the last line in your 'act' from
checkSelbstzerstoerung();
// to
if(getWorld() != null)checkSelbstzerstoerung();

// change 'snowmanHitten' method to
private Object snowmanHitten()
{
    return getOneIntersectingObject(Snowman.class);
}
moobe moobe

2013/1/19

#
OK, this is really fantastic now, I'm so happy :D Writing
if(getWorld() != null)
Is so much better than all those elses. I only have two questions now: What's a variable from type "Object" and why do you use private methods? Do they need more memory?
danpost danpost

2013/1/19

#
The Object class is the superclass of both the World and the Actor classes. All the 'get..Object..' methods return Object objects (that is, actors that are cast as Object type). In order to call a 'Snowman' method from the 'Feuerball' class, the snowman object must first be sub-casted to a 'Snowman' type object because as an Object type, the Snowman class will not be searched to find the method in question. Anyways, more changes follow:
// IN YOU SNOWMAN CLASS

// add the following method
public void addedToWorld(World world)
{
    createCounters();
    createHearts();
}

// remove those same lines from each 'if'block in 'checkNextLevel'

// change your 'Snowman' constructor to
public snowman()
{
    setImage("Schnee.png");
}
In all your world classes, remove any 'level' related data; which includes 1) removing the declared variable 'level' 2) changing your world constructors from 'LevelN(Snowman, int)' to 'LevelN(Snowman)' 3) changing all 'new Snowman(int)' to 'new Snowman()' 4) remove any other 'level' value related code Adjust any calls to any of these constructors accordingly (removing the unwanted paremeters).
Gevater_Tod4711 Gevater_Tod4711

2013/1/19

#
In Java the superclass of all classes is java.lang.Object. So if you have an object which type is Object it can be every object you want because every object is a subobject of this class. Private methods can only be called from the class where they are declared. So if you want to call them from the act method of the class they are in (or from any other mehtod in the class) it's possible but if you try to call the method like this:
Snowman snowman=(Snowman) snowmanHitten();  
snowman.anyPrivateMethod(); 
It's not possible. This way your classes are save from any other programmers (if you want to publish your classes). They don't see these methods.
moobe moobe

2013/1/19

#
Thanks for your posts, you guys are really pros! The level variable was senseless, I wanted to change that soon, but what do you mean with
// change your 'Snowman' constructor to  
public snowman()  
{  
    setImage("Schnee.png");  
}  
Isn't it the same way I've written my program? One more thing: I've written the addedToWorld method this way now:
if (level > 0)
{
createConters();
createHearts();
}
Otherwise I've got The counters and hearts in my menu :)
danpost danpost

2013/1/19

#
moobe wrote...
what do you mean with
// change your 'Snowman' constructor to  
public snowman()  
{  
    setImage("Schnee.png");  
}  
Isn't it the same way I've written my program?
You know, maybe I changed it, and then changed it back. Sorry.
You need to login to post a reply.
1
2