Game Development Community

Creating an animated sprite from scratch

by Arden · in Torque Game Builder · 08/18/2010 (8:56 pm) · 2 replies

Hiya, I'm trying to create an animated sprite from code when a level is loaded and I can't figure out what I'm doing wrong. When I press the up key, it's not detected and I don't get the "up pressed" message.

Any help would be appreciated.
Thx!


echo ("STARTING SOLDIER.CS...!");
%soldier = new t2dAnimatedSprite()
{
scenegraph = %this.scenegraph;
config = "soldierDataBlock";
};

%this=%soldier

%soldier.setPosition(-38.643, 12.288);
echo ("Sprite created!");

function Soldier::onLevelLoaded(%this, %scenegraph)
{
$theSoldier = %this;

moveMap.bindObj(keyboard, "w", "Up", %this);
moveMap.bindObj(keyboard, "s", "Down", %this);
moveMap.bindObj(keyboard, "a", "Left", %this);
moveMap.bindObj(keyboard, "d", "Right", %this);
moveMap.bindObj(keyboard, "enter", "Enter", %this);

%gold = 0;
echo ("Keys bound!");
}

function Soldier::Up(%this, %pressed)
{
if ( %pressed )
{
echo ("Up key pressed!");
%this.setLinearVelocityY(-%this.vSpeed);
%this.setAnimation("Archer_walk_northAnimation");
}
else
{
%this.setLinearVelocityY(0);
}
}

function Soldier::Down(%this, %pressed)
{
if ( %pressed )
{
%this.setLinearVelocityY(%this.vSpeed);
}
else
{
%this.setLinearVelocityY(0);
}
}

function Soldier::Left(%this, %pressed)
{
if ( %pressed )
{
%this.setLinearVelocityX(-%this.hSpeed);
}
else
{
%this.setLinearVelocityX(0);
}
}

function Soldier::Right(%this, %pressed)
{
if ( %pressed )
{
%this.setLinearVelocityX(%this.hSpeed);
}
else
{
%this.setLinearVelocityX(0);
}
}

function Soldier::Enter(%this, %pressed)
{
if ( %pressed )
{
echo("Grenade!!!");
}
}

#1
08/19/2010 (6:08 am)
The fourth parameter for the movemap is a description, not the object. Movemaps are globa objects, not responding for a specific object. But what you can do is to make the function call in the third parameter something like "Soldier.Up()". See TDN for examples when searching for movemap.
#2
08/19/2010 (9:34 pm)
Got it. Thanks for the tip Ronny.