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

2014/3/31

Some problems with my game

Alex89 Alex89

2014/3/31

#
Hi everyone. I have 3 little problems with my game I can't find a solution for. I bet someone can help me out. :) #1 I found another discussion on how to create obstacles: http://www.greenfoot.org/topics/2746 danpost's code works well but I can't find out how to make two different classes become obstacles. If I copy the code and change the class name in it I can move through both objects. This is how the code looks like:
public void setLocGuardianShield(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(GuardianShield.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }
    
    public void setLocWall(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Wall.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }
#2 and 3 The player can can create a hallucination that stuns all enemies on impact which makes them unable to move for 10 seconds. This works well so far but the counter doesn't work. Enemies will not move ever again as soon as they get stunned. In the same class there is an option to shield the player which makes him/her invulnerable. This should prevent that enemies can't kill the player for 10 seconds but this option doesn't work at all.
//Enemy class
public class Enemy extends Creatures
{
    int enemymovement=1; //To toggle the movement for enemies. 1=activated; 0=deactivated.
    int shield=0; //To toggle the shield for the Sentry. 1=activated; 0=deactivated.

    public void act()
    {
        Stuncount();
        Shield();
    }


   //Code you don't need to care about.

  
    public void Shield() //If the f-key is pressed the Guardian Shield gets activated and the Sentry becomes invicible.
    {
        String key = Greenfoot.getKey();
        
        if("s".equals(key))
        {
            shield = 1;
            Shieldcount();
        }
    }
    
    public void Shieldcount()
    {
        //Enemies can't kill the Sentry for 10 seconds when the Guardian Shield is activated.
        int shieldcounter=250;
        
        if(shieldcounter == 0)
        {
            shield=0;
        }
        else if (shieldcounter>0)
        {
            shieldcounter--;
        }
    }
    
    public void Stuncount()
    {
        //Enemies can move again after 10 seconds when touched by a Hallucination.
        int stuncounter=250;
                
        if(stuncounter == 0)
        {
            enemymovement=1;
        }
        else if (stuncounter>0)
        {
            stuncounter--;
        }
    }
}
//Zergling (an enemy) class
public class Zergling extends Enemy
{
    public void act() 
    {
        movement();
        eatFood();
    }    

    public void eatFood() //The Sentry dies at a touch with the enemy excepet when the Guardian Shield is activated..
    {
        if (canSee(Sentry.class) && shield==0)
        {
            eat (Sentry.class);
            Greenfoot.stop();
        }
        else if (canSee(Hallucination.class)) //Stuns all enemies at a touch with a Hallucination.
        {
            eat(Hallucination.class);
            enemymovement=0;
        }
    }

  //Code you don't need to care about.
}
I hope anyone of you can help me. Thanks in advance. Greetings Alex
Super_Hippo Super_Hippo

2014/3/31

#
In the Shieldcount() and Stuncount() method, you always set the ints to 250 again. Instead, have the int as a private int and only change its value to 250 if it gets activated. The ints enemymovement and shield probably should be private/public static.
danpost danpost

2014/3/31

#
For problem #1: You could try the follows:
public void setLoc(int x, int y)
{
    int oldX = getX();
    int oldY = getY();
    super.setLocation(x, y);
    if(!getIntersectingObjects(GuardianShield.class).isEmpty() || !getIntersectingObjects(Wall.class)).isEmpty())
    {
        super.setLocation(oldX, oldY);
    }
}
Alex89 Alex89

2014/4/1

#
Thanks for the answers. @Super_Hippo If I put the enemymovement and shield to private the Zergling sub class has no access to those variables. @danpost Sadly this didn't change anything but thanks for the try.
danpost danpost

2014/4/1

#
Alex89 wrote...
@danpost Sadly this didn't change anything but thanks for the try.
Please show the entire class code of the actor that is not acknowledging obstacles.
Super_Hippo Super_Hippo

2014/4/1

#
Alex89 wrote...
If I put the enemymovement and shield to private the Zergling sub class has no access to those variables.
In that case, use 'public' instead of 'private'!
You need to login to post a reply.