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

2015/10/2

Help on my Ammo Counter~

1
2
danpost danpost

2015/10/3

#
Erio wrote...
well, here is my code for wall collision: < Code Omitted >
There are two things here. One is that turning by only 3 degrees at a time will make your actor hug the edge of the wall until it can safely move along the edge of the wall (turn by a larger value, like 180). The other is that turning can also cause a collision; so, you may have to move a bit more away from the wall after turning.
Erio Erio

2015/10/3

#
I tried changing up my code a little bit but they keep on turning back & forth against the wall due to my followHuman method.
public void checkCollision()
 {

     if (getOneIntersectingObject(Wall.class) != null)
    {
    
    turn(180);
    move(3);
    
    }
    else 
    {
        followHuman();
    }
}
davmac davmac

2015/10/3

#
I tried making my Zombie to move and turn when they collide with a wall, but they seem to be confused because I have a follow method where they turn towards the Human and move towards it. Can you make me a code that if there is collision they move and turn else they go back to following human? I'm not really sure on how to make a code for that.
If I understand you correctly, what you need is a counter (an instance variable of type int) which is normally 0, but is set to some value when the zombie hits a wall. When it's non-zero, you should not call followHuman() but instead just decrement the counter. Something like:
public void checkCollision()
{
    if (getOneIntersectingObject(Wall.class) != null)
    {
        turn(180);
        move(3);
        wallCounter = 15;
    }
    else
    {
        if (wallCounter == 0) {
            followHuman();
        }
        else {
            move(3);
        }
    }
}
I'd also suggest that:
  • a zombie should turn a random amount between 90 and 270 degrees when hit a wall, instead of a fixed 180
  • movement doesn't belong in a method called 'checkCollision' - either rename the method or put the movement and following logic elsewhere
Finally, please fix your code indentation, at least before posting it here. It's as easy as pressing ctrl+shift+I.
Erio Erio

2015/10/3

#
I'm not really sure if I did this right but here are my codes and can you pls help me fix my code: Wall detection and Collision():
public void detectWall()
    {
        
        Game game = (Game)getWorld();
    WallCounter wallCounter = game.getWallCounter();
    if (getOneIntersectingObject(Wall.class) != null)
            {
                wallCounter.setWall(15);
            }
            else
            {
                wallCounter.setWall(0);
            }
        }

    
public void Collision()
{
    Game game = (Game)getWorld();
    WallCounter wallCounter = game.getWallCounter();
    if (wallCounter.getWall() == 15)
    {
        turn(180);
        move(3);
        
    }
    else
    {
        if (wallCounter.getWall() == 0) 
        {
            followHuman();
        }
        else {
            move(3);
        }
    }
}
WallCounter class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class WallCounter extends Actor
{
   
   int wall = 0;
    
    public WallCounter()
    {
       
    }

    /**
     * Create a new counter, initialised to 0.
     */
   
    
   
    
    
    public void act() 
    {
        
        
        
    }

 
    public void add(int value)
    {
        wall+=value;
    }
   
    public void subtract()
    {
        wall--;
    }


   
    public int getWall()
    {
        return wall;
    }

    public void setWall(int newWall)
    {
       
        wall = newWall;
        
    }
    
   
   
}
davmac davmac

2015/10/3

#
What on earth is this??
    WallCounter wallCounter = game.getWallCounter();
I said:
davmac wrote...
what you need is a counter (an instance variable of type int)
You don't need any "WallCounter" class. You need an int instance variable in your Zombie class. I.e.
    private int wallCounter = 0;
Erio Erio

2015/10/4

#
well, I give up on that.. I just need a code for finding the best path when they hit a wall, so that they won't turn back and forth due to my followHuman method.
davmac davmac

2015/10/4

#
I gave you that before, although I missed one thing. Here it is again:
public void checkCollision()
{
    if (getOneIntersectingObject(Wall.class) != null)
    {
        turn(180);
        move(3);
        wallCounter = 15;
    }
    else
    {
        if (wallCounter == 0) {
            followHuman();
        }
        else {
            move(3);
            wallCounter--;  // I missed this last time
        }
    }
}
Erio Erio

2015/10/4

#
thanks! lastly, how do i use getRandomNumber to make a range from 90 to 270?
davmac davmac

2015/10/4

#
Erio wrote...
how do i use getRandomNumber to make a range from 90 to 270?
Through the power of mathematics :) "Greenfoot.getRandomNumber(x)" gives you a number between 0 and x. If you want a number between A and B, you can first get a number between 0 and (B-A) and then add A. In this case:
Greenfoot.getRandomNumber(180) + 90
(The 180 is from 270-90).
Erio Erio

2015/10/4

#
oh, I wasn't really sure on how to use the getRandomNumber at first. Thanks for explaining! :) I'm almost done with my game. Thanks guys :)
You need to login to post a reply.
1
2