How to Make a Jump Function in TGB/iTGB/TGE
by CharlesL · 01/24/2012 (12:54 pm) · 0 comments
Jumping seems to be a common question. It is a common function to have but on the outside it seems rather baffling. In the following tutorial I am going to teach you how to make one for yourself.
The basic idea of it a jump function is as follows:
So the first step is to define the variable and enable collision callback:
Now you have to implement this in this function in the game(this function is different per product, I'm using iTorque)(game.cs):
Hope it helps! Please comment below for help if you need it :)
The basic idea of it a jump function is as follows:
- When the level loads define a variable(we'll call it isJumping) the tells if the character is on the ground.
- When the character collides with the ground isJumping is set to false
- If player taps then make character jump. Set isJumping to true
- When character collides again set isJumping to false
- loop ...
So the first step is to define the variable and enable collision callback:
character::onLevelLoaded(%this, %scenegraph)
{
//This allows collision callback(true/false)
%this.setCollisionCallback(true);
// Make our global isJumping variable
$isJumping = false;
//We call the character later
$character = %this;
}Then we check to see if character is colliding with the ground:character::onCollision(%srcObj, %dstObj, %this)
{
//checks to see what the character is colliding with(so you can jump only when you touch the ground)
if(%dstObj.class $= "ground_class")
{
$isJumping = false;
}
}Finally we write the jump function:function jump()
{
if ($isJumping == false)
{
$isJumping = true;
$character.setLinearVelocityY( -200 );
}
}Now you have to implement this in this function in the game(this function is different per product, I'm using iTorque)(game.cs):
function touchesDown(%touchIDs, %touchesX, %touchesY)
{
echo("Entered touch down function");
// How many fingers are down
%numTouches = getWordCount( %touchIDs );
// Loop through each finger and print its coordinates
for(%count = 0; %count < %numTouches; %count++)
{
if ($gameOver == false){
jump();
} else if ($gameOver == true){
restart();
}
// Finger ID
%curTouch = getWord( %touchIDs, %count );
// X screen coordinate
%curX = getWord( %touchesX, %count );
// Y screen coordinate
%curY = getWord( %touchesY , %count );
// World position
%worldPos = sceneWindow2D.getWorldPoint(%curX, %curY);
// Print results
echo("Scanning touch " @ curTouch @ " at " @ %worldPos);
}
}Hope it helps! Please comment below for help if you need it :)
