Adding enemies to platformer
by Joanne Hok · in Torque Game Builder · 04/16/2012 (5:39 pm) · 6 replies
So I'm going off the Ninja Platformer tutorial and I just want to add an enemy pacing back and forth (like a koopa in mario)... and I don't know where to start. I've looked at the shooter tutorial to figure out how to create an enemy, but it doesn't help... it won't even move. Here are the tutorials I'm talking about: docs.garagegames.com/tgb/official/ . Google hasn't really helped either. Help!
#2
I also spoke to my teacher about it and he says I can create a path and also apply collisions to the enemy object. Any confirmations of this? And is there a skeleton code I can get somewhere?
04/17/2012 (5:12 pm)
I did that exact code too. Still doesn't seem to work, and I followed the shooter tutorial step by step up until that point and the example shows that it should be moving, but it doesn't...I also spoke to my teacher about it and he says I can create a path and also apply collisions to the enemy object. Any confirmations of this? And is there a skeleton code I can get somewhere?
#3
First, to clarify. A 2d platformer, enemy just walks back and forth on a single ledge.
1) Triggers / pseudo-triggers ( blocks which may be invisible ): Use the onEnter or onCollision callbacks and when called multiply the velocity by a -1.
2) Behavior / Code: Put the enemy where you want the start to be, program in a distance variable. When the enemy goes that distance, turn them around and multiply the velocity by a -1.
3) Path: Set the very simple 2 point path. Attach the enemy, have it use the Follow Mode "Wrap".
Honestly, there may be even more options to do this. Those are three or four distinct ones that came to mind for me.
This is an example of using a Psuedo-trigger. I've had troubles using triggers in the past, so I don't have the code for them memorized off-hand. For this to work you'd have to put an object with the class "TurnAroundBlock" where you want the Koopa to turn around at. Pretty simple.
......K.......
~~B~~~~~~~B~~~
Doing a very similar thing with the "onEnter" callback of a trigger should work as well though.
04/17/2012 (11:04 pm)
I can think of several ways to implement this.First, to clarify. A 2d platformer, enemy just walks back and forth on a single ledge.
1) Triggers / pseudo-triggers ( blocks which may be invisible ): Use the onEnter or onCollision callbacks and when called multiply the velocity by a -1.
2) Behavior / Code: Put the enemy where you want the start to be, program in a distance variable. When the enemy goes that distance, turn them around and multiply the velocity by a -1.
3) Path: Set the very simple 2 point path. Attach the enemy, have it use the Follow Mode "Wrap".
Honestly, there may be even more options to do this. Those are three or four distinct ones that came to mind for me.
This is an example of using a Psuedo-trigger. I've had troubles using triggers in the past, so I don't have the code for them memorized off-hand. For this to work you'd have to put an object with the class "TurnAroundBlock" where you want the Koopa to turn around at. Pretty simple.
......K.......
~~B~~~~~~~B~~~
function Koopa::onLevelLoaded(%this, %level)
{
%this.speed = 20;
%this.setLinearVelocityX( %this.speed );
}
function Koopa:onCollision(%srcObj, %dstObj, /*the others that I don't remember*/)
{
if(%dstObj.class $= "TurnAroundBlock")
{
%srcObj.setLinearVelocityX( %srcObj.getLinearVelocityX * -1 );
// insert code to flip image to match direction.
}
}Doing a very similar thing with the "onEnter" callback of a trigger should work as well though.
#4
I recommend you to go through it , it is a very simple and quick one.
It uses onWorldLimit with switch$(%limit).
Here is what I have for my scorpions:
04/19/2012 (11:05 am)
There is a good example of this in fish tutorial.I recommend you to go through it , it is a very simple and quick one.
It uses onWorldLimit with switch$(%limit).
Here is what I have for my scorpions:
function Scorpion04::onWorldLimit(%this, %mode, %limit)
{
switch$(%limit)
{
case "left":
%this.setFlipX(false);
%this.setLinearVelocityPolar(92, %this.getSpeed());
case "right":
%this.setFlipX(true);
%this.setLinearVelocityPolar(92, -%this.getSpeed());
}
}
#5
Here's another issue I'm running into: whenever an enemy makes contact with the player, it dies. So if say the player character is attacking in the wrong direction and its butt touches the enemy, it'll still die.
I know the shooter tutorial deals with player contacting the enemy and the enemy dies and the player takes a bit of damage. How would I differentiate between the enemy touching the player and the player attacking the enemy? (other than using projectiles)
...I think I'm just over thinking it, and I'm looking over something super simple.
04/22/2012 (6:23 pm)
Oh wow, thank you guys! I got him pacing back and forth now.Here's another issue I'm running into: whenever an enemy makes contact with the player, it dies. So if say the player character is attacking in the wrong direction and its butt touches the enemy, it'll still die.
I know the shooter tutorial deals with player contacting the enemy and the enemy dies and the player takes a bit of damage. How would I differentiate between the enemy touching the player and the player attacking the enemy? (other than using projectiles)
...I think I'm just over thinking it, and I'm looking over something super simple.
#6
I don't recall off-hand if the direction an object is facing is kept track of, but it is an easy variable to add into the scripting.
04/23/2012 (9:18 am)
You could compare the X locations of the Player and the Enemy. That way you know which side the Enemy is contacting the Player from. After that, to determine if the player is attacking, or being hit, look at what direction the player is facing.I don't recall off-hand if the direction an object is facing is kept track of, but it is an easy variable to add into the scripting.
Torque Owner Gofacial
Gofworks
All I did to get my enemies to move is call my enemyMovement() function which just does this:
%this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
I think this is how it is handled in the shooter Tutorial. There is probably a problem somewhere else in your script or in your fields in TGB that is interfering with this script.