Code error?
by rennie moffat · in Torque Game Builder · 08/10/2009 (9:09 am) · 2 replies
HI, I am still working on the same code, to help create basic movement on a 2D hero based scroller. I have gone over the code and can't find an error. It was simply copy and pasted from the player mechanics tutorial. The game loads, however my man does not move. I can't find this glitch. Can anyone drop me some insight?
function playerClass::onLevelLoaded(%this, %scenegraph)
{
$pGuy = %this;
moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
moveMap.bindCmd(keyboard, "space", "playerJump();", "");
%this.setCollisionMaxIterations(2);
%force = 20;
sceneWindow2D.mount($pGuy, "0 0", %force, true);
}
function playerClass::updateHorizontal(%this)
{
if(%this.moveLeft)
{
%this.setLinearVelocityX(-60);
}
if(%this.moveRight)
{
%this.setLinearVelocityX(60);
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
}
if(!%this.airborne)
%this.setLinearVelocityY(0);
}
function playerClass::updateVertical(%this)
{
%yVelocity = %this.getLinearVelocityY();
if(%this.airborne)
{
%this.setLinearVelocityY(0);
%collision = %this.castCollision(0.005);
if(%collision !$= "")
%this.setLinearVelocityX(0);
}
%this.setLinearVelocityY(100);
%collision = %this.castCollision(0.005);
if(%collision $= "")
{
%this.airborne = true;
%this.setConstantForceY(100);
}
else if(%yVelocity < 0 && %this.airborne)
{
%this.setConstantForceY(100);
}
else
{
%this.airborne = false;
%this.setConstantForceY(0);
}
%this.setLinearVelocityY(%yVelocity);
}
function playerClass::updateMovement(%this)
{
%this.updateHorizontal();
%this.updateVertical();
}
function playerJump()
{
if(!$pGuy.airborne)
{
$pGuy.setLinearVelocityY(-225);
$pGuy.airborne = true;
}
if(%this.airborne)
{
%this.setLinearVelocityY(0);
%collision = %this.castCollision(0.005);
if(%collision !$= "")
%this.setLinearVelocityX(0);
}
if(%collision $= "")
{
%this.airborne = true;
%this.setConstantForceY(100);
}
else if(%yVelocity < 0 && %this.airborne)
{
%this.setConstantForceY(100);
}
}
function PlatformerSceneGraph::onUpdateScene()
{
if (isObject($pGuy))
$pGuy.updateMovement();
else
{
%this.airborne = false;
%this.setConstantForceY(0);
}
}About the author
My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.
#2
yes, don worry i am not offended. Only at myself for being so lame when it comes to this. Anyhow, I will press on. The coding, i scanned, but since it was in a tutorial sponsored, I thought "hey, it worked for them, why not me?"
So yes my understanding of TorqueScript coding is coming along, but obviously, still not there.
Thanks for the help, will read other post.
08/10/2009 (11:21 am)
ok cool,yes, don worry i am not offended. Only at myself for being so lame when it comes to this. Anyhow, I will press on. The coding, i scanned, but since it was in a tutorial sponsored, I thought "hey, it worked for them, why not me?"
So yes my understanding of TorqueScript coding is coming along, but obviously, still not there.
Thanks for the help, will read other post.
Torque Owner Ehrlich
you are usig this:
but the functions you're referencing (playerLeft and PlayerRight) are NOT defined in your code. let alone the ones that are supposed to stop the player.
in your case, i'd do something like:
function playerClass::onLevelLoaded(%this, %scenegraph) { $pGuy = %this; $pSpeed = 10; moveMap.bindCmd(keyboard, "left", "$pGuy.move(left);", "$pGuy.Stop();"); moveMap.bindCmd(keyboard, "right", "$pGuy.move(right);", "$pGuy.Stop();"); moveMap.bindCmd(keyboard, "space", "playerJump();", ""); } function playerClass::move(%this, %dir) { switch$(%dir) { case "left": %this.setLinearVelocityX(-$pSpeed); case "right": %this.setLinearVelocityX($pSpeed); } } function playerClass::Stop(%this) { %this.setAtRest(); }this way, you control your player with no problems... and well, there you have it.
my advice, if you cant read other ppl's code, dont just copy and paste it... its not good at all, if you cant see (read) the logic behind it.
PS: this is just my take on the problem... there can be another approaches that are WAY better than mine, but take this one as a starting point. oh... and i didnt write a snippet for the jump function, since it all depends on how you want your char to do that (with gravity or stuff like that), and since i dont know how you wanna take onto that, i better leave it up to you.