Can't get my WASD controls to work
by Andrew Skyberg · in Torque Game Builder · 03/24/2009 (12:41 am) · 11 replies
Hello all,
New to the scene and had picked up 2D for Teens to guide me through the basics. I'm shamefully stuck on ch. 6 where its having me give simple controls to an object. Once I'm in play mode to test it, nothing on the keyboard makes the object respond.
I've used torsion to help me find some mistakes and after proofing myself found only one other correction. Here is what I have for the object class setting, my player and game .cs file. Any help is greatly appreciated. I've also tried searching the forums but nothing yet, I'll keep searching in the meantime.

game.cs file located in gameScripts folder, I didn't include all the code since it only required me to add exec("./player.cs"); to it.
player.cs file located in gameScripts folder
New to the scene and had picked up 2D for Teens to guide me through the basics. I'm shamefully stuck on ch. 6 where its having me give simple controls to an object. Once I'm in play mode to test it, nothing on the keyboard makes the object respond.
I've used torsion to help me find some mistakes and after proofing myself found only one other correction. Here is what I have for the object class setting, my player and game .cs file. Any help is greatly appreciated. I've also tried searching the forums but nothing yet, I'll keep searching in the meantime.
game.cs file located in gameScripts folder, I didn't include all the code since it only required me to add exec("./player.cs"); to it.
function startGame(%level)
{
exec("./player.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}player.cs file located in gameScripts folder
function BattyPlayer::onLevelLoaded(%this, %scenegraph)
{
%MeBatty = %this;
moveMap.bindCmd(keyboard, "w", "BattyPlayerUp();", "BattyPlayerUpStop;");
moveMap.bindCmd(keyboard, "s", "BattyPlayerDown();", "BattyPlayerDownStop();");
moveMap.bindCmd(keyboard, "a", "BattyPlayerLeft();", "BattyPlayerLeftStop();");
moveMap.bindCmd(keyboard, "d", "BattyPlayerRight();", "BattyPlayerRightStop();");
}
function BattyPlayerUp()
{
$BattyPlayer.setLinearVelocityY(-15);
}
function BattyPlayerDown()
{
$BattyPlayer.setLinearVelocityY(15);
}
function BattyPlayerLeft()
{
$BattyPlayer.setLinearVelocityX(-25);
}
function BattyPlayerRight()
{
$BattyPlayer.setLinearVelocityX(25);
}
function BattyPlayerUpStop
{
$BattyPlayer.setLinearVelocityY(0);
}
function BattyPlayerDownStop()
{
$BattyPlayer.setLinearVelocityY(0);
}
function BattyPlayerLeftStop()
{
$BattyPlayer.setLinearVelocityX(0);
}
function BattyPlayerRightStop()
{
$BattyPlayer.setLinearVelocityX(0);
}
#2
03/24/2009 (9:39 am)
Alright, corrected that and also just found out after pressing every key that ` gave me a console. (I should of known this :S )function BattyPlayerUpStop
{
$BattyPlayer.setLinearVelocityY(0);
}
#3
03/24/2009 (10:01 am)
You have no parameter list () after the function name, so it's an incomplete function prototype.
#4
When I looked at what lines it was referring to, its the lines for movement.
ex. Line 12 would be
03/24/2009 (10:13 am)
Aye, alright, I made sure everyone of them was completed. When I run it now it is free of errors but still no response? I verified that its loading the player.cs file but something else caught my eye.When I looked at what lines it was referring to, its the lines for movement.
ex. Line 12 would be
$BattyPlayer.setLinearVelocityY(-15);but in the screen shot below it refers to not finding the object?
#5
03/24/2009 (10:53 am)
Then you don't have an object called $Player (which would be global, obviously). In your screenshot, you appear to call it just BattyPlayer as a class with no specific name, which is fine, but that's not the same as $Player.
#6
Also as I make changes, I'm doing Project - Reload Project to make sure everything is refreshing before testing it, even tried deleting the player.cs.dso so it can be recreated.
03/24/2009 (12:31 pm)
Shouldn't it still move the image I set as class BattyPlayer around on the screen? I'm just going by what is listed in the book, I keep checking it over and I'm still puzzled. Also as I make changes, I'm doing Project - Reload Project to make sure everything is refreshing before testing it, even tried deleting the player.cs.dso so it can be recreated.
#7
03/24/2009 (1:10 pm)
You're calling the functions on an object named $Player. Any way you slice it, you're going to need an object named $Player.
#8
I get the same results as BattyPlayer.

03/24/2009 (1:33 pm)
So if I replace everything that had 'BattyPlayer' to Player is that what your referring to? Please correct me if I'm wrong, I'm going in circles :/I get the same results as BattyPlayer.
function Player::onLevelLoaded(%this, %scenegraph)
{
%MePlayer = %this;
moveMap.bindCmd(keyboard, "w", "PlayerUp();", "PlayerUpStop();");
moveMap.bindCmd(keyboard, "s", "PlayerDown();", "PlayerDownStop();");
moveMap.bindCmd(keyboard, "a", "PlayerLeft();", "PlayerLeftStop();");
moveMap.bindCmd(keyboard, "d", "PlayerRight();", "PlayerRightStop();");
}
function PlayerUp()
{
$Player.setLinearVelocityY(-15);
}
etc...
#9
Look at your functions, they say $Player; look at your editor screen, it says Player. It seems to me that there is a pretty basic lack of understanding about programming here. When you call $Player.method(), you're going to need something called $Player, which also needs to support that method.
03/24/2009 (1:49 pm)
If you scroll up, you'll see that I said that $Player is not the same as Player.Look at your functions, they say $Player; look at your editor screen, it says Player. It seems to me that there is a pretty basic lack of understanding about programming here. When you call $Player.method(), you're going to need something called $Player, which also needs to support that method.
#10

It works, thanks for everyone having the time to let me pick at your brains on my mishap.
03/24/2009 (2:21 pm)
I think I'm on the same page now. I took all the $ symbols away from $Player and now its working. I also took Player out of Class and put it in Name, it wouldn't work otherwise. If it is still incorrect please let me know :)
function PlayerUp()
{
Player.setLinearVelocityY(-15);
}It works, thanks for everyone having the time to let me pick at your brains on my mishap.
#11
$MeBatty = %this;
You're setting the global variable $MeBatty to the object that's passed in to the function, but then other functions are referring to $BattyPlayer, not $MeBatty.
To fix player.cs (the full thing starts on the bottom of page 155 in 2D Game Building for Teens), just replace that $MeBatty line with this:
$BattyPlayer = %this;
Your bat should now fly around using the w, a, s, and d keys.
My son spent hours getting to that point in the book and trying to figure out what was wrong -- he's not a programmer (yet) so he had no idea what to look for. I finally found it after spending too long trying to find the typo in his code -- turned out it's a mistake in the book.
Hey, mistakes happen, right? So where's the errata on the author or publisher site? It's just freakin' irresponsible not to have that available -- especially for something that's aimed at newbies.
Jay Jennings
09/18/2009 (1:07 am)
For anybody else who comes across this problem, it's caused by this line in player.cs:$MeBatty = %this;
You're setting the global variable $MeBatty to the object that's passed in to the function, but then other functions are referring to $BattyPlayer, not $MeBatty.
To fix player.cs (the full thing starts on the bottom of page 155 in 2D Game Building for Teens), just replace that $MeBatty line with this:
$BattyPlayer = %this;
Your bat should now fly around using the w, a, s, and d keys.
My son spent hours getting to that point in the book and trying to figure out what was wrong -- he's not a programmer (yet) so he had no idea what to look for. I finally found it after spending too long trying to find the typo in his code -- turned out it's a mistake in the book.
Hey, mistakes happen, right? So where's the errata on the author or publisher site? It's just freakin' irresponsible not to have that available -- especially for something that's aimed at newbies.
Jay Jennings
Associate Phillip O'Shea
Violent Tulip
moveMap.bindCmd(keyboard, "w", "BattyPlayerUp();", "BattyPlayerUpStop;"); ... function BattyPlayerUpStop { $BattyPlayer.setLinearVelocityY(0); }Do you see the problem there? Try:
moveMap.bindCmd(keyboard, "w", "BattyPlayerUp();", "BattyPlayerUpStop();"); ... function BattyPlayerUpStop() { $BattyPlayer.setLinearVelocityY(0); }