Help with a simple follow method

Monkey Forums/Monkey Programming/Help with a simple follow method

DeadFall(Posted 2012) [#1]
How do I make it so I can get the player's x and y
from the main class to be used in a different class.

Right now I'm trying to make it so the enemies in my
game follow the player. But what ever I've tried I
will ether get an error or the enemy wont move.

So any help would be appreciated.

-DeadFall


Origaming(Posted 2012) [#2]
Global ?


Shinkiro1(Posted 2012) [#3]
You can make it global or you use a registry:

[monkeycode]
Class Registry
Global player:Player
Global CurrentLevel:Level
End
[/monkeycode]

This way you can easily keep track of globals and exactly know when you are using one.
Usage would be like this:
[monkeycode]
If enemy.x < Registry.player.x
enemy.x += 1
End
[/monkeycode]


therevills(Posted 2012) [#4]
I normally set stuff like that up within a screen:

[monkeycode]Class GameScreen Extends Screen
Field player:Player
Field level:Level

Method Update:Void()
player.Update()
level.Update()
End
End

Class Player
Field x#, y#

Method CheckDeath:Bool()
If y > gameScreen.level.GetMaxY()
Return True
End
Return False
End
End
[/monkeycode]


Shinkiro1(Posted 2012) [#5]
@therevills
Instead of ..
[monkeycode]
Method CheckDeath:Bool()
If y > gameScreen.level.GetMaxY()
Return True
End
Return False
End
[/monkeycode]

you can simply write ..
[monkeycode]
Method CheckDeath:Bool()
Return (y > gameScreen.level.GetMaxY())
End
[/monkeycode]
which is shorter and clearer imo. I just noticed a lot of this in diddy source code.

It's no big deal however, just wanted to let you know.


Samah(Posted 2012) [#6]
Shinkiro: that'd be therevills's code, because I always do it the way you described. Actually, I wouldn't have even put in those parentheses either :)


Gerry Quinn(Posted 2012) [#7]
You have to give the enemies access to the player's position, either by making it global or by having each enemy contain a player object, or an object that knows where the player is.

For example, if the there is a GameMap object that knows where everyone is, you could either make it global or else give each enemy a GameMap object pointing to the simgle gameMap instance. It will probably have lots of other useful information too, such as the position of obstacles or other enemies.


DeadFall(Posted 2012) [#8]
Hey guys thanks for the responses! I actually had tried
making player:Player a global but the first time I did
do it I left it in the main class so it wouldn't work
properly. But thanks to you guys I realized my mistake and
got it all fixed and works properly.


therevills(Posted 2012) [#9]
Instead of ..
you can simply write ..


IMO Swings and Roundabouts ;)


Samah(Posted 2012) [#10]
IMO Swings and Roundabouts ;)

Noooooooooo Swing............ D: