Need help creating a "NPC Movement" behavior for a RPG
by Diego Santos Leao - GameBlox Studio · in Torque Game Builder · 03/27/2008 (7:48 am) · 3 replies
I'm creating a top down rpg and I want to add a random movement to some of my npcs (in the future I will add some AI, but for now random would be nice).
The problem is that I don't know the "right way" to do this. I can only think of a behavior that, when added, would start a scheduled event to call "setConstantForce(%X,%Y)" to make the character walk and a "setConstantForce(0,0)" to stop him.
Is there another (better) way?
The problem is that I don't know the "right way" to do this. I can only think of a behavior that, when added, would start a scheduled event to call "setConstantForce(%X,%Y)" to make the character walk and a "setConstantForce(0,0)" to stop him.
Is there another (better) way?
About the author
Creative director at Gameblox Interactive, a game studio focused on downloadable games and advergames. www.gameblox.net - contact@gameblox.net.
#2
06/15/2008 (12:08 am)
Maybe this snippet would be of help? It's the code I used to animate my pets in Grimm's Hatchery.//---------------------------------------------------------------------------------------------
// Moves the pet or monster in a random direction.
//---------------------------------------------------------------------------------------------
function Sprite::moveSprite(%this)
{
%this.direction = getRandom(1, 4);
switch (%this.direction)
{
case 1: //move right
%this.targetX = %this.getPositionX()+10;
%this.targetY = %this.getPositionY();
%this.frame = 0;
case 2: //move left
%this.targetX = %this.getPositionX()-10;
%this.targetY = %this.getPositionY();
%this.frame = 4;
case 3: //move up
%this.targetX = %this.getPositionX();
%this.targetY = %this.getPositionY()-10;
%this.frame = 8;
case 4: //move down
%this.targetX = %this.getPositionX();
%this.targetY = %this.getPositionY()+10;
%this.frame = 12;
case 5: //don't move
%this.targetX = %this.getPositionX();
%this.targetY = %this.getPositionY();
%this.frame = 12;
}
//move sprite
%this.firstFrame = %this.frame;
%this.setTimerOn(%this.timePeriod);
%this.moveTo(%this.targetX, %this.targetY, %this.speed);
}
//---------------------------------------------------------------------------------------------
// TIMER
// Use this timer manager to track events in the sprite class.
//---------------------------------------------------------------------------------------------
function Sprite::onTimer(%this)
{
if ($paused == 0)
{
%this.setTimerOff();
// Move pets
if (%this.time < 3)
{
%this.setFrame(%this.frame);
%this.setTimerOn(%this.timePeriod);
%this.frame++;
%this.time++;
}
else pause
{
%this.time = 0;
%this.setFrame(%this.firstFrame);
%this.moveSprite();
}
}
}
#3
I solved this issue with the moveTo (as suggested by Mike - Thanks mike!) function triggered by a scheduled event. I didn't knew about the onTimer callback, and will use it the next time I need something similar :) Thanks for your answer and would also notice that I am a fan of your work, though I only played the demo of your game :) Unfortunatelly I don't have the time to play all of it (I have a 'day job') :(
08/09/2008 (6:09 pm)
I just saw your answer amaranthia :)I solved this issue with the moveTo (as suggested by Mike - Thanks mike!) function triggered by a scheduled event. I didn't knew about the onTimer callback, and will use it the next time I need something similar :) Thanks for your answer and would also notice that I am a fan of your work, though I only played the demo of your game :) Unfortunatelly I don't have the time to play all of it (I have a 'day job') :(
Associate Mike Lilligreen
Retired T2Der