game crashes when trying to fire a second time.
by Nathan Morton · in Torque Game Builder · 03/29/2011 (6:52 pm) · 11 replies
I have my enemies fire a missile when they spawn, then I want them to fire another on via a schedule.
code is below
the specific line that causes the problem is
when I comment it out, game runs fine, but the enemies only fire once.
when I leave it in, game blows up with an app crash. nothing indicated in the console log.
any help is appreciated
code is below
//********Drone Spawning********
function spawnDrone()
{
%enemyDrone = new t2dAnimatedSprite(){
scenegraph = $sceneGraph;
class = enemyShip;
Layer=1;
imageMap = Defender2ImageMap;
size = "6.102 5.768";
};
%speed = getrandom(3,7);
%x= getrandom(-40,40);
%enemyDrone.playAnimation("Defender2Animation");
%enemyDrone.health=3;
%enemyDrone.setPosition(%x, -45);
%enemyDrone.setLinearVelocityY(%speed*2);
%enemyDrone.setLinearVelocityX(getrandom(-5,5));
%enemyDrone.setCollisionActive( true, true );
%enemyDrone.setCollisionPhysics(false, false);
%enemyDrone.setCollisionCallback(true);
%enemyDrone.setWorldLimit( kill, "-73.748 -67.575 33.359 76.630" );
%this = %enemyDrone;
/*if ($game $= true) - WORKS FINE NOT USDE DURING TESTING
{
schedule (getrandom(500,1000),0,spawnDrone);
}*/
if ($game $= true)
{
%this.fireMissile();
}
}
function enemyShip::fireMissile(%this)
{
echo("fireMissile");
echo(%this);
%this.enemyMissile = new t2dAnimatedSprite()
{
scenegraph = %this.scenegraph;
class = enemyMissile;
enemy = %this;
};
schedule (getrandom(500,1000),0,%this.fireMissile());
%this.enemyMissile.fire();
}the specific line that causes the problem is
schedule (getrandom(500,1000),0,%this.fireMissile());
when I comment it out, game runs fine, but the enemies only fire once.
when I leave it in, game blows up with an app crash. nothing indicated in the console log.
any help is appreciated
#2
if I use
nothing happens at all - no indication that I even tried to schedule something
if I use
I get the following message in my console log
game/gameScripts/enemy.cs (50): Unable to find function fireMissile
03/30/2011 (6:26 pm)
still having a problem. though the game doesn't crash anymore.if I use
%enemyDrone.schedule(getrandom(500,1000),0,fireMissile);
nothing happens at all - no indication that I even tried to schedule something
if I use
%enemyDrone.schedule(getrandom(500,1000),0,fireMissile());
I get the following message in my console log
game/gameScripts/enemy.cs (50): Unable to find function fireMissile
#3
fireMissle is not a stand alone function. It is scoped to your enemyShip class. Because %enemyDrone has the enemyShip class, it can schedule to call its own functions. That is the difference between schedule() and %someObject.schedule().
03/30/2011 (7:17 pm)
Try this:%enemyDrone.schedule(getRandom(500,1000), fireMissle);
fireMissle is not a stand alone function. It is scoped to your enemyShip class. Because %enemyDrone has the enemyShip class, it can schedule to call its own functions. That is the difference between schedule() and %someObject.schedule().
#4
You need to get the scene graph from your level:
03/30/2011 (8:45 pm)
Also, I don't have the code in front of me, but I do not think this is what you want:%this.scenegraph;
You need to get the scene graph from your level:
SceneWindow2D.getScenegraph()
#5
03/30/2011 (8:53 pm)
With my above posts in mind, here is how I would rewrite the function:function enemyShip::fireMissile(%this)
{
// This echo will notify that we are in the function
echo("fireMissile");
// This echo will tell us the specific object calling the method (id)
echo(%this);
// Get the scenegraph
%sceneGraph = scenegraph = SceneWindow2D.getScenegraph();
// This will create a new t2dAnimatedSprite
// The properties here are somewhat circular, but that should be fine
%this.enemyMissile = new t2dAnimatedSprite()
{
scenegraph = %sceneGraph;
class = enemyMissile;
enemy = %this;
};
// Schedule a call to this function in a random
// time between 0.5 and 1 second
%this.schedule(getRandom(500,1000), fireMissile);
// Call the enemyMissile::fire() routine
%this.enemyMissile.fire();
}
#6
03/30/2011 (8:55 pm)
You are definitely on the right track. Your logic is fine. The problem is just learning the right functions to call and what certain inherent variables are used for. Give that a try and let me know how it works.
#7
set in my game.cs as
under the startGame function - if this isn't best practices let me know.
Are there any good resources on the actual scripting side? What I generally do right now is find a tutorial that is "close" to what I want and go from there.
I'd love some explanations on the collision functions, and custom functions.
I really do appreciate all the help!
03/30/2011 (9:09 pm)
actually I have%sceneGraph = scenegraph = SceneWindow2D.getScenegraph();
set in my game.cs as
$sceneGraph = sceneWindow2D.getSceneGraph();
under the startGame function - if this isn't best practices let me know.
Are there any good resources on the actual scripting side? What I generally do right now is find a tutorial that is "close" to what I want and go from there.
I'd love some explanations on the collision functions, and custom functions.
I really do appreciate all the help!
#8
Saves you having to call getSceneGraph. So yes, what you are doing is pretty much how a lot of us are doing the same thing. :)
EDIT:
I think Mich made a typo when he posted this:
I don't think that will even compile.
03/31/2011 (5:37 am)
Nathan - In my startgame function I set a global variable to the scenegraph like this:$global_scenegraph = sceneWindow2D.loadLevel(%level);
Saves you having to call getSceneGraph. So yes, what you are doing is pretty much how a lot of us are doing the same thing. :)
EDIT:
I think Mich made a typo when he posted this:
%sceneGraph = scenegraph = SceneWindow2D.getScenegraph();
I don't think that will even compile.
#9
Click on the Scripting section to start reading them.
03/31/2011 (7:31 am)
@Nathan - I recently updated the iTorque 2D documentation and added a new section on scripting. The guides are applicable to Torque 2D: iTorque 2D Documentation.Click on the Scripting section to start reading them.
#10
I'll check out that documentation.
@Chris, I was hoping I was using something close, thanks for confirming.
03/31/2011 (6:03 pm)
@Mike, thanks for all the help, it's working correctly now. I probably need to read up on schedule() a bit.I'll check out that documentation.
@Chris, I was hoping I was using something close, thanks for confirming.
#11
Because of how similar the two engines are, you can probably find both the Torque 2D and iTorque 2D documentation useful for gathering information. The iTorque 2D docs are the most up to date and I cover a lot more details on certain aspects, like building particles in the Rainy Day tutorial and explaining scrollers a bit more. Stuff like that.
Let us know if you need any other help.
03/31/2011 (7:54 pm)
@Nathan - Huzzah! Glad you got it working. I hope you like the docs. On a separate note, I've been working my way through the product line to overhaul and fix the documentation. I started with T3D, hit iTorque 2D, so Torque 2D is next in line for a documentation overhaul. Because of how similar the two engines are, you can probably find both the Torque 2D and iTorque 2D documentation useful for gathering information. The iTorque 2D docs are the most up to date and I cover a lot more details on certain aspects, like building particles in the Rainy Day tutorial and explaining scrollers a bit more. Stuff like that.
Let us know if you need any other help.
Employee Michael Perry
ZombieShortbus
Just use %drone:
if($game == true) { %drone. fireMissile(); }Also, I would not use schedule(...). Use %this.schedule(...). Try to make those changes and see what happens. Also, check your console.log for any errors or warnings.