Turning Fish Game into a Shooter?
by Samarie · in Torque Game Builder · 01/31/2011 (7:31 am) · 3 replies
Hey guys,
I wanted to turn the fish game tutorial into a shooter. The fish will shoot missiles out of their mouth to kill the other fish. I wanted to use the space shooter example like the one below,

but the problem I ran into is the missiles and space ships are moving in one direction, where as the fish are swimming in different directions, swimming to the left and then swimming back to the right. How do I make the missiles shoot in the direction that the fish is swimming, so when the fish swims in another direction the missile is shooting in the direction the fish is facing. Is there a code example out there? Does anyone know how to make this possible? Your help would be greatly appreciated thanks.

I wanted to turn the fish game tutorial into a shooter. The fish will shoot missiles out of their mouth to kill the other fish. I wanted to use the space shooter example like the one below,

but the problem I ran into is the missiles and space ships are moving in one direction, where as the fish are swimming in different directions, swimming to the left and then swimming back to the right. How do I make the missiles shoot in the direction that the fish is swimming, so when the fish swims in another direction the missile is shooting in the direction the fish is facing. Is there a code example out there? Does anyone know how to make this possible? Your help would be greatly appreciated thanks.

About the author
I'm an undergraduate student studying game and simulation programming at a university.
#2
P.S. Is it possible to implement this with the character in the TGB Platformer Mechanics Tutorial? Kind of like the Super NES Contra games, an enemy is coming at you from the back and you turn around to shoot them. I'm not really sure where I would place the code in the Platformer Mechanics tutorial since it isn't set up like the fish game. In the Platformer, I'd have the laser coming out of his hand.

Here's the link:
tdn.garagegames.com/wiki/TGB/Tutorials/Platformer/Player
Any help would be greatly appreciated. Thanks!
02/02/2011 (5:51 am)
Awesome! Thanks! It's shooting in both directions. But, I noticed that the only time the laser fires is when the fish is not moving. P.S. Is it possible to implement this with the character in the TGB Platformer Mechanics Tutorial? Kind of like the Super NES Contra games, an enemy is coming at you from the back and you turn around to shoot them. I'm not really sure where I would place the code in the Platformer Mechanics tutorial since it isn't set up like the fish game. In the Platformer, I'd have the laser coming out of his hand.

Here's the link:
tdn.garagegames.com/wiki/TGB/Tutorials/Platformer/Player
Any help would be greatly appreciated. Thanks!
#3
[quote]But, I noticed that the only time the laser fires is when the fish is not moving. /quote]
Your inputs may be fighting each other. Can you post your code for binding a key to firing and your actual firing code? Do the same for at least one direction key to show how you are making the fish move. This way I can see if the inputs are overwriting each other.
Yes, it's possible to implement this into the platformer. The firing code should port over just fine, so long as you are linking all the names/classes the same way. If you want to modify the base code from the platformer tutorial, you create the moveMap binding in playerClass::onLevelLoaded in game.cs. You will also add the playerShoot() function to game.cs. Remember, the platformer does not use behaviors.
As for the positioning of the laser, you will want to change the projectile start position using some kind of offset. You can do so via trial and error or use some kind of mountPoint as an indicator. For example, let's say you are creating the projectile in the same position as your player:
Pseudo code
That will create the projectile in the middle of the player. Now, let's say you get the position of the player's hand via a mountPoint or trial/error. Just use that:
Pseudo code
I don't have Torque 2D in front of me, so I can only provide pseudo code. Try wrapping the concept into your actual code. If you need more specifics, just let me know.
02/02/2011 (7:44 am)
@Samarie - [quote]But, I noticed that the only time the laser fires is when the fish is not moving. /quote]
Your inputs may be fighting each other. Can you post your code for binding a key to firing and your actual firing code? Do the same for at least one direction key to show how you are making the fish move. This way I can see if the inputs are overwriting each other.
Yes, it's possible to implement this into the platformer. The firing code should port over just fine, so long as you are linking all the names/classes the same way. If you want to modify the base code from the platformer tutorial, you create the moveMap binding in playerClass::onLevelLoaded in game.cs. You will also add the playerShoot() function to game.cs. Remember, the platformer does not use behaviors.
As for the positioning of the laser, you will want to change the projectile start position using some kind of offset. You can do so via trial and error or use some kind of mountPoint as an indicator. For example, let's say you are creating the projectile in the same position as your player:
Pseudo code
%position = player.GetPosition(); %projectile = createProjectile(%position);
That will create the projectile in the middle of the player. Now, let's say you get the position of the player's hand via a mountPoint or trial/error. Just use that:
Pseudo code
%position = player.GetPosition(); %offset = player.GetOffset(); %newPosition = %position + %offset; %projectile = createProjectile(%newPosition);
I don't have Torque 2D in front of me, so I can only provide pseudo code. Try wrapping the concept into your actual code. If you need more specifics, just let me know.
Employee Michael Perry
ZombieShortbus
First and foremost, you want to decide what direction the laser will be going. Let's say by default your fish is facing right (both in game and due to art setup). You would want your laser to shoot toward the right:
function fishFire(%fishObject) { // Fish is not flipped, which means it is facing right if(%fishObject.FlipX == 0) { %laser = createLaser(); // pseudo code %laser.setLinearVelocityX(30); // Fires to the right } }Now, you can add on to that based on the value FlipX being true:
function fishFire(%fishObject) { // If FlipX is false, fish is facing right // Otherwise, fish is facing left if(%fishObject.FlipX == 0) { %laser = createLaser(); // pseudo code %laser.setLinearVelocityX(30); // Fires to the right } else { %laser = createLaser(); // pseudo code %laser.setLinearVelocityX(-30); // Fires to the left } }You see I do not go into the actual projectile creation, but that is covered in the BehaviorShooter project. What needs to be adjusted is where the projectile starts. This requires an offset value, which is also shown in the BehaviorShooter shootsAdvBehavior. You might have to tweak this value based on the FlipX as well, but that is trivial to do.
Hope that gets you going. When TDN is back up, I will see if I can find an exact tutorial.