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

2013/1/25

Some code

moobe moobe

2013/1/25

#
Hi, I've added new types of dragons in my game There are three different classes called "Drache", "Drache2", and "Drache3". The interesting thing is that when Drache3 gets killed, Drache2 appears. When Drache2 gets killed, Drache appears. When Drache gets killed, it's dead :) Creating the dragons is very easy, but my source code is very bad. Could you guys have a look on it, please and tell me, how to clean it up. I've tried it in different ways, but it doesn't work as good as I want, because I need the references for each classes. Here's the code of "Schnee", which means snow in English. It can kill a dragon and disappears after it has done its job. "Feuerball" is an other class which can be killed by snow as well.
 /**
     * Act - do whatever the Schnee wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        fall();
        if(getWorld() != null) checkDracheHitten();
        if(getWorld() != null) checkFeuerballHitten();
        if(getWorld() != null) checkDrache2();
        if(getWorld() != null) checkDrache3();
    }   


    public Schnee()
    {
        setYGeschwindigkeit(-WURFKRAFT);
    }


    public void checkSelbstzerstoerung()
    {
        if (groundHitten() || amRand()) 
        {
            getWorld().removeObject(this); 
        }
    }
    

    public void checkDracheHitten()
    {
        if (dracheHitten())
        {
            getWorld().removeObject(drache);
            getWorld().removeObject(this);
        }
    }
    

    public void checkFeuerballHitten()
    {
        if (feuerballHitten())
        {
            getWorld().removeObject(feuerball);
            getWorld().removeObject(this);
        }
    }
    

    public void checkDrache2()
    {
        Drache2 drache2 = (Drache2) drache2Hitten();  
        if(drache2 != null)  
        {
            getWorld().removeObject(this);
            drache2.createDrache();  
        }            
    }
    

    private Object drache2Hitten()  
    {  
        return getOneIntersectingObject(Drache2.class);  
    }
    

    public void checkDrache3()
    {
        Drache3 drache3 = (Drache3) drache3Hitten();  
        if(drache3 != null)  
        {
            getWorld().removeObject(this);
            drache3.createDrache2();  
        }            
    }
    

    private Object drache3Hitten()  
    {  
        return getOneIntersectingObject(Drache3.class);  
    }


    public boolean groundHitten()
    {
        ground = getOneIntersectingObject(Eisboden.class);
        if (ground != null) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public boolean dracheHitten()
    {  
        drache = getOneIntersectingObject(Drache.class);
        if(drache != null) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean feuerballHitten()
    {  
        feuerball = getOneIntersectingObject(Feuerball.class);
        if(feuerball != null) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
danpost danpost

2013/1/25

#
Do each of the dragon classes have different images or the same? I was thinking (even if they are different) that it would be much easier to have just one dragon class instead of three. Just add a countdown counter in the single dragon class starting at 3. Each time the dragon is hit by a Schnee object, decrease the counter (if zero after decreased, remove dragon) and change its image.
moobe moobe

2013/1/25

#
Ok, fixed it now, thank you very much for your help!
You need to login to post a reply.