"MyCastle" Tutorial from 2D Game Building Book / problem with deathray start position
by Jon Rothstein · in Torque Game Builder · 07/20/2009 (10:29 pm) · 2 replies
How do I get the deathray to look like it is fireing from mounted raygun rather than the flying bat holding the ray gun?
The following code does everything right except the deathray shoots from the flying bat rather than mounted gun.
function BattyPlayer::onLevelLoaded(%this, %scenegraph)
{
$BattyPlayer = %this;
moveMap.bindCmd( keyboard, "up", "BattyPlayerUp( );" , "BattyPlayerUpStop( );");
moveMap.bindCmd( keyboard, "down" , "BattyPlayerDown( );" , "BattyPlayerDownStop( );");
moveMap.bindCmd( keyboard, "left" , "BattyPlayerLeft( );" , "BattyPlayerLeftStop( );");
moveMap.bindCmd( Keyboard, "right" , "BattyPlayerRight( );" , "BattyPlayerRightStop( );");
moveMap.bindCmd( Keyboard, "space" , "BattyPlayerBoost( );" , "BattyPlayerBoostStop( );");
moveMap.bindCmd( keyboard, "numpad0" , "$BattyPlayer.createDeathray( );", "");
%this.isDead = false;
%this.startX = %this.getPositionX( );
%this.startY = %this.getPositionY( );
}
function BattyPlayer::updateMovement(%this)
{
if(%this.moveUp)
{
$BattyPlayer.setLinearVelocityY(-$BattyPlayer.vSpeed);
}
if(%this.moveDown)
{
$BattyPlayer.setLinearVelocityY($BattyPlayer.vSpeed);
}
if(%this.moveLeft)
{
$BattyPlayer.setFlipX(true);
$BattyPlayer.setLinearVelocityX(-$BattyPlayer.hSpeed);
}
if(%this.moveRight)
{
$BattyPlayer.setFlipX(false);
$BattyPlayer.setLinearVelocityX($BattyPlayer.hSpeed);
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY(0);
}
}
function BattyPlayerUp( )
{
$BattyPlayer.moveUp = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerDown( )
{
$BattyPlayer.moveDown = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerLeft( )
{
$BattyPlayer.moveLeft = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerRight( )
{
$BattyPlayer.moveRight = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerUpStop( )
{
$BattyPlayer.moveUp = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerDownStop( )
{
$BattyPlayer.moveDown = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerLeftStop( )
{
$BattyPlayer.moveLeft = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerRightStop( )
{
$BattyPlayer.moveRight = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerBoost( )
{
%flipX = $BattyPlayer.getFlipX( );
if(%flipX)
{
%hSpeed = -$BattyPlayer.hSpeed * 3;
}
else
{
%hSpeed = $BattyPlayer.hSpeed * 3;
}
$BattyPlayer.setLinearVelocityX( %hSpeed );
}
function BattyPlayerBoostStop( )
{
$BattyPlayer.setLinearVelocityX(0);
}
function BattyPlayer::explode( %this )
{
%this.isDead = true;
%this.setEnabled( false );
%this.schedule( 2000, "spawn" );
}
function BattyPlayer::spawn( %this )
{
%this.isDead = false;
%this.setPosition( %this.startX, %this.startY );
%this.setEnabled( true );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function BattyPlayer::createDeathray( %this )
{
if( !%this.isDead)
{
%this.playerDeathray = new t2dStaticSprite( )
{
scenegraph = %this.scenegraph;
class = playerDeathray;
missileSpeed = %this.missileSpeed;
player = %this;
};
%this.playerDeathray.fire( );
}
}
function playerDeathray::fire( %this )
{
%this.setWorldLimit( kill, "-240 -180 240 180");
%this.setLinearVelocityX( %this.missileSpeed );
%this.setPosition( %this.player.getPosition( ) );
%this.setImageMap( deathrayImageMap );
%this.setSize( 50, 20 ); //aprox half size of original graphic
%this.setCollisionActive( true, true );
%this.setCollisionPhysics( false, false );
%this.setCollisionCallback( true );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function playerDeathray::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if( %dstObj.class $= "enemyGhost" )
{
%srcObj.explode( );
%dstObj.explode( );
}
}
function playerDeathray::explode( %this )
{
%this.safeDelete( );
}
function enemyGhost::explode( %this )
{
%this.spawn( );
}
The following code does everything right except the deathray shoots from the flying bat rather than mounted gun.
function BattyPlayer::onLevelLoaded(%this, %scenegraph)
{
$BattyPlayer = %this;
moveMap.bindCmd( keyboard, "up", "BattyPlayerUp( );" , "BattyPlayerUpStop( );");
moveMap.bindCmd( keyboard, "down" , "BattyPlayerDown( );" , "BattyPlayerDownStop( );");
moveMap.bindCmd( keyboard, "left" , "BattyPlayerLeft( );" , "BattyPlayerLeftStop( );");
moveMap.bindCmd( Keyboard, "right" , "BattyPlayerRight( );" , "BattyPlayerRightStop( );");
moveMap.bindCmd( Keyboard, "space" , "BattyPlayerBoost( );" , "BattyPlayerBoostStop( );");
moveMap.bindCmd( keyboard, "numpad0" , "$BattyPlayer.createDeathray( );", "");
%this.isDead = false;
%this.startX = %this.getPositionX( );
%this.startY = %this.getPositionY( );
}
function BattyPlayer::updateMovement(%this)
{
if(%this.moveUp)
{
$BattyPlayer.setLinearVelocityY(-$BattyPlayer.vSpeed);
}
if(%this.moveDown)
{
$BattyPlayer.setLinearVelocityY($BattyPlayer.vSpeed);
}
if(%this.moveLeft)
{
$BattyPlayer.setFlipX(true);
$BattyPlayer.setLinearVelocityX(-$BattyPlayer.hSpeed);
}
if(%this.moveRight)
{
$BattyPlayer.setFlipX(false);
$BattyPlayer.setLinearVelocityX($BattyPlayer.hSpeed);
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
}
if(!%this.moveUp && !%this.moveDown)
{
%this.setLinearVelocityY(0);
}
}
function BattyPlayerUp( )
{
$BattyPlayer.moveUp = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerDown( )
{
$BattyPlayer.moveDown = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerLeft( )
{
$BattyPlayer.moveLeft = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerRight( )
{
$BattyPlayer.moveRight = true;
$BattyPlayer.updateMovement( );
}
function BattyPlayerUpStop( )
{
$BattyPlayer.moveUp = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerDownStop( )
{
$BattyPlayer.moveDown = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerLeftStop( )
{
$BattyPlayer.moveLeft = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerRightStop( )
{
$BattyPlayer.moveRight = false;
$BattyPlayer.updateMovement( );
}
function BattyPlayerBoost( )
{
%flipX = $BattyPlayer.getFlipX( );
if(%flipX)
{
%hSpeed = -$BattyPlayer.hSpeed * 3;
}
else
{
%hSpeed = $BattyPlayer.hSpeed * 3;
}
$BattyPlayer.setLinearVelocityX( %hSpeed );
}
function BattyPlayerBoostStop( )
{
$BattyPlayer.setLinearVelocityX(0);
}
function BattyPlayer::explode( %this )
{
%this.isDead = true;
%this.setEnabled( false );
%this.schedule( 2000, "spawn" );
}
function BattyPlayer::spawn( %this )
{
%this.isDead = false;
%this.setPosition( %this.startX, %this.startY );
%this.setEnabled( true );
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function BattyPlayer::createDeathray( %this )
{
if( !%this.isDead)
{
%this.playerDeathray = new t2dStaticSprite( )
{
scenegraph = %this.scenegraph;
class = playerDeathray;
missileSpeed = %this.missileSpeed;
player = %this;
};
%this.playerDeathray.fire( );
}
}
function playerDeathray::fire( %this )
{
%this.setWorldLimit( kill, "-240 -180 240 180");
%this.setLinearVelocityX( %this.missileSpeed );
%this.setPosition( %this.player.getPosition( ) );
%this.setImageMap( deathrayImageMap );
%this.setSize( 50, 20 ); //aprox half size of original graphic
%this.setCollisionActive( true, true );
%this.setCollisionPhysics( false, false );
%this.setCollisionCallback( true );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
function playerDeathray::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
if( %dstObj.class $= "enemyGhost" )
{
%srcObj.explode( );
%dstObj.explode( );
}
}
function playerDeathray::explode( %this )
{
%this.safeDelete( );
}
function enemyGhost::explode( %this )
{
%this.spawn( );
}
Torque Owner Timothy Castagna
TK Games LLC
{
%this.setWorldLimit( kill, "-240 -180 240 180");
%this.setLinearVelocityX( %this.missileSpeed );
%this.setPosition( %this.player.getPosition( ) );
%this.setImageMap( deathrayImageMap );
%this.setSize( 50, 20 ); //aprox half size of original graphic
%this.setCollisionActive( true, true );
%this.setCollisionPhysics( false, false );
%this.setCollisionCallback( true );
}
%this.setPosition( %this.player.getPosition( ) );
this line is the problem, you are using the players position to fire the ray, change it to %this.MountedGunName.getPosition() or something similar.