making a co-op game
by Aleksandar Sandic · in Torque Game Builder · 07/04/2010 (4:40 am) · 3 replies
Hi
I was following the platformer tuorial
tdn.garagegames.com/wiki/TGB/Tutorials/Platformer
And I thought, it would be a good exercise, to try and make it into a two-player co-op game. So I made a grey version of the ninja, set its class to "PlayerClass" (same as the purple ninja), and its name to "player2" (purple ninja is still just "player")

Then I made a few adjustments to the source code. The original code assigned the player the global variable $player
Obviously, this would make trouble, so I made the script check each ninja's name and then assign it to either $player or $player2
As far as I understand, this should work. Keyboard inputs are handled for $player, so the grey ninja should not do anything, besides standing still once he touches the floor.
But instead he falls right through the floor, while the purple ninja does not do anything. he does not even respond to pressing a button (he can still fall, but he does not go though floors. Oh, never mind, the falling - I forgot to set up the collision properties for grey ninja. But still no response to button pressing from either ninja.
So the next thing I tried was commenting away my modifications

I expected both ninjas now to move simultaneously (since they are both $player), however, purple ninja just tries walking but stays at the same place, while grey ninja can move around, but ends up jumping infinitely high.
Once I delete the grey guy everything is back to normal (no wonder, all modifications are gone after all)
So, where is the problem?
Never mind the grey ninja turning purple after a movement, I wanted to get the mechanics right, before fixing the animation.
EDIT
Ok, when I don't comment away my changes in source code, but have only one ninja, that ninja (purple) does not respond to any key commands. So my guess is, that if(%this.Name $= "player") is wrong syntax.
Can anyone please tell me how to express this properly?
I was following the platformer tuorial
tdn.garagegames.com/wiki/TGB/Tutorials/Platformer
And I thought, it would be a good exercise, to try and make it into a two-player co-op game. So I made a grey version of the ninja, set its class to "PlayerClass" (same as the purple ninja), and its name to "player2" (purple ninja is still just "player")

Then I made a few adjustments to the source code. The original code assigned the player the global variable $player
function playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
[...]
}Obviously, this would make trouble, so I made the script check each ninja's name and then assign it to either $player or $player2
function playerClass::onLevelLoaded(%this, %scenegraph){
if(%this.Name $= "player"){
$player = %this;
}else if(%this.Name $= "player2"){
$player2 = %this;
}
[...]
}As far as I understand, this should work. Keyboard inputs are handled for $player, so the grey ninja should not do anything, besides standing still once he touches the floor.
But instead he falls right through the floor, while the purple ninja does not do anything. he does not even respond to pressing a button (he can still fall, but he does not go though floors. Oh, never mind, the falling - I forgot to set up the collision properties for grey ninja. But still no response to button pressing from either ninja.
So the next thing I tried was commenting away my modifications

I expected both ninjas now to move simultaneously (since they are both $player), however, purple ninja just tries walking but stays at the same place, while grey ninja can move around, but ends up jumping infinitely high.
Once I delete the grey guy everything is back to normal (no wonder, all modifications are gone after all)
So, where is the problem?
Never mind the grey ninja turning purple after a movement, I wanted to get the mechanics right, before fixing the animation.
EDIT
Ok, when I don't comment away my changes in source code, but have only one ninja, that ninja (purple) does not respond to any key commands. So my guess is, that if(%this.Name $= "player") is wrong syntax.
Can anyone please tell me how to express this properly?
#2
07/04/2010 (9:26 am)
Also, make sure you set up all the stuff in the collision tab for the second guy (Figure 2.3 from the tutorial page about the player)... that might solve your falling through the floor issues.
#3
I also fixed the animation by adding a dynamic field to each player, that stores the ninja's colour. Then I changed the names of each animation to include the colour at the end (e.g.. playerRunAnimationGrey). Now the setCurrentAnimation function simply picks the proper colour itself
Now I can try adding enemies o the game.
07/05/2010 (1:14 am)
Thanks, I'll keep it in mind. But it turns out, i don't even need to store the object in a variable. I can just as well simply refer to it by its name. So if i want to issue a command for player or player2, I can just writefunction player2Left(){
player2.moveLeft = true;
}I also fixed the animation by adding a dynamic field to each player, that stores the ninja's colour. Then I changed the names of each animation to include the colour at the end (e.g.. playerRunAnimationGrey). Now the setCurrentAnimation function simply picks the proper colour itself
[...]
if (%this.airborne){
if(%yVelocity < 0){
%this.playAnimation("PlayerJumpUpAnimation" @ %this.colour);
return;
}else{
%this.playAnimation("PlayerJumpDownAnimation" @ %this.colour);
return;
}
}
[...]I took this idea from the whack-a-mole tutorial :)Now I can try adding enemies o the game.
Torque 3D Owner Tim Scheiman
Ghost Ship Studios