Making a platform game.
by Greg Charles Eckermann · in Torque Game Builder · 03/18/2007 (11:51 pm) · 4 replies
Whoever made the Mini Platform tutorial, its a great tutorial BTW. I posted this before but no one answered, so I might as well make a topic about it. Anyhow I'm using the code to prototype a Castlevania cone. Where the mid air movements cannot be controlled. The Mini Platform tutorial works great as a prototype except when I add a function to limit the jump movements. If I take out the if (!%airborne) it will work with no bugs. But I don't want the player to control his/her movements while in mid-air. But when I add it, it doe's not work all the time and in a second the character's position will freeze at a random time.
Here's may changes on the Mini Platform tutorial.
function playerJump()
{
if(!$pGuy.airborne)
{
$pGuy.setLinearVelocityY(-80);
$pGuy.airborne = true;
$pGuy.jump = true;
}
}
function playerClass::jump(%this)
{
if(%this.moveLeft)
{
%this.jumpd=1;
}
if(%this.moveRight)
{
%this.jumpd=2;
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.jumpd=3;
}
}
function playerClass::updateHorizontal(%this)
{
if(%this.jumpd==1)
{
%this.setLinearVelocityX(-30);
}
if(%this.jumpd==2)
{
%this.setLinearVelocityX(30);
}
if(%this.jumpd==3)
{
%this.setLinearVelocityX(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(50);
%collision = %this.castCollision(0.005);
if(%collision $= "")
{
%this.airborne = true;
%this.setConstantForceY(50);
if(!%this.jump){
%this.jumpd=3;
}
}
else if(%yVelocity < 0 && %this.airborne)
{
%this.setConstantForceY(50);
}
else
{
if(%yVelocity > 0)
{
%this.airborne = false;
%this.jump = false;
%this.setConstantForceY(0);
}
}
%this.setLinearVelocityY(%yVelocity);
}
function playerClass::updateMovement(%this)
{
%this.jump();
%this.updateHorizontal();
%this.updateVertical();
%this.setCurrentAnimation();
debuger.text = %this.jumpd;
}
Should I rewrite it all from scratch, or is there a way to fix this?
Here's may changes on the Mini Platform tutorial.
function playerJump()
{
if(!$pGuy.airborne)
{
$pGuy.setLinearVelocityY(-80);
$pGuy.airborne = true;
$pGuy.jump = true;
}
}
function playerClass::jump(%this)
{
if(%this.moveLeft)
{
%this.jumpd=1;
}
if(%this.moveRight)
{
%this.jumpd=2;
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.jumpd=3;
}
}
function playerClass::updateHorizontal(%this)
{
if(%this.jumpd==1)
{
%this.setLinearVelocityX(-30);
}
if(%this.jumpd==2)
{
%this.setLinearVelocityX(30);
}
if(%this.jumpd==3)
{
%this.setLinearVelocityX(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(50);
%collision = %this.castCollision(0.005);
if(%collision $= "")
{
%this.airborne = true;
%this.setConstantForceY(50);
if(!%this.jump){
%this.jumpd=3;
}
}
else if(%yVelocity < 0 && %this.airborne)
{
%this.setConstantForceY(50);
}
else
{
if(%yVelocity > 0)
{
%this.airborne = false;
%this.jump = false;
%this.setConstantForceY(0);
}
}
%this.setLinearVelocityY(%yVelocity);
}
function playerClass::updateMovement(%this)
{
%this.jump();
%this.updateHorizontal();
%this.updateVertical();
%this.setCurrentAnimation();
debuger.text = %this.jumpd;
}
Should I rewrite it all from scratch, or is there a way to fix this?
#2
03/19/2007 (2:48 pm)
I don't know how to exactly do that. but I did do thisfunction playerClass::onLevelLoaded(%this, %scenegraph)
{
$pGuy = %this;
moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
moveMap.bindCmd(keyboard, "space", "playerJump();", "");
%force = 20;
sceneWindow2D.mount($pGuy, "0 0", %force, true);
%this.setCollisionMaxIterations(2);
}
function playerLeft()
{
$pGuy.moveLeft = true;
}
function playerLeftStop()
{
$pGuy.moveLeft = false;
}
function playerRight()
{
$pGuy.moveRight = true;
}
function playerRightStop()
{
$pGuy.moveRight = false;
}
function playerJump()
{
$pGuy.jump = true;
}
function playerClass::jump(%this)
{
if(!%this.airborne)
{
if(%this.moveLeft)
{
%this.jumpd=1;
}
if(%this.moveRight)
{
%this.jumpd=2;
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.jumpd=3;
}
if (%this.jump)
{
%this.setLinearVelocityY(-80);
%this.airborne = true;
}
}
}
function playerClass::updateHorizontal(%this)
{
if(%this.jumpd==1)
{
%this.setLinearVelocityX(-30);
}
if(%this.jumpd==2)
{
%this.setLinearVelocityX(30);
}
if(%this.jumpd==3)
{
%this.setLinearVelocityX(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(50);
%collision = %this.castCollision(0.005);
if(%collision $= "")
{
%this.airborne = true;
%this.setConstantForceY(50);
if(!%this.jump){
%this.jumpd=3;
}
}
else if(%yVelocity < 0 && %this.airborne)
{
%this.setConstantForceY(50);
}
else
{
if(%yVelocity > 0)
{
%this.airborne = false;
%this.jump = false;
%this.setConstantForceY(0);
}
}
%this.setLinearVelocityY(%yVelocity);
}
function playerClass::setCurrentAnimation(%this)
{
%xVelocity = %this.getLinearVelocityX();
%yVelocity = %this.getLinearVelocityY();
if(%xVelocity > 0)
{
%this.setFlip(false, false);
}
else if(%xVelocity < 0)
{
%this.setFlip(true, false);
}
if(%this.airborne)
{
if(%yVelocity < 0)
{
%this.playAnimation(playerJumpUp);
}
else
{
%this.playAnimation(playerJumpDown);
}
}
else
{
if(%xVelocity == 0)
{
%this.playAnimation(playerStand);
}
else
{
if(%this.getAnimationName() $= "playerRun")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(playerRun);
}
}
else
{
%this.playAnimation(playerRun);
}
}
}
}
function playerClass::updateMovement(%this)
{
%this.jump();
%this.updateHorizontal();
%this.updateVertical();
%this.setCurrentAnimation();
debuger.text = %this.jumpd;
}
function PlatformerSceneGraph::onUpdateScene()
{
if (isObject($pGuy))
$pGuy.updateMovement();
}
#3
If not, here is an example method based on your playerLeft() function
function playerClass::playerLeft(%this)
{
%this.moveLeft = true;
%this.jump();
}
example usage:
$pGuy.playerLeft;
Now you can have multiple players without the variables getting confused. Also, I wouldn't have a property and method with the same name as you do (e.g. jump).
03/19/2007 (3:02 pm)
Does it work now?If not, here is an example method based on your playerLeft() function
function playerClass::playerLeft(%this)
{
%this.moveLeft = true;
%this.jump();
}
example usage:
$pGuy.playerLeft;
Now you can have multiple players without the variables getting confused. Also, I wouldn't have a property and method with the same name as you do (e.g. jump).
#4
03/19/2007 (4:08 pm)
It still has issues I think I'm going to code from scratch on this. Basicly doing my own scripting on this. But thanks awayway. I'm going to create a new platform tutorial on this, maybe do a diffrent way of gravity and collisions.
Torque Owner Kevin James