Character Directional Movement Help?

Monkey Forums/Monkey Programming/Character Directional Movement Help?

Amon(Posted 2013) [#1]
Imagine a top down view. Your character/object can move in either 4 directions.

Two things I'm trying to implement are giving me a problem.

I want to disallow Diagonal Movement but also at the same time I don't want my player/character/object to move in a different direction until a wall or other object is hit.

For example: If moving up by tapping KeyHit(KEY_UP) I want to disable the ability to then move either Left/Right/Down until an object is hit or a value is attained.

It's become difficult to code because I've implemented other things like movement speed, collision etc in an un-optimized way which is causing me bother with adding extra code for extra features.

If I could I'd like to have the Technical Knowledge and Logic of muddy_shoes, the raw ability for complex framework coding that samah and therevils have, the determination for long haul project completion that GfK has, a Light Sabre and a JetPack powered by Leprechaun Whiskey.

You never know...


Raul(Posted 2013) [#2]
I think I understand what you want. I made long time ago in visual basic 6 :)) something like that.

I had a "tile map" with 32 x 32 pixels per tile. My character was allowed to move N S W or E only one tile - 32 pixels. (every 4 or 8 pixels I was change the animation frame)
If the user was pressing the UP key.. the character would go N until an obstacle tile was hit. I was checking the collision map before moving to the next tile.

Anyway that is simple to implement. As soon as the player press a key, check you movementFlag. If movementFlag = 1 then ignore the key. Otherwise, calculate destination and start moving it.. As soon as he reach the destination movementFlag = 0 again :D


dawlane(Posted 2013) [#3]
Well I would think about using something like this. If the key press was stored in a binary format e.g. 1=up,2=down,4=left,5=right with some sort of a flag to single that a key can be pressed.

If key & 1 And move=0 Then
     do_up
     move=1
     return
EndIf
If key & 2 And move=0 Then
     do_down
     move=1
     return
EndIf

etc



Why0Why(Posted 2013) [#4]
If direction=North and Player.CanMove(North)=True Then Player.Y+=1



Gerry Quinn(Posted 2013) [#5]
Reminds me of Pacman, where for ease of play you want to be able to register the player's desire to turn right or left before he reaches the corner.

The solution is easy: when he is moving, and a request is received to go in a currently unavailable direction, record the requested direction in a variable. If he gets to the corner or whatever and it hasn't changed (a further request will overwrite it), he starts moving in that direction.


Amon(Posted 2013) [#6]
Hey guys thanks for the tips/suggestions. I got it working sort of with the recommendations made here. There is a problem though. My game features a character that has a buddy. They are both part of the same Class. The only difference is that they are a different colour. <<<

*****SIDESTEP*****
Colour

Above is how we spell Colour in the United Kingdom. As the English people invented the English Language, nurtured it, advanced it, matured it and fine tuned it, I take personal offence that no matter the configuration of my Chrome Browser the bloody spell check flags The Proper Spelling Of Colour as a spelling mistake which should be Color. We all know that the spelling of Colour as Color is Americas way of trying to claim the English Language as their invention. Sorry but it's not. Google Chrome needs to learn that.

******END SIDESTEP*****

Now both these players which are part of the same Class and list start in different locations. I'm pulling my hair out trying to implement a way that stops both of them changing direction until both have either hit an object or both have stopped moving due to an obstacle.

Any help with that would be appreciated.

Ta!


Why0Why(Posted 2013) [#7]
I use what I posted above. Add a method to check to see if the next move is valid. I add this method to my player classes. Just pass in x and y position. If it isn't valid then try the left or right tile with the same method and then turn them to that direction if it is valid.

So say the player is at x=10 and y=10 and the wall is at y=8. You pass in (10,9) to see if he can move up 1. he can so player is updated to x=10 and y=9. Then you pass in the next position (10,8). It returns false because there is a wall there. Now randomly choose left or right, say right is chosen. Then you pass in (11,9) and it is valid so turn player right.


Gerry Quinn(Posted 2013) [#8]
Amon,

I don't see what your problem is with two objects being of the same class, so long as they are independent instances of the class. The only thing that's needed is to put them in contact with each other:

local char1:Actor = New Actor()
local char2:Actor = New Actor()
char1.myBuddy = char2
char2.myBuddy = char1
actors.AddLast( char1 )
actors.AddLast( char2 )


'.......
Class Actor
Method CanChangeDirection:Bool()
Return IsStopped() And mybuddy.IsStopped()
End
End