Problems getting two scripts working together.
by J.T. Ooley · in Torque Game Builder · 09/13/2010 (2:14 am) · 6 replies
To me these seem like they should work together. The first script should fire a weapon on a mouse click and the second should create the sprite that's fired. But instead of firing the weapon the ship just spazzes in the direction of the mouse. The console isn't showing me any errors so I guess it's just bad code : any ideas?
function SceneWindow2D::onMouseDown( %this , %modifier , %worldPosition , %mouseClicks )
{
$FireWeaponActive = true;
%time = GetTimeWeapons();
ship.setTimerOn( %time );
ship.RunTimer();
}
function SceneWindow2D::onMouseUp( %this , %modifier , %worldPosition , %mouseClicks )
{
Ship.setTimerOff();
$FireWeaponActive = false;
}
function SceneWindow2D::onRightMouseDown( %this , %modifier , %worldPosition , %mouseClicks )
{
$RightMouseClickDetected = true;
}
function SceneWindow2D::onRightMouseUp( %this , %modifier , %worldPosition , %mouseClicks )
{
$RightMouseClickDetected = false;
}
function GetTimeWeapons( %this )
{
%time = 0.1;
%time *= 1000;
return%time;
}
function ShipClass::onTimer( %this )
{
%this.RunTimer();
}
function ShipClass::RunTimer( %this )
{
%angle = FindShootAngle();
Ship.turnSpeed = 250.0;
ship.rotateTo( %angle , ship.turnSpeed , true , false , true, 0.1 );
$FireAngle = Ship.getRotation();
FireWeapon( $FireAngle );
}
function FireWeapon( $FireAngle )
{
FireMainLaser( $FireAngle );
}
function FindShootAngle()
{
%vec = t2dVectorSub( Ship.getPosition() , SceneWindow2D.getMousePosition() );
return -mRadToDeg( mATan( getWord( %vec , 0 ) , getWord( %vec,1 ) ) );
}function FireMainLaser( $FireAngle )
{
%MainLaser = new t2dStaticSprite()
{
scenegraph = sceneWindow2D.getScenegraph();
class = MainLaserClass;
};
%MainLaser.setProperties( $FireAngle );
}
function MainLaserClass::setProperties( %this , $FireAngle )
{
%this.setImageMap( LasersImageMap );
%this.setSize( 1.5 , 1.5 );
%this.setPosition( Ship.getPosition() );
%this.setRotation( $FireAngle );
%this.setImpulseForcePolar( $FireAngle , 125 );
%this.setCollisionActive( true , true );
%this.setCollisionPhysics( false , false );
%this.setCollisionCallback( true );
%this.setCollisionLayers( "20" );
%this.setWorldLimit( "kill" , -55 , -40 , 55 , 40 , false );
%this.setLayer ( "15" );
%this.setLifeTime( 0.5 );
}
function MainLaserClass::onCollision( %srcObj , %dstObj , %srcRef , %dstRef , %time , %normal , %contactCount , %contacts )
{
%srcObj.safeDelete();
}
#2
If I can't find the solution tonight, I'm gonna re-write it tomorrow. Part of the code I modded from a tutorial. Might be better if I started from scratch.
Thanks for your input
09/13/2010 (6:17 am)
Yea, I have Torsion. Also, I put echo statements in all of the functions to make sure each one was being called and the console picked up all of the echoes. And the angles are right, because the ship faces the mouse like it's supposed to on a right click. The only problem is durring a left mouse click, the lasers aren't appearing and the ship starts to spazz out. If I can't find the solution tonight, I'm gonna re-write it tomorrow. Part of the code I modded from a tutorial. Might be better if I started from scratch.
Thanks for your input
#3
09/13/2010 (6:13 pm)
I know this may be too soon, but were you able to figure it out?
#4
edit:
I figured it out! It was so simple lol. The lasers were colliding with the ship. I forgot to set the ships collision layers...
09/13/2010 (6:16 pm)
Well, I kind of rewrote it. I had a typo in the setProperties function ("LaserClass" instead of "MainLaserClass") and the spazzing stopped but of course with the typo the lasers weren't appearing. When I fixed the typo the spazzing started again x.X So my guess is that the only problem is in the setProperties function?edit:
I figured it out! It was so simple lol. The lasers were colliding with the ship. I forgot to set the ships collision layers...
#5
09/13/2010 (7:42 pm)
Quote:I've done that so many times... like everytime I've made something shoot in TGB.
I figured it out! It was so simple lol. The lasers were colliding with the ship. I forgot to set the ships collision layers...
#6
09/14/2010 (2:41 am)
Too funny! I'm with Patrick here... it's the one "lesson learned" that I never learn.
Associate William Lee Sims
Machine Code Games
BUT! You can still try to get a handle on this. Put echo-statements in your code to make sure that the angles are what you expect. Also, I'd try to make sure that MainLaserClass::setProperties is being called (even though it clearly is being called).
Also, as an aside, you can use t2dAngleToPoint to calculate your "FindShootAngle()".