Help with a simple follow method
Monkey Forums/Monkey Programming/Help with a simple follow method
| ||
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 |
| ||
Global ? |
| ||
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] |
| ||
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] |
| ||
@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. |
| ||
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 :) |
| ||
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. |
| ||
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. |
| ||
Instead of .. you can simply write .. IMO Swings and Roundabouts ;) |
| ||
IMO Swings and Roundabouts ;) Noooooooooo Swing............ D: |