Enemy Movement
Monkey Forums/Monkey Programming/Enemy Movement
| ||
Is it possible to get an enemy (character) uncontrolled to move back and fourth between a position, also would it be possible to have collisions working so that if a player landed ontop of an enemy the enemy died but if the player hit the enemy from the side then the player would die? |
| ||
Yes, yes and yes. :) |
| ||
yes the game draws images on the screen in X and Y co-ordinates just compare the Y position of the enemy and player when they collide If you are making a side scrolling platform game, then you would need to look up code that people have posted here, 'tilemap collision' might be something to look up. There is a bunch of helper code under the name 'diddy' which seems to do tile map collision |
| ||
How do i do it so that the enemy is constantly moving when the game starts, setting the enemy position to be equal to enemy position + enemyspeed isnt working |
| ||
That question is way too generic. Could you post some code which isn't working? What kind of game is it? Is it top down, is it a jump'n'run? |
| ||
All of the code for actually grabbing the player sprite is elsewhere but this is the code that draws the player to the screen, ex and ey are the coordinates and exspeed is the speed also declared as 1 further up in the program.Class enemy Field enemy1Sprite:Image Field ex:Int Field ey:Int Field exspeed:Int Method initialise() DrawImage enemy1Sprite, ex += exspeed , ey End End |
| ||
So when the level first starts i want the enemy to move to the side, im not worrying about the player moving back and fourth just that it moves to start with. Its just a fixed size level game where player has to get from bottom to top. |
| ||
Ah thanks for that. I don't know if that implicit thing work but I guess not. Better would probably be:ex += exspeed DrawImage enemy1Sprite, ex , ey Plus this code has to be called in OnRender by the way. I don't know how often you call initialize() but the name tells me that it's just called once. To have a movement you must do that continuously |
| ||
Thank you i will try that, its called initalise but it is called constantly 'enemy.initalise' goes in on render, ive just named it badly |
| ||
Okay that worked, how do i do it so that the enemy moves back and fourth? |
| ||
Now that's more tricky. The easiest way would be to just have boundaries within the enemy walks. Let's say the enemy just walks in x direction. Then you need a start x and an end x:Field startX:Int = 10 Field endX:Int = 100 Field moveRight:Bool = True Method initialise() If( moveRight = True ) ex += exspeed If( ex > endX ) Then moveRight = False Else ex -= exspeed If( ex < startX ) Then moveRight = True End If DrawImage enemy1Sprite, ex , ey End |
| ||
Yeah was trying to think about that but was struggling to actually code it, many thanks |
| ||
You're welcome. Obviously there are more sexier ways of doing this, especially when you have kind of a level layout where the enemies just should walk as long as they hit a wall or something like that... |
| ||
I ideally wanted a side scrolling platform game but never got round to coding it and wasnt sure on how to do it and was limited by time. |
| ||
Enemies are quite similar to players. You update the player by checking which way he wants to move based on keys pressed or whatever. You do the same with enemies based on what they want to do - in this case patrolling between x=0 and x=400, switching direction when he touches either. I would write something like the following - it's slightly longer than it could be, but it's more systematic than updating in OnRender etc. I made the base image global in case you want to have lots of enemies using the same image. Never (in small simple programs, anyway) load images more than once. |
| ||
(deleted double post) |