Game Development Community

Tutorial Question: PlayerUp()

by Brian A. · in Torque Game Builder · 02/28/2005 (8:42 pm) · 26 replies

Was doing the tutorial and have run into some problems... after defining the action map, I entered the function playerUp/playerUpStop. When I run it, the "w" key does not make the player to move up. The tutorial said to enter the code BELOW the "setupT2DScene() function. Did I do this in the right place?

(something helpfull: include the final source code in the end of Tutorial so you can compare all the code (for newbees :) )

Here is the code... any ideas? Thanks.

datablock fxImageMapDatablock2D(playershipImageMap)
 {    
 mode = full;    
 textureName = "~/client/images/playerShip"; 
 }; 

$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; }; 
$player.setPosition("-35 0"); 
$player.setSize( "14 7" ); 
$player.setImageMap( playershipImageMap ); 
	
	
	
// set up the enemy
 datablock fxImageMapDatablock2D(enemyship1ImageMap)
	{
	mode = full;
	textureName = "~/client/images/enemyship1";
	};
	
%enemy = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
%enemy.setSize ("14 7");
%enemy.setPosition("40 0");
%enemy.setImageMap (enemyship1ImageMap);

//move the enemy towards us
%enemy.setLinearVelocityX(-20);


// Bind Keys to actions
playerMap.bindCmd(keyboard, "w", "playerUp();", "playerUpStop();"); 
playerMap.bindCmd(keyboard, "s", "playerDown();", "playerDownStop();"); 
playerMap.bindCmd(keyboard, "a", "playerLeft();", "playerLeftStop();"); 
playerMap.bindCmd(keyboard, "d", "playerRight();", "playerRightStop();"); 
playerMap.bindCmd(keyboard, "space", "playerFire();", ""); 

// Activate the action map. 
playerMap.push(); 
	
	//
	// ************************************************************************
		
}

function playerUp()
 {    
 // Set the player moving up.    
 $player.setLinearVelocityY( - ); 
 } 
 
 function playerUpStop()
  {    
  $player.setLinearVelocityY( 0 ); 
  }
Page «Previous 1 2
#1
02/28/2005 (8:46 pm)
Ah.. You got it all right. There's a bug in the tutorial doc. Change -$10 to -10 and you should be all set.

BUT, before you do, you should learn about the console and the script errors it reports. Double-click the T2D icon and hit "~". The console will appear. Using the scroll bars, scroll up until you see red text. There's the script error!
#2
02/28/2005 (8:48 pm)
Here's my final code from the tutorial:

//-----------------------------------------------------------------------------
// Torque 2D. 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

// --------------------------------------------------------------------
// Initialise Client.
// --------------------------------------------------------------------
function initialiseClient()
{
	// Initialise Base Client.
	InitBaseClient();
	
	// Key-Bindings.
	GlobalActionMap.bind(keyboard, tilde, ToggleConsole);
	GlobalActionMap.bindCmd(keyboard, "alt enter", "", "toggleFullScreen();");
	GlobalActionMap.bindCmd(keyboard, "q", "", "quit();");
		
	// Initialise Canvas.
	InitCanvas("T2D");

	
	// ************************************************************************
	// Load-up Demo Datablocks.
	// NOTE:-	Remove this is you're not interested in running the demos
	//			or any or the default datablocks.
	// ************************************************************************
	exec("./demoDatablocks.cs");
	
	
	
	// Load-up Datablocks.	
	exec("./datablocks.cs");
	// Load-up GUIs.
	exec("./mainScreenGui.gui");
	
	// Set GUI.
	Canvas.setContent(mainScreenGui);
	// Set Cursor.
	Canvas.setCursor(DefaultCursor);
	
   // Setup Scene.
	setupT2DScene();
}


// --------------------------------------------------------------------
// Destroy Client.
//
// Here we destroy the SceneGraph.
// --------------------------------------------------------------------
function destroyClient()
{
	// Destroy fxSceneGraph2D.
	if ( isObject(t2dSceneGraph) )
		t2dSceneGraph.delete();	
}


// --------------------------------------------------------------------
// Setup T2D Scene.
// --------------------------------------------------------------------
function setupT2DScene()
{
	// Create fxSceneGraph2D.
	new fxSceneGraph2D(t2dSceneGraph);
	
	// Associate Scenegraph with Window.
	sceneWindow2D.setSceneGraph( t2dSceneGraph );
	
	// Set Camera Position to be centered on (0,0) with
	// view width/height of (100/80).
	sceneWindow2D.setCurrentCameraPosition( "0 0 100 75" );

	//======================== Add my code here =============================
	SetupImages();

	CreatePlayer();	

	CreateEnemy();
	
	CreateTileMap();
}

function SetupImages()
{
	datablock fxImageMapDatablock2D(playershipImageMap)
	{
		mode = full;
		textureName = "~/client/images/playerShip";
	};

	datablock fxImageMapDatablock2D(playermissileImageMap)
	{
		mode = full;
		textureName = "~/client/images/playerMissile";
	};

	datablock fxImageMapDatablock2D(enemyship1ImageMap)
	{
		mode = full;
		texturename = "~/client/images/enemyship1";
	};

  datablock fxImageMapDatablock2D(enemymissileImageMap)
  {
  	mode = full;
  	textureName = "~/client/images/enemyMissile";
  };

	// Create images for the tilemap
	datablock fxImageMapDatablock2D(bgBlankSkyImageMap)
	{
		mode = full;
		textureName = "~/client/images/bg_blank_sky";
	};

	datablock fxImageMapDatablock2D(bgCloud1ImageMap)
	{
		mode = full;
		textureName = "~/client/images/bg_cloud_1";
	};

	datablock fxImageMapDatablock2D(bgCloud2ImageMap)
	{
		mode = full;
		textureName = "~/client/images/bg_cloud_2";
	};

	datablock fxImageMapDatablock2D(bgCloud3aImageMap)
	{
		mode = full;
		textureName = "~/client/images/bg_cloud_3a";
	};

	datablock fxImageMapDatablock2D(bgCloud3bImageMap)
	{
		mode = full;
		textureName = "~/client/images/bg_cloud_3b";
	};
}
#3
02/28/2005 (8:50 pm)
Wow... did not know there was script error report in the console! Nice... thanks for the help--been pounding my head on this for a while.
#4
02/28/2005 (8:50 pm)
And...

function CreateTileMap()
{
	// Create tile-map.
	%scrollerMap = new fxTileMap2D() { scenegraph = t2dSceneGraph; };
	
	// Load saved tile-map file.
	%scrollerMap.loadTileMap("~/client/maps/scroller.map");

	// Load tile layer from tile-map
	%skyLayer = %scrollerMap.getTileLayer(0);

	// Generate Scrolling Sky, re-size to fill the whole screen
	%skyLayer.setPosition("0 -10");
	%skyLayer.setTileSize("25 25");
	%skyLayer.setWrap(true, false);
	%skyLayer.setAutoPan("10 0");
}

function CreatePlayer()
{
	$player = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	$player.setPosition("-35 0");
	$player.setSize("14 7");
	$player.setImageMap(playershipImageMap);
	$player.fireLinkPoint = $player.addLinkPoint("0.45 0.2");
	$player.setWorldLimit(bounce, "-49 -37 40 37");

	//player collisions
	$player.setGroup(1);
	$player.setLayer(1);
	$player.setCollisionActive(true, true);
	$player.setCollisionMaterial(standardMaterial);
	$player.setCollisionPolyCustom(4, "-1 0 -0.1 -0.6 0.98 0.15 -0.1 0.7");
	$player.setCollisionMasks(BIT(2), BIT(2));
	$player.setCollisionCallback(true);
	
	attachThruster($player, "-0.12 -0.33", 0);

  new ActionMap(playerMap);
  playerMap.bindCmd(keyboard, "w", "playerUp();", "playerUpStop();");
  playerMap.bindCmd(keyboard, "s", "playerDown();", "playerDownStop();");
  playerMap.bindCmd(keyboard, "a", "playerLeft();", "playerLeftStop();");
  playerMap.bindCmd(keyboard, "d", "playerRight();", "playerRightStop();");
  playerMap.bindCmd(keyboard, "space", "playerFire();", "");
  playerMap.push();
}

function CreateEnemy()
{
	%enemy = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; } ;
	%enemy.setSize("14 7");
	%enemy.setPosition("40" SPC (-30 + (getRandom() * 60)));
	%enemy.setImageMap(enemyship1ImageMap);
	%enemy.setWorldLimit(kill, "-75 -40 60 40");
	%enemy.setLinearVelocityX(-20);

	%enemy.setGroup(2);
	%enemy.setLayer(2);
	%enemy.setCollisionActive(true, true);
	%enemy.setCollisionMaterial(standardMaterial);
	%enemy.setCollisionPolyCustom(5, "-0.9 0 0 -0.6 1 -0.3 1 0.3 0 0.5");
	%enemy.setCollisionMasks(BIT(1), BIT(1));
	%enemy.setCollisionCallback(true);
  
	attachThruster(%enemy, "0.8 -0.12", 180);
	attachThruster(%enemy, "0.8 0.18", 180);
  
	%enemyFire = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	%enemyFire.setPosition(%enemy.getPosition());
	%enemyFire.setSize("3 1.5");
	%enemyFire.setImageMap(enemymissileImageMap);
	%enemyFire.setLinearVelocityX(-35);
	%enemyFire.setWorldLimit(kill, "-60 -40 60 40");

	%enemyFire.setGroup(2);
	%enemyFire.setLayer(2);
	%enemyFire.setCollisionActive(true, false);
	%enemyFire.setCollisionMaterial(standardMaterial);
	%enemyFire.setCollisionScale("0.9 0.5");
	%enemyFire.setCollisionMasks(BIT(1), BIT(1));
	%enemyFire.setCollisionCallback(true);

	schedule(2000, 0, "CreateEnemy");
}

function attachThruster(%mountObj, %mountPosition, %angle)
{
	// Create Player Thruster.
	%thruster = new fxParticleEffect2D() { scenegraph = t2dSceneGraph; };
	%thruster.loadEffect("~/client/effects/smallThruster.eff");
	%thruster.mount( %mountObj, %mountPosition, 0, false );
	%thruster.setRotation( %angle );
	%thruster.playEffect();
}

function createExplosion( %object )
{
	// Ignore if object not around anymore.
	if ( !isObject(%object) )
		return;

	// Shockwave Explosion.
	%explosion = new fxParticleEffect2D() { scenegraph = t2dSceneGraph; };
	%explosion.loadEffect("t2d/client/effects/shockwave_burst.eff");
	%explosion.setPosition( %object.getPosition() );
	%explosion.setEffectLifeMode( kill, 0.1 );
	%explosion.playEffect();
}
#5
02/28/2005 (8:50 pm)
And finally...

function playerUp()
{
	$player.setLinearVelocityY(-10);
}

function playerUpStop()
{
	if ($player.getLinearVelocityY() < 0)
		$player.setLinearVelocityY(0);
}

function playerDown()
{
	$player.setLinearVelocityY(10);
}

function playerDownStop()
{
	if ($player.getLinearVelocityY() > 0)
		$player.setLinearVelocityY(0);
}

function playerLeft()
{
	$player.setLinearVelocityX(-10);
}

function playerLeftStop()
{
	if ($player.getLinearVelocityX() < 0)
		$player.setLinearVelocityX(0);
}

function playerRight()
{
	$player.setLinearVelocityX(10);
}

function playerRightStop()
{
	if ($player.getLinearVelocityX() > 0)
		$player.setLinearVelocityX(0);
}

function playerFire()
{
	%projectile = new fxStaticSprite2D() { scenegraph = t2dSceneGraph; };
	%projectile.setPosition($player.getLinkPoint($player.fireLinkPoint));
	%projectile.setSize("3 1.5");
	%projectile.setImageMap(playermissileImageMap);
	%projectile.setWorldLimit(kill, "-52 -40 52 40");
	%projectile.setLinearVelocityX(35);

	//projectile collisions
	%projectile.setGroup(1);
	%projectile.setLayer(1);
	%projectile.setCollisionActive(true, false);
	%projectile.setCollisionMaterial(projectileMaterial);
	%projectile.setCollisionScale("0.9 0.5");
	%projectile.setCollisionMasks(BIT(2), BIT(2));
	%projectile.setCollisionCallback(true);
}

function KillPlayer()
{
	$player.setVisible(false);
	playerMap.pop();
	schedule(2000, 0, "ResetPlayer");
}

function ResetPlayer()
{
	$player.setPosition("-35 0");
	$player.setVisible(true);
	playerMap.push();
}

function fxSceneObject2D::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
	createExplosion(%dstObj);
	
	if (%srcObj == $player)
	{
		KillPlayer();
		%dstObj.safeDelete();
	}
	else if (%dstObj == $player)
	{
		KillPlayer();
		%srcObj.safeDelete();
	}
	else
	{
		%srcObj.safeDelete();
		%dstObj.safeDelete();
	}
}
#6
02/28/2005 (9:08 pm)
THANK YOU!!!!!! (that will help).

Another question... after making the change "-$10 to -10" and ran the program it still did not work. When I checked the console it was no longer red, however I notice the following in light grey:

T2D/client/client.cs (110): Unable to find object: 'playerMap' attempting to call function 'bindCmd'

Thanks.
#7
02/28/2005 (9:16 pm)
Check if your missing
new ActionMap(playerMap);

before
playerMap.bindCmd(keyboard, "w", "playerUp();", "playerUpStop();"); 
playerMap.bindCmd(keyboard, "s", "playerDown();", "playerDownStop();"); 
playerMap.bindCmd(keyboard, "a", "playerLeft();", "playerLeftStop();"); 
playerMap.bindCmd(keyboard, "d", "playerRight();", "playerRightStop();"); 
playerMap.bindCmd(keyboard, "space", "playerFire();", ""); 
// Activate the action map. 
playerMap.push();
#8
02/28/2005 (9:19 pm)
Btw a good place to keep up on is the Bug area... the bug you initially ran into I did as well and I posted a bug report :)


www.garagegames.com/mg/forums/result.thread.php?qt=26440


A great thread to follow is

www.garagegames.com/mg/forums/result.thread.php?qt=26493

some good advice from people who've worked with Torquescript.... I posted a suggestion about checking for the red text as well as others with some very useful things :) best of luck and feel free to ask any questions you need.
#9
02/28/2005 (9:34 pm)
New ActionMap(playerMap);

THAT WAS IT! It works.... thank u for the info and help!
#10
03/01/2005 (12:35 am)
Guys,

Thank you for helping each other out on this. I sort of got here late in the day.

Awesome.

- Melv.
#11
03/01/2005 (6:16 pm)
Just to note, we will be updating the tutorial doc, and also releasing the finalized script that it works with on the next T2D update. :)
#12
08/09/2005 (8:24 am)
Hi everyone, I'm a newbie here and I'm havig some trouble right at the begining of the T2d tutorial. I can't get the enemy spaceship to move to the left. Here is my code so far:

function setupT2DScene()
{
// Create fxSceneGraph2D.
new fxSceneGraph2D(t2dSceneGraph);

// Associate Scenegraph with Window.
sceneWindow2D.setSceneGraph( t2dSceneGraph );

// Set Camera Position to be centered on (0,0) with
// view width/height of (100/80).
sceneWindow2D.setCurrentCameraPosition( "0 0 100 75" );


// ************************************************************************
//
// Add your custom code here...
//
// ************************************************************************

// Set up the player spaceship
datablock fxImageMapDatablock2d( playershipImageMap )
{
mode = full;
textureName = "~/client/images/playerShip";
};

$player = new fxstaticSprite2D() { scenegraph = t2dSceneGraph; };
$player.setPosition( "-35 0" );
$player.setSize( "14 7" );
$player.setImageMap( playershipImageMap );


// Setup the enemy spaceship
datablock fxImageMapDatablock2d( enemyshipImageMap )
{
mode = full;
textureName = "~/client/images/enemyShip1";
};

$enemy = new fxstaticSprite2D() { scenegraph = t2dSceneGraph; };
$enemy.setPosition( "40 0" );
$enemy.setSize( "14 7" );
$enemy.setImageMap( enemyshipImageMap );

// Move the enemy towards us.
%enemy.setLinearVelocityX( -20 );

// Create a new action map.
new ActionMap( playerMap );

// Bind the keys to actions
player.bindCmd( keyboard, "w", "playerUp();", "playerUpStop();" );
player.bindCmd( keyboard, "s", "playerDown();", "playerDownStop();" );
player.bindCmd( keyboard, "a", "playerLeft();", "playerLeftStop();" );
player.bindCmd( keyboard, "d", "playerRight();", "playerRightStop();" );
player.bindCmd( keyboard, "space", "playerFire();", "" );

// Active the action map.
playerMap.push();
}

function playerUp()
{
// Set the player moving up
$player.setLinearVelocityY( -10 );
}

function playerUpStop()
{
// If we are moving up then nulify any upward movement
if( $player.getLinearVelocityY() < 0 )
$player.setLinearVelocityY( 0 );
}


And this is the error I get in the console:

Loading compiled script T2D/client/demoDatablocks.cs.
Loading compiled script T2D/client/datablocks.cs.
Loading compiled script T2D/client/mainScreenGui.gui.
T2D/client/client.cs (110): Unable to find object: '' attempting to call function 'setLinearVelocityX'
T2D/client/client.cs (113): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (114): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (115): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (116): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (117): Unable to find object: 'player' attempting to call function 'bindCmd'

Does anyone know what I'm doing wrong here? Any help is appreciated.

Best regards,
Alex
#13
08/09/2005 (8:27 am)
You put

%enemy.setLinearVelocityX( -20 );

it should be

$enemy.setLinearVelocityX( -20 );
#14
08/09/2005 (8:30 am)
Awesome, that fixed it! thanks!
#15
08/09/2005 (8:30 am)
You have a bunch of typos... for example:

// Move the enemy towards us.
%enemy.setLinearVelocityX( -20 );

should be $enemy.

Also:

// Create a new action map.
new ActionMap( playerMap );

// Bind the keys to actions
player.bindCmd( keyboard, "w", "playerUp();", "playerUpStop();" );
player.bindCmd( keyboard, "s", "playerDown();", "playerDownStop();" );
player.bindCmd( keyboard, "a", "playerLeft();", "playerLeftStop();" );
player.bindCmd( keyboard, "d", "playerRight();", "playerRightStop();" );
player.bindCmd( keyboard, "space", "playerFire();", "" );

you created a new "playerMap" but then refer to it as "player."

Watch that coding carefully! Notice that the console window identified all of these:

T2D/client/client.cs (110): Unable to find object: '' attempting to call function 'setLinearVelocityX'
T2D/client/client.cs (113): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (114): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (115): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (116): Unable to find object: 'player' attempting to call function 'bindCmd'
T2D/client/client.cs (117): Unable to find object: 'player' attempting to call function 'bindCmd'
#16
08/09/2005 (8:32 am)
Awesome, that fixed it! thanks!
#17
08/09/2005 (8:34 am)
Thanks again, fixed it:

// ************************************************************************
//
// Add your custom code here...
//
// ************************************************************************

// Set up the player spaceship
datablock fxImageMapDatablock2d( playershipImageMap )
{
mode = full;
textureName = "~/client/images/playerShip";
};

$player = new fxstaticSprite2D() { scenegraph = t2dSceneGraph; };
$player.setPosition( "-35 0" );
$player.setSize( "14 7" );
$player.setImageMap( playershipImageMap );


// Setup the enemy spaceship
datablock fxImageMapDatablock2d( enemyshipImageMap )
{
mode = full;
textureName = "~/client/images/enemyShip1";
};

$enemy = new fxstaticSprite2D() { scenegraph = t2dSceneGraph; };
$enemy.setPosition( "40 0" );
$enemy.setSize( "14 7" );
$enemy.setImageMap( enemyshipImageMap );

// Move the enemy towards us.
$enemy.setLinearVelocityX( -20 );

// Create a new action map.
new ActionMap( playerMap );

// Bind the keys to actions
playerMap.bindCmd( keyboard, "w", "playerUp();", "playerUpStop();" );
playerMap.bindCmd( keyboard, "s", "playerDown();", "playerDownStop();" );
playerMap.bindCmd( keyboard, "a", "playerLeft();", "playerLeftStop();" );
playerMap.bindCmd( keyboard, "d", "playerRight();", "playerRightStop();" );
playerMap.bindCmd( keyboard, "space", "playerFire();", "" );

// Active the action map.
playerMap.push();
}

function playerUp()
{
// Set the player moving up
$player.setLinearVelocityY( -10 );
}

function playerUpStop()
{
// If we are moving up then nulify any upward movement
if( $player.getLinearVelocityY() < 0 )
$player.setLinearVelocityY( 0 );
}
#18
08/09/2005 (8:37 am)
The console is your friend :)
#19
08/09/2005 (8:40 am)
Be careful when refreshing the page, it can repost your message
#20
08/09/2005 (8:50 am)
You keep hitting refresh and it keeps reposting your post over and over
Page «Previous 1 2