Moving between walls

How to make Player to stop when it collides with wall but be able to move in other directions (with no walls)?
When I’m trying to do it the Player just stops after colliding and don’t move anymore or constantly bumping against the wall and directions for keys get opposite.
What I want that If Player hit the wall it stops, steps a little in opposite direction and then will be ready to move in other directions.

Are you trying to make a platformer? If so, try using Platformer automatism.

No, it’s a maze game. You can move up, down, left and right, and you can not move through the walls.
It’s sort of platformer without a gravity and jumping, I think.

Really simple to do! Check the Space Marine template (when creating a new game, choose native platform and then Space Marine): most of the time, just add an event without condition, just an action to automatically separate objects and it will be ok. :slight_smile: (This action can be used both in native and HTML5 games).

I really like that command (“move away…”). Really simple to use. :slight_smile: Thanks!
For moving Player I used “Apply a force” and “X/Y position”. I found that the second one is easier to do, but at the end of the program I gave an action “Set time scale to 0” to stop the game but the Player stopped moving only where the action was “Aply a force”. How to stop Player to move in other directions too?
Here is the text of program:
Another question:
How to restart the game after game is over?

The problem is: “Apply a force” depends of time, but move an object modifying its position not depends (it’s made automatically). To make “X/Y position” depends of time, you can do:

Right key is pressed                      |                   Do +50 * TimeDelta() to the position of Object

DeltaTime() save the time elapsed since the last frame, so your object will move 50 pixels per seconds :wink:

Easy, add the action: “Go to scene”, with the game scene (or menu scene) to restart it :slight_smile:

This is already the case when you set a force of 50px/sec (the force internally use TimeDelta() ).

Thanks for explanation of using TimeDelta() but I prefer to not use it for simplicity of the program. So, maybe I just destroy Player object so it couldn’t move. :wink:
When I use “Go to scene” to restart the same scene it just stays as it was at the end of the game (with already no coins).
Another question: How to make a pause between actions? For example, show “game over” text, wait 3 seconds, restart the game.

When the player loses the game, make a Variable gameover = 1. Then have an event that looks like this:

If gameover = 1 | Create object gameovertext
| Do = 99 to Z-Order of gameovertext
| Reset the timer “gameend”
| Do = 1 to Variable end

And another event that looks like this:

If gameover = 1
If end = 1
If timer “gameend” > 3 seconds | Go to scene “levelname”

“Go to scene” still not working. I suppose to make it work I need to have at least 2 scenes, for example, “game” and “game over”. Then during switching between them they will restart.

Tried with 2 scenes - in Preview can see only one and message that in compiled game I’ll go to another scene. So, no switching, no restarting.
Tried to compile the game - Player stuck between coins - can not collect them - moving away from them like from walls. In preview there is no such problem. Added condition to “move away”: “If Player collide with wall” - the same problem. :confused:

So, in GD command “wait 3 sec” or “pause 3 sec” consists from 2 lines: “Reset timer” where you want a pause and “Value of timer is >3” to tell that pause is 3 sec?

Just do Go to scene “nameofthecurrentscene”

Perhaps you should include your project file with your post so we can see what the problems are?


Pretty much.

[OK, I got it about Restarting the game. When yo use the action “Go to scene” you are restarting Events for this scene but not its initial look (like in the Scene editor). To restart the scene itself you use the condition “At the beginning of the scene” and for every sprite on the scene which can change its position during the game you need to add its initial position which I assumed will be performed automatically. I think it is possible to create an action “Restart the game” which will do it for you.] WRONG, just there is no switching between scene in the Preview mode.

[And about moving away from coins instead only from walls in the compiled game, I suspect this is a bug.] The bug is in my project. The game constantly restarts - just need to reset timer in the beginning of the game.

Go to scene “scenename” should load the scene exactly in its start state (it does for me)? Are you using the latest version of GD?

It’s really impossible to say unless you show us the events you’re using or upload the file, however, I strongly suspect it’s not a bug. I haven’t yet encountered any bugs with collisions of this kind.

OK, now I succeeded to compile the project again and everything works fine: I can collect coins and restart the scene. But during the preview restarting of the scene doesn’t work. (8.01 KB)

It’s normal, the preview doesn’t support the scene change.

Here is an example where the Player stucks between the coins in compiled game but moves normally collecting them during the preview. Can’t make the timer work either. (9.52 KB)
Here is the lines with the timer:

For the timer - You must only reset the timer once! In your code, you are resetting the timer constantly once there are no coins left. You can do something like:

Number of coin = 0
Number of door = 0
Variable end = 0

Show object gameover
Reset the timer “pause”
Do = 1 to Variable end

This way, the timer will only be reset once. You could also use ‘trigger once’ condition instead to do the same thing. Edit: You also must reset this timer at the beginning of the scene.

At the beginning of scene | Reset the timer “pause”

For the hero getting stuck in compiled game.

Perhaps it is because you have no conditions on your move hero away from door and move hero away from wall events. It should look like this:

hero is in collision with wall | Move hero away from wall

hero is in collision with door | Move hero away from door

Resetting timer at the beginning of the game unstucks the Player, so now he moves normally in the compiled game just like in the Preview. But still why Preview and compiled game behaved differently is a mystery for me. A bug?

Can not find in Actions “Trigger once”. Can see it only in Conditions (Advanced), not Actions.

Because the preview does not support switching scenes. If you do not reset that timer, the scene is constantly resetting in the compiled game (because the timer has a junk value and so the condition timer > 3 is always true).

Edit: Basically, it’s good practice to reset timers before you use them.