Moving till colission.

E

EzVo

Guest
Hello, I recently started working with Gamemaker and I want to make a puzzle game but I'm having some problems with the movement. I want to make an object that moves in one direction with a single press on the button but it can't move in a different direction till it touches the wall. I know how to make your character move. I just want to know how it keeps moving in that direction till it touches the wall, you can't change the direction in the middle of the movement.
 

obscene

Member
Basically, in the step event... in pseudocode....

Code:
if told_to_move=true
{
  // move in direction (hspeed, vspeed, speed/direction, etc)
  if collision with wall
  {
     // stop movement setting hspeed,vspeed,speed or whatever to 0.
     told_to_move=false
  }
}

A little more to it than that but that's the concept. Every step that code runs and it moves a little more until it reaches the wall and is told to stop moving. The exact way you will do this depends on how you choose to code your collision and how you move your objects.
 
T

TDSrock

Guest
Just simply lock out the controls while the object is moving.

Presuming you have an input field setup around at the top of your step event just this is enough.
Code:
if(!moving){
left = (your left input here)
}
Due to the simplicity of this question I gave you the basic code to deal with this situation.
However I did put it in a spoiler, I hope you will try to figure it out before you open the spoiler up.
 
B

bojack29

Guest
Why not use an alarm in your step event?
Code:
if (alarm[0] == -1){
     if (keyboard_check(vk_right)){
          alarm[0] = 3;
     }
}else{
     if (!place_meeting(x + 1, y, obj_wall)){
          x ++;
          alarm[0] = 3;
     }
}
Youll have to make an alarm event and just leave a comment (otherwise the alarm wont work)
 
T

TDSrock

Guest
Why not use an alarm in your step event?
Code:
if (alarm[0] == -1){
     if (keyboard_check(vk_right)){
          alarm[0] = 3;
     }
}else{
     if (!place_meeting(x + 1, y, obj_wall)){
          x ++;
          alarm[0] = 3;
     }
}
Youll have to make an alarm event and just leave a comment (otherwise the alarm wont work)
This overall sounds like a bad idea, using an alarm would imply knowing how long this will take, amusing we always move at a constant rate and we don't know how far away the wall is(unless we calc it out before hand) we won't know how to wait with the alarm.

Even if we go through the trouble of calculating it out or putting the speed to be correct for the distance to move(so further means faster) it still seems silly to use an alarm of this purpose, when a simple Boolean can do the same, as-long as said Boolean is handled correctly.
 
Last edited by a moderator:
B

bojack29

Guest
This will work fine. Try it yourself. Its like treating an alarm like a boolean variable.
The place meeting function stops the object from moving. The alarm inside the place meeting function resets over and over again so long as it keeps moving.
 
T

TDSrock

Guest
This will work fine. Try it yourself. Its like treating an alarm like a boolean variable.
The place meeting function stops the object from moving.
If using it like a Boolean, why not just use a Boolean.
 
B

bojack29

Guest
Why bother wasting memory on something you dont need?
 
T

TDSrock

Guest
Why bother wasting memory on something you dont need?
Your probably wasting much more through the alarm, that is why. Seeing as the alarm will count down on each step that occurs. Thus the alarm will be burning away additional CPU calls to do exactly that. Memory isn't a huge issue, especially when talking about a Boolean value. in C they only take up 8 bits(or 1 byte) Are you seriously going to aim to save 8 bits when most users(based on steams reports) have 4 gigs of RAM, thus 3.2e+10 bits~.
Saving the memory makes no sense in the context of current hardware. Doing it efficiently however might matter more especially if you are building a CPU intensive game.

Ontop of that you've just cleared one alarm from being used as a Boolean to it being available as something else, as GameMaker still limits you on the amount of Alarms you may have(unless has changed recently, I have not updated some time).

Also I don't think the alarm will take up space in the actual compiled game if it is not used. I haven't tested this but I assume the programmers behind GameMaker are smart enough to not compile unused Alarm events.

And as my last point. If the user ever decides to move to C# with XNA for example, knowing how to do this using an actual Boolean will prove benificial as the alarm event will no longer be around unless it's built up from scratch.
 
B

bojack29

Guest
Why are you getting so upset? Calm down. It solved all his problems in 5 lines of code.

And second I doubt he will use all 12 alarms in a single object. In the 6 years Ive programmed for Game Maker i have never once managed to use all 12 alarms in a single object. That is crazy.

Its simply a clever way to do something like this. Generally I reuse that single commented out alarm over and over again for different things and its a brilliant way to do things without making all thesr one shot variables in a step event.
Code:
if (!a){
     .....
     a = true;
}
Code over and over again is ugly and cumbersome. Even for complex AI I use maybe 2 alarms? Its all about being clever and using ingenuity.
 
B

bojack29

Guest
And my last point, while you talk about stealing an alarm. This trick allows you to use that SAME alarm in many different ways. So no, I dont see it as stealing it. If used cleverly you can use a single alarm to do hundreds of things.
 
Top