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

2015/2/1

Zombie problem

Corrupted Corrupted

2015/2/1

#
I'm reprogramming a game's mechanics and need help changing the enemy (zombies) target. In the original game, the enemies follow the mouse cursor. I made a few changes so that the mouse is no longer an object but is now used to aim. The biggest problem I'm facing so far though is that the zombies don't aim for the player but rather still aim for the mouse. I know it has to do with the co-ordinates but I don't know how to get the players current location. Sorry for the bother, I am new to this.
danpost danpost

2015/2/1

#
You will need to post the code for the Zombie class. Without it, how can we help fix it?!! Use the 'code' link below the reply box to insert code into you posts.
Corrupted Corrupted

2015/2/1

#
public class Zombie extends Actor { public int speed; public Zombie(int spd) { speed = spd; } public void act() { doStuff(); } private void doStuff() { if (Greenfoot.getMouseInfo() == null) return; int x = Greenfoot.getMouseInfo().getX(); int y = Greenfoot.getMouseInfo().getY(); turnTowards(x,y); move(speed); } } I already know it's because of Greenfoot.getMouseInfo that they follow the mouse
danpost danpost

2015/2/2

#
Corrupted wrote...
I already know it's because of Greenfoot.getMouseInfo that they follow the mouse
Well, yes. The variables 'x' and 'y' need to be set to the location of the player -- not the location of the mouse (as you previously stated). A few questions need answered before a definitive answer can be given in this regard. (1) do you have a field in your subclass of World that holds a reference to the player? (a) if yes, do you have a 'getter' method that returns that reference (give name of player field and 'getter' method, if exists) (2) will there always be exactly one player in the world (at all times) You may find, after getting the zombies to turn toward the player, that the zombies do not exactly move in the direction they turn toward when low speeds are given. That is, they move toward the player at less precise angles than you would expect. For an example, it might move at 45 degree angle until it aligns with the player vertically or horizontally and then move along the axis. To avoid this behavior, you will need to track the 'exact' coordinates of the zombie's location by using double values or using a magnification system. My Zombie Shooter Demo uses a magnification system for more precise turning and moving. The 'QActor' class which extends Actor is what provides the smooth moving and turning. You can run the scenario right on the site and press the 'q' key to view the class. You can also download it and make use of the reusability of the class by importing it into your own projects. As well, then you can view the Zombie class and the World subclass code to see how the zombies were made to follow the player.
Corrupted Corrupted

2015/2/2

#
To be completely honest I am not exactly sure how the programmer references the player other than how I changed it, being "Player". But the game works in a similar way to your Zombie Shooter Demo. Would it just help if I posted the Player codes? (It is kind of lengthy compared to the zombie's)
danpost danpost

2015/2/2

#
Supplying the Player class code would only give me details on the state and behavior of the Player instance(s). That is really of no concern in this issue. The issue is in the behavior of the Zombie instances and having them find where the player is at in the world. Hence, the code to your subclass of World would be something to look at.
danpost danpost

2015/2/2

#
Corrupted wrote...
I am not exactly sure how the programmer references the player other than how I changed it, being "Player".
"Player" is the name of a class, not a reference to an instance of that class. A class cannot have a location within the world; only instances created from the class can (provided it creates Actor-type objects).
Corrupted Corrupted

2015/2/2

#
Well that actually make a lot more sense I've been trying to think different ways on how to find the player coordinates, I tried finding them off the bullet programming thinking that if the bullets new where to spawn from I would be able to use those codes to give the zombies coordinates to turn to...
Ben&Sam Ben&Sam

2015/2/2

#
if u make the class public so it can be universally called it might work, just an idea
davmac davmac

2015/2/2

#
Corrupted, look at tutorial #6. It specifically addresses the question of How can I access the variables (or methods) of one object/class from another one? - in this case, you need the Zombies to call the player's getX() and getY() methods to find where to turn towards.
Corrupted Corrupted

2015/2/6

#
I've tried just about everything now, I think I may just have to try from the beginning or try something else...
davmac davmac

2015/2/6

#
Corrupted wrote...
I've tried just about everything now, I think I may just have to try from the beginning or try something else...
As I said, tutorial #6 tells you how to do what you want. If you're saying you tried that and it didn't work and that you would like some help, you would need to post the code that you tried. You must realise that we can't help you if we can't see what you've done.
Corrupted Corrupted

2015/2/19

#
I just published it, maybe this will help? I even made the published the source code, it's basically what I have done so far...
danpost danpost

2015/2/19

#
First problem I see is that you have three different lines in the wrld class that have 'new Player()' in them. Each time you use that, you create a new Player instance. So, you create one and assign it to the field 'ma'; then you create another and add that one to the world; then you take the counter that belongs to the one that is not in the world and add it into the world; finally, each time you use the method 'getPlayer' you get a NEW Player instance that was just created and had no chance of being added into the world, yet. Only use 'new Player()' once, assigning it to the field, then use the field name to refer to it from then on. Also, you need to move the field declaration line outside the constructor and apply a 'private' access modifier to it:
// either declare and assign outside constructor
private Player ma = new Player();
// or
// declare outside
private Player ma;
// and assign in the constructor
ma = new Player();
// then
// add to world in the constructor
addObject(ma, getWidth()/2, getHeight()/2);
// and
// return the value of the field in the get method
public Player getPlayer()
{
    return ma;
}
You need to login to post a reply.