TGB If statements ignored, content executed.(Solved)
by Jan Max Friedrich · in Torque Game Builder · 05/01/2009 (9:01 pm) · 4 replies
Okay I'm running into an issue here where TGB seems to completely ignore my if statements.
It's just some very basic movement code:
function sprite::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
moveMap.bindCmd(keyboard, "left", "moveLeft();", "moveLeftStop();");
moveMap.bindCmd(keyboard, "right", "moveRight();", "moveRightStop();");
%this.enableUpdateCallback();
SetMovementVariables
();
applyMovement();
}
function SetMovementVariables
()
{
$slow = 20;
$mid = 30;
$fast = 60;
$ultra = 200;
}
function moveLeft()
{
$MoveNow = "Left";
}
function moveLeftStop()
{
if($MoveNow == "Left")
{
$MoveNow = "Stop";
}
}
function moveRight()
{
$MoveNow = "Right";
}
function moveRightStop()
{
if($MoveNow == "Right")
{
$MoveNow = "Stop";
}
}
function applyMovement()
{
//echo("SCHEDULER ACTIVE");
if($MoveNow == "Left"){
sprite.setLinearVelocityX(-$slow);
error("MOVING LEFT");
}
if($MoveNow == "Right"){
sprite.setLinearVelocityX($slow);
error("MOVING RIGHT");
}
if($MoveNow == "Stop"){
sprite.setLinearVelocityX(0);
error("MOVEMENT STOPPED");
}
schedule(10, 0, "applyMovement");
}
Sounds like it should be working, right? Well this is what it produces in the console(with NO button pressed):
MOVING LEFT
MOVING RIGHT
MOVEMENT STOPPED
MOVING LEFT
MOVING RIGHT
MOVEMENT STOPPED
MOVING LEFT
MOVING RIGHT
MOVEMENT STOPPED
...
(and so on)
Am I going insane or am I missing something. I can't think of what may be going wrong unless I completely misunderstand how TGB handles if statements. What baffles me is the fact that all this starts happening with no button pressed.
It's just some very basic movement code:
function sprite::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
moveMap.bindCmd(keyboard, "left", "moveLeft();", "moveLeftStop();");
moveMap.bindCmd(keyboard, "right", "moveRight();", "moveRightStop();");
%this.enableUpdateCallback();
SetMovementVariables
();
applyMovement();
}
function SetMovementVariables
()
{
$slow = 20;
$mid = 30;
$fast = 60;
$ultra = 200;
}
function moveLeft()
{
$MoveNow = "Left";
}
function moveLeftStop()
{
if($MoveNow == "Left")
{
$MoveNow = "Stop";
}
}
function moveRight()
{
$MoveNow = "Right";
}
function moveRightStop()
{
if($MoveNow == "Right")
{
$MoveNow = "Stop";
}
}
function applyMovement()
{
//echo("SCHEDULER ACTIVE");
if($MoveNow == "Left"){
sprite.setLinearVelocityX(-$slow);
error("MOVING LEFT");
}
if($MoveNow == "Right"){
sprite.setLinearVelocityX($slow);
error("MOVING RIGHT");
}
if($MoveNow == "Stop"){
sprite.setLinearVelocityX(0);
error("MOVEMENT STOPPED");
}
schedule(10, 0, "applyMovement");
}
Sounds like it should be working, right? Well this is what it produces in the console(with NO button pressed):
MOVING LEFT
MOVING RIGHT
MOVEMENT STOPPED
MOVING LEFT
MOVING RIGHT
MOVEMENT STOPPED
MOVING LEFT
MOVING RIGHT
MOVEMENT STOPPED
...
(and so on)
Am I going insane or am I missing something. I can't think of what may be going wrong unless I completely misunderstand how TGB handles if statements. What baffles me is the fact that all this starts happening with no button pressed.
About the author
#2
Thanks You SO much!
That really made me regain my sanity.
Just changing the string comparisons still resulted in the same bug.
However:
When I took the // off of //echo("SCHEDULER ACTIVE"); it suddenly started working perfectly. Is there an issue with comments on the first line? Who knows.. but I'm glad it works. =D
05/01/2009 (9:30 pm)
Thanks You SO much!
That really made me regain my sanity.
Just changing the string comparisons still resulted in the same bug.
However:
When I took the // off of //echo("SCHEDULER ACTIVE"); it suddenly started working perfectly. Is there an issue with comments on the first line? Who knows.. but I'm glad it works. =D
#3
function sprite::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
moveMap.bindCmd(keyboard, "left", "moveLeft();", "moveLeftStop();");
moveMap.bindCmd(keyboard, "right", "moveRight();", "moveRightStop();");
%this.enableUpdateCallback();
SetMovementVariables
();
HGMovement();
}
function SetMovementVariables
()
{
$slow = 20;
$mid = 30;
$fast = 60;
$ultra = 200;
$MoveNow = "Stop";
}
function moveLeft()
{
$MoveNow = "Left";
}
function moveLeftStop()
{
if($MoveNow $= "Left")
{
$MoveNow = "Stop";
}
}
function moveRight()
{
$MoveNow = "Right";
}
function moveRightStop()
{
if($MoveNow $= "Right")
{
$MoveNow = "Stop";
}
}
function HGMovement()
{
echo("SCHEDULER ACTIVE", $MoveNow);
if($MoveNow=="Left"){
sprite.setLinearVelocityX(-$slow);
error("MOVING LEFT");
}
if($MoveNow $= "Right"){
sprite.setLinearVelocityX($slow);
error("MOVING RIGHT");
}
if($MoveNow $= "Stop"){
sprite.setLinearVelocityX(0);
error("MOVEMENT STOPPED");
}
schedule(10, 0, "HGMovement");
}
05/01/2009 (9:33 pm)
Working code for anyone who may be interested:function sprite::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
moveMap.bindCmd(keyboard, "left", "moveLeft();", "moveLeftStop();");
moveMap.bindCmd(keyboard, "right", "moveRight();", "moveRightStop();");
%this.enableUpdateCallback();
SetMovementVariables
();
HGMovement();
}
function SetMovementVariables
()
{
$slow = 20;
$mid = 30;
$fast = 60;
$ultra = 200;
$MoveNow = "Stop";
}
function moveLeft()
{
$MoveNow = "Left";
}
function moveLeftStop()
{
if($MoveNow $= "Left")
{
$MoveNow = "Stop";
}
}
function moveRight()
{
$MoveNow = "Right";
}
function moveRightStop()
{
if($MoveNow $= "Right")
{
$MoveNow = "Stop";
}
}
function HGMovement()
{
echo("SCHEDULER ACTIVE", $MoveNow);
if($MoveNow=="Left"){
sprite.setLinearVelocityX(-$slow);
error("MOVING LEFT");
}
if($MoveNow $= "Right"){
sprite.setLinearVelocityX($slow);
error("MOVING RIGHT");
}
if($MoveNow $= "Stop"){
sprite.setLinearVelocityX(0);
error("MOVEMENT STOPPED");
}
schedule(10, 0, "HGMovement");
}
#4
That really shouldn't be an issue... I've noted it and will look into it soon.
Glad you got it all working though!
05/01/2009 (11:38 pm)
Quote:When I took the // off of //echo("SCHEDULER ACTIVE"); it suddenly started working perfectly. Is there an issue with comments on the first line? Who knows.. but I'm glad it works. =D
That really shouldn't be an issue... I've noted it and will look into it soon.
Glad you got it all working though!
Associate Alex Scarborough