Scores
by Anthony Ratcliffe · in Torque Game Builder · 05/15/2008 (2:42 pm) · 3 replies
I tried to create a score system for player 1 and 2 on a pong game from a tutorial
heres my code
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
function playerOneClass::onLevelLoaded(%this, %scenegraph)
{
$playerOne = %this;
// key bindings for player one
moveMap.bindCmd(keyboard, "w", "playerOneUp();", "playerOneUpStop();");
moveMap.bindCmd(keyboard, "s", "playerOneDown();", "playerOneDownStop();");
}
function playerTwoClass::onLevelLoaded(%this, %scenegraph)
{
$playerTwo = %this;
// key bindings for player two.
moveMap.bindCmd(keyboard, "UP", "playerTwoUp();", "playerTwoUpStop();");
moveMap.bindCmd(keyboard, "DOWN", "playerTwoDown();", "playerTwoDownStop();");
}
function ballClass::onLevelLoaded(%this, %scenegraph)
{
$ball = %this;
$player1 = 0;
$player2 = 0;
// start the ball off at a speed of 100 at a 45 degree angle
$ball.setLinearVelocityPolar(45, 100);
}
// player one movement
function playerOneUp()
{
$playerOne.moveUp = true;
}
function playerOneDown()
{
$playerOne.moveDown = true;
}
function playerOneUpStop()
{
$playerOne.moveUp = false;
}
function playerOneDownStop()
{
$playerOne.moveDown = false;
}
// player two movement
function playerTwoUp()
{
$playerTwo.moveUp = true;
}
function playerTwoDown()
{
$playerTwo.moveDown = true;
}
function playerTwoUpStop()
{
$playerTwo.moveUp = false;
}
function playerTwoDownStop()
{
$playerTwo.moveDown = false;
}
// check if the ball collides.
function ballClass::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
// this just makes sure the paddles stay put on their x axis
if(%dstObj.class $= "playerOneClass")
{
$playerOne.setPositionX(-96);
}
if(%dstObj.class $= "playerTwoClass")
{
$playerTwo.setPositionX(96);
}
}
// movment for player one
function playerOneClass::updateMovement(%this)
{
if (%this.moveUp)
{
%this.setLinearVelocityY(-100);
}
if (%this.moveDown)
{
%this.setLinearVelocityY(100);
}
if (!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY(0);
}
}
// movement for player two
function playerTwoClass::updateMovement(%this)
{
if (%this.moveUp)
{
%this.setLinearVelocityY(-100);
}
if (%this.moveDown)
{
%this.setLinearVelocityY(100);
}
if (!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY(0);
}
}
function ballClass::onWorldLimit(%this, %mode, %limit)
{
switch$ (%limit)
{
case "left":
$player1++;
echo ("p1scores") ;
case "right":
$player2++;
echo ("p2scores") ;
}
}
// update the scene
function t2dSceneGraph::onUpdateScene()
{
$playerOne.updateMovement();
$playerTwo.updateMovement();
}
the names of the score are guiplayera and guiplayer1 and guiplayerb and guiplayer2 for the other score
anyone work this out i'm stumped
heres my code
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
function playerOneClass::onLevelLoaded(%this, %scenegraph)
{
$playerOne = %this;
// key bindings for player one
moveMap.bindCmd(keyboard, "w", "playerOneUp();", "playerOneUpStop();");
moveMap.bindCmd(keyboard, "s", "playerOneDown();", "playerOneDownStop();");
}
function playerTwoClass::onLevelLoaded(%this, %scenegraph)
{
$playerTwo = %this;
// key bindings for player two.
moveMap.bindCmd(keyboard, "UP", "playerTwoUp();", "playerTwoUpStop();");
moveMap.bindCmd(keyboard, "DOWN", "playerTwoDown();", "playerTwoDownStop();");
}
function ballClass::onLevelLoaded(%this, %scenegraph)
{
$ball = %this;
$player1 = 0;
$player2 = 0;
// start the ball off at a speed of 100 at a 45 degree angle
$ball.setLinearVelocityPolar(45, 100);
}
// player one movement
function playerOneUp()
{
$playerOne.moveUp = true;
}
function playerOneDown()
{
$playerOne.moveDown = true;
}
function playerOneUpStop()
{
$playerOne.moveUp = false;
}
function playerOneDownStop()
{
$playerOne.moveDown = false;
}
// player two movement
function playerTwoUp()
{
$playerTwo.moveUp = true;
}
function playerTwoDown()
{
$playerTwo.moveDown = true;
}
function playerTwoUpStop()
{
$playerTwo.moveUp = false;
}
function playerTwoDownStop()
{
$playerTwo.moveDown = false;
}
// check if the ball collides.
function ballClass::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts )
{
// this just makes sure the paddles stay put on their x axis
if(%dstObj.class $= "playerOneClass")
{
$playerOne.setPositionX(-96);
}
if(%dstObj.class $= "playerTwoClass")
{
$playerTwo.setPositionX(96);
}
}
// movment for player one
function playerOneClass::updateMovement(%this)
{
if (%this.moveUp)
{
%this.setLinearVelocityY(-100);
}
if (%this.moveDown)
{
%this.setLinearVelocityY(100);
}
if (!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY(0);
}
}
// movement for player two
function playerTwoClass::updateMovement(%this)
{
if (%this.moveUp)
{
%this.setLinearVelocityY(-100);
}
if (%this.moveDown)
{
%this.setLinearVelocityY(100);
}
if (!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY(0);
}
}
function ballClass::onWorldLimit(%this, %mode, %limit)
{
switch$ (%limit)
{
case "left":
$player1++;
echo ("p1scores") ;
case "right":
$player2++;
echo ("p2scores") ;
}
}
// update the scene
function t2dSceneGraph::onUpdateScene()
{
$playerOne.updateMovement();
$playerTwo.updateMovement();
}
the names of the score are guiplayera and guiplayer1 and guiplayerb and guiplayer2 for the other score
anyone work this out i'm stumped
About the author
#2
05/15/2008 (4:02 pm)
Is your on worldLimit callback getting hit? Have you specified an area and checked callback for your ball's worldlimit?
#3
like james said, you should specify the world limits for the ball object, and enable the onWorldLimit callback... maybe thats whats failing... and, just for debug, add some line like this in your code:
warn("executing onworldlimit callback") //so you know the callback its being called
and so on... like i said, its pretty usefull in the testing phase.
05/20/2008 (11:53 am)
Its good practive to use debug code in this instances, so you can check if the callbacks are being called, and if so, if they're workin...like james said, you should specify the world limits for the ball object, and enable the onWorldLimit callback... maybe thats whats failing... and, just for debug, add some line like this in your code:
warn("executing onworldlimit callback") //so you know the callback its being called
and so on... like i said, its pretty usefull in the testing phase.
Torque Owner Anthony Ratcliffe