Game Development Community

player code for moving- doesnt work

by David bailey · in Technical Issues · 05/21/2011 (9:55 am) · 5 replies

function HERO::onLevelLoaded(%this, %scenegraph)
{
//set the player's ship name to the instance
$herosquirrel = %this;


moveMap.bindCmd(keyboard, "w", "herosquirrelUp();", "herosquirrelUpStop();");
moveMap.bindCmd(keyboard, "s", "herosquirrelDown();", "herosquirrelDownStop();");
moveMap.bindCmd(keyboard, "a", "herosquirrelLeft();", "herosquirrelLeftStop();");
moveMap.bindCmd(keyboard, "d", "herosquirrelRight();", "herosquirrelRightStop();");
}

function playerSQ::updateMovement(%this)
{
if(%this.moveLeft)
{
%this.setLinearVelocityX( -$herosquirrel.hSpeed );
}


if(%this.moveRight)
{
%this.setLinearVelocityX( $herosquirrel.hSpeed );
}
if(%this.moveUp)
{
%this.setLinearVelocityY( -$herosquirrel.vSpeed );
}
if(%this.moveDown)
{
%this.setLinearVelocityY( $herosquirrel.vSpeed );
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX( 0 );
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY( 0 );
}
}


function herosquirrelUp()
{
$herosquirrel.moveUp = true;
$herosquirrel.updateMovement();
}


function herosquirrelDown()
{
$herosquirrel.moveDown = true;
$herosquirrel.updateMovement();
}


function herosquirrelLeft()
{
$herosquirrel.moveLeft = true;
$herosquirrel.updateMovement();
}


function herosquirrelRight()
{
$herosquirrel.moveRight = true;
$herosquirrel.updateMovement();
}


functionherosquirrelLeftStop()
{
$herosquirrel.moveLeft = false;
$herosquirrel.updateMovement();
}


functionherosquirrelRightStop()
{
$herosquirrel.moveRight = false;
$herosquirrel.updateMovement();
}


function herosquirrelUpStop()
{
$herosquirrel.moveUp = false;
$herosquirrel.updateMovement();
}


function herosquirrelDownStop()
{
$herosquirrel.moveDown = false;
$herosquirrel.updateMovement();
}

#1
05/22/2011 (4:15 am)
You are defining this in HERO::onLevelLoaded

$herosquirrel = %this;

You likely need to define that in playerSQ::onLevelLoaded but that is just a guess. Make sure you are also exec("..."); your script.
#2
05/22/2011 (3:19 pm)
[IMG]http://i93.photobucket.com/albums/l74/spawn85027/help-1.jpg[/IMG]

I just want to punch my keyboard, I am no programmer. Any reason why this wouldnt work? Just trying to test this programming stuff out by getting this squirrel to move to the Right.
#3
05/22/2011 (4:14 pm)
<a href="http://s93.photobucket.com/albums/l74/spawn85027/?action=view&amp;current=help-1.jpg" target="_blank"><img src="http://i93.photobucket.com/albums/l74/spawn85027/help-1.jpg" border="0" alt="Photobucket"></a>
#4
05/22/2011 (5:58 pm)
Under SCRIPTING the name is HERO and under CLASS its Playerhero
This is my Player.cs

function PlayerHero::onLevelLoaded(%this, %scenegraph)
{
$Hero = %this;

moveMap.bindCmd(keyboard, "d", "HeroRight();", "HeroRightStop();");


}




function HeroRight()
{
$Hero.setLinearVelocityX( 30 );
}



#5
05/22/2011 (8:09 pm)
got it to work.....