I thought I’d try and experiment with improving the collision detection so it doesn’t just stop the character in their tracks when they touch a prop. So if you run into the bottom of the rock while moving up and right it would stop moving up but carry on to the right.
To do this I’m using the undoMovement() function I’d previously split into two parts to allow continuation of movement running into the boundaries of the map.
The problem I’m having is that it only seems to work on the left and right sides of the props, on the bottom and top the character just gets stuck in place when it touches them. Even though the code for each is copied with the axis and width/height changed.
Feel like I’m missing something simple, but any help would be much appreciated as I’ve been banging my head against a wall trying to work out why it’s behaving like this.
Try to setup your boolean tests as variables, that way when you step through your code with a breakpoint you can see where things may go wrong. I’d also recommend saving the collision of the prop as a variable to get even more data.
Here’s an example of what I was doing when I tried to figure out this problem myself one day. (this code doesn’t work either).
Thanks for the pointers, I’ve tried creating variables to track the values but it just gives me ‘not available’ when I run the game. I’ve put a breakpoint below the while loop so I can move around in game to see the values change but get nothing. Any ideas what I’m doing wrong?
Conditional breakpoints worked like a charm. Noticed that the knight was getting stuck because it was moving inside the collision rectanlge for the prop before it stopped so it was triggering more movement conditions than it was meant to. So I created a buffer zone equivalent to the knight’s speed so it would only undo the movement that went further than knight.getSpeed() into the collision recs. Excuse the messy code, it’s how I had to written down as I was thinking it through.
Decided to have one left crack at it after I finished the course because I needed to working to experiment with introducing other features. I realised I was thinking about it the wrong way, all that matters is a collision, the direction of collision, and the direction of movement.
The code below appears to be working fully without getting stuck or causing any odd movement. The test will be when I appply the collision to the enemies.