Disable/freeze input devices
by Conny Landstedt · in Torque Game Builder · 02/17/2009 (12:39 pm) · 9 replies
Hi I was wondering if someone would help me whit a problem I am having. I have been trying to figure this thing out by myself but I haven’t found anything about this on the forum or the documentation.
So the thing is this, I want to disable/freeze the keyboard keys that are used for the player to move and do stuff for a while or until something happens like a collision. I am still weary new to TGB so I would appreciate explanations or help to be precise so I can learn and don’t get confused.
Thanks in advance.
So the thing is this, I want to disable/freeze the keyboard keys that are used for the player to move and do stuff for a while or until something happens like a collision. I am still weary new to TGB so I would appreciate explanations or help to be precise so I can learn and don’t get confused.
Thanks in advance.
About the author
#2
02/18/2009 (9:44 am)
Hi thank you for the very helpful and informative answer. It helped me a lot. Now I just have small problem reinstating the input. If you or someone else would be so kind of telling me what I am doing wrong I would be very great full. This is how my player movement code look right now. I have tried many different things to get this to work but I won’t go in to them sins they are so many even I can’t remember them all. The thing is that this code almost works. It’s just that I can’t get the player control to reinstate after a collision...function PlayerBall::onLevelLoaded(%this, %scenegraph)
{
$BallPlayer = %this;
moveMap.bindCmd(keyboard, "up", "ballPlayerUp();", "ballPlayerUpStop();");
moveMap.bindCmd(keyboard, "down", "ballPlayerDown();", "ballPlayerDownStop();");
moveMap.bindCmd(keyboard, "left", "ballPlayerLeft();", "ballPlayerLeftStop();");
moveMap.bindCmd(keyboard, "right", "ballPlayerRight();", "ballPlayerRightStop();");
}
function ballPlayerUp()
{
$BallPlayer.moveUp = true;
$BallPlayer.updateMovement();
}
function ballPlayerDown()
{
$BallPlayer.moveDown = true;
$BallPlayer.updateMovement();
}
function ballPlayerLeft()
{
$BallPlayer.moveLeft = true;
$BallPlayer.updateMovement();
}
function ballPlayerRight()
{
$BallPlayer.moveRight = true;
$BallPlayer.updateMovement();
}
function PlayerBall::updateMovement(%this)
{
if(%this.moveLeft)
{
$BallPlayer.setLinearVelocityX( -$BallPlayer.hSpeed );
moveMap.pop();
}
if(%this.moveRight)
{
$BallPlayer.setLinearVelocityX( $BallPlayer.hSpeed );
moveMap.pop();
}
if(%this.moveUp)
{
$BallPlayer.setLinearVelocityY( -$BallPlayer.vSpeed );
moveMap.pop();
}
if(%this.moveDown)
{
$BallPlayer.setLinearVelocityY( $BallPlayer.vSpeed );
moveMap.pop();
}
if (%collision = true)
{
%this.moveLeft = false;
%this.moveRight = false;
%this.moveUp = false;
%this.moveDown = false;
moveMap.push();
return;
}
}
#3
02/18/2009 (10:24 am)
How and where does %collision get set to true?
#4
Because I have set the player to send collision and send physics, respectively I have set the environment that it collides whit to receive collision and receive physics. Maybe I should not be using ‘=’ but something like ‘==’ or ‘&&’. Maybe that is what is confusing…
02/18/2009 (10:47 am)
Well I might not quite understand your question, because to me as I understand it I set it to true right there don’t I? Or more like if the collision is true then that happens as in if there is a collision this happens. But I might be using that code entirely wrong but it seems to be working because the objects speed gets reset to 0.Because I have set the player to send collision and send physics, respectively I have set the environment that it collides whit to receive collision and receive physics. Maybe I should not be using ‘=’ but something like ‘==’ or ‘&&’. Maybe that is what is confusing…
#5
The function updateMovement(...) is intended to be called every time a user presses a movement key.
So, the user presses the UP key, which results in $BallPlayer.moveUp being set to true. updateMovement(...) is called immediately.
Inside the function, we hit this section of code:
We set the player's velocity to start moving, then immediately disable the moveMap.
Finally, this section of code will ALWAYS execute:
You are not checking if there was a collision. You are creating a variable, setting it to true, then performing the rest of the function which includes pushing the moveMap and canceling movement variables.
If this is your intention, why use an if() statement at all? If it is not your intention, you may want to use if(%collision == true), and set that variable elsewhere.
02/18/2009 (11:10 am)
Well, let's step through it:function PlayerBall::updateMovement(%this)
The function updateMovement(...) is intended to be called every time a user presses a movement key.
So, the user presses the UP key, which results in $BallPlayer.moveUp being set to true. updateMovement(...) is called immediately.
Inside the function, we hit this section of code:
if(%this.moveUp)
{
$BallPlayer.setLinearVelocityY( -$BallPlayer.vSpeed );
moveMap.pop();
}We set the player's velocity to start moving, then immediately disable the moveMap.
Finally, this section of code will ALWAYS execute:
if (%collision = true)
{
%this.moveLeft = false;
%this.moveRight = false;
%this.moveUp = false;
%this.moveDown = false;
moveMap.push();
return;
}You are not checking if there was a collision. You are creating a variable, setting it to true, then performing the rest of the function which includes pushing the moveMap and canceling movement variables.
If this is your intention, why use an if() statement at all? If it is not your intention, you may want to use if(%collision == true), and set that variable elsewhere.
#6
Then I read up a bit more about tilesets like in some tutorials and stuff and made a new class called tileClass and applied that class to my tileset. After that I added this code to my player class:
But for some reason it still won’t work although as far as I know it should. I can move the player one time and then it freezes and won’t unlock although it should.
02/18/2009 (7:35 pm)
Ok so here is how I have tried to fix it. First I remove as you said all the stupid code that wasn’t even necessary so the updateMovement function looks like this now and it works just fine: function PlayerBall::updateMovement(%this)
{
if(%this.moveLeft)
{
$BallPlayer.setLinearVelocityX( -$BallPlayer.hSpeed );
moveMap.pop();
}
if(%this.moveRight)
{
$BallPlayer.setLinearVelocityX( $BallPlayer.hSpeed );
moveMap.pop();
}
if(%this.moveUp)
{
$BallPlayer.setLinearVelocityY( -$BallPlayer.vSpeed );
moveMap.pop();
}
if(%this.moveDown)
{
$BallPlayer.setLinearVelocityY( $BallPlayer.vSpeed );
moveMap.pop();
}
%this.moveLeft = false;
%this.moveRight = false;
%this.moveUp = false;
%this.moveDown = false;
}Then I read up a bit more about tilesets like in some tutorials and stuff and made a new class called tileClass and applied that class to my tileset. After that I added this code to my player class:
function PlayerBall::onCollision(%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
if (%dstObject.class == "tileClass")
{
moveMap.push();
}
}But for some reason it still won’t work although as far as I know it should. I can move the player one time and then it freezes and won’t unlock although it should.
#7
Can you describe the game play element a little more? You have a player represented by a ball, which you can move up, down, left and right using the keyboard.
Here are the key questions:
1. Do you want the player to constantly move, or just when you are pressing a key?
2. What, specifically, will make the player stop moving?
3. What, specifically, will allow the player move again?
If it is simple collision, then we can avoid enabling/disabling the moveMap all together and just use regular collision.
02/18/2009 (10:16 pm)
@Conny - Hmm, it seems we are slipping further away from an optimized solution. Let's start at the beginning.Can you describe the game play element a little more? You have a player represented by a ball, which you can move up, down, left and right using the keyboard.
Here are the key questions:
1. Do you want the player to constantly move, or just when you are pressing a key?
2. What, specifically, will make the player stop moving?
3. What, specifically, will allow the player move again?
If it is simple collision, then we can avoid enabling/disabling the moveMap all together and just use regular collision.
#8
1. I want the player to move when I press the button. Whit that I mean that in the beginning the player is standing still. I then press a direction and the player ball moves in a straight line in that direction until it hits something. The player constantly move whit out any more input just the first button press dose it all and he it stops when it hits something. That is why it is so important for the player not to be able to make any more input when the object is moving.
2. When the player hits a solid object on the tilemap. If not the player continues outside the map and the game board and dies.
3. When the player have stopped moving from a collision whit the map as in from a box. The player should then be able to press one of the 4 direction keys again. That means that whenever the player is standing still he can move.
02/19/2009 (7:51 am)
If you think so then by all means I will answer your questions. I have a player that moves on 4 direktions. The level is built up by a tilemap if “boxes” that the player can collide whit. To reace his gole.1. I want the player to move when I press the button. Whit that I mean that in the beginning the player is standing still. I then press a direction and the player ball moves in a straight line in that direction until it hits something. The player constantly move whit out any more input just the first button press dose it all and he it stops when it hits something. That is why it is so important for the player not to be able to make any more input when the object is moving.
2. When the player hits a solid object on the tilemap. If not the player continues outside the map and the game board and dies.
3. When the player have stopped moving from a collision whit the map as in from a box. The player should then be able to press one of the 4 direction keys again. That means that whenever the player is standing still he can move.
#9
Basically just copy your bindings that you set at the start into the function and delete the function names within the binding.
it will look a little like this:
moveMap.bindCmd(keyboard, "left", "", "");
moveMap.bindCmd(keyboard, "right", "", "");
moveMap.bindCmd(keyboard, "up", "", "");
The keys are still bound but now have no functions attached and so are inactive.
Then just re-bind them in the onCollision function when the collision occurs.
I hope that makes sense.
04/21/2009 (1:58 pm)
You could try the really bodged method I used in one of my games which is to set the button that tells it to move to also completely unbind the keys.Basically just copy your bindings that you set at the start into the function and delete the function names within the binding.
it will look a little like this:
moveMap.bindCmd(keyboard, "left", "", "");
moveMap.bindCmd(keyboard, "right", "", "");
moveMap.bindCmd(keyboard, "up", "", "");
The keys are still bound but now have no functions attached and so are inactive.
Then just re-bind them in the onCollision function when the collision occurs.
I hope that makes sense.
Community Manager Michael Perry
ZombieShortbus
Once declared, you can create key bindings for the ActionMap:
moveMap.bind("keyboard", "a", "doAction", "Action Description");Before it is usable, the system needs to know what the current ActionMap being used is. This is acknowledge through pushing and popping:
Enables the moveMap
Disables the moveMap
Removes the moveMap
When you create a new TGB project from the editor, moveMap is usually generated for you. Check out game\gameScripts\game.cs, startGame() function.