need help creating an auto fire function
by nick fortier · in Technical Issues · 05/31/2011 (5:37 am) · 2 replies
So I've been messing around with the shooter tutorial, but instead of having the enemy ships just shoot once when they spawn I want them to shoot continuously.
Now I've been racking my brain on how to get them to do this.I figured out that I can put a %this.schedule line on the function and then duplicate that line with deferent schedule settings to get my enemy ships to fire more,but its not auto fire the way I want.With the function from the tutorial the ship uses a spawn function to tell it when to fire, I don't want this. I want the fire function to be independent of the enemy ships spawn function.
I've also tried messing around with various timer functions but can't get them to work, I'm not getting any errors just nothing happens. Heres a recent script:
function enemyShip::onLevelLoaded(%this,%scenegraph)
{
%this.startX=%this.getPositionX();
%this.spawn();
%spawnMissile.setTimerOn(100);
}
//movement//
function enemyShip::enemyMovement(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed,%this.maxSpeed));
}
//spawn//
function enemyShip::onWorldLimit(%this,%mode,%limit)
{
if(%limit $="left")
{
%this.spawn();
}
}
function enemyShip::spawn(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed,%this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
}
//explode//
function enemyShip::explode(%this)
{
%this.spawn();
}
//enemyfire//
function enemyship::fireMissile(%this)
{
%this.enemyMissile=new t2dStaticSprite()
{
scenegraph=%this.scenegraph;
class=enemyMissile;
missileSpeed=%this.missileSpeed;
enemy=%this;
};
%this.enemyMissile.fire();
}
function spawnMissile::onTimer(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed,%this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
%this.schedule(1000,"fireMissile",enemyMissile);
}
Im pretty new to all of this programming stuff and would like a nudge in the right direction.
Now I've been racking my brain on how to get them to do this.I figured out that I can put a %this.schedule line on the function and then duplicate that line with deferent schedule settings to get my enemy ships to fire more,but its not auto fire the way I want.With the function from the tutorial the ship uses a spawn function to tell it when to fire, I don't want this. I want the fire function to be independent of the enemy ships spawn function.
I've also tried messing around with various timer functions but can't get them to work, I'm not getting any errors just nothing happens. Heres a recent script:
function enemyShip::onLevelLoaded(%this,%scenegraph)
{
%this.startX=%this.getPositionX();
%this.spawn();
%spawnMissile.setTimerOn(100);
}
//movement//
function enemyShip::enemyMovement(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed,%this.maxSpeed));
}
//spawn//
function enemyShip::onWorldLimit(%this,%mode,%limit)
{
if(%limit $="left")
{
%this.spawn();
}
}
function enemyShip::spawn(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed,%this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
}
//explode//
function enemyShip::explode(%this)
{
%this.spawn();
}
//enemyfire//
function enemyship::fireMissile(%this)
{
%this.enemyMissile=new t2dStaticSprite()
{
scenegraph=%this.scenegraph;
class=enemyMissile;
missileSpeed=%this.missileSpeed;
enemy=%this;
};
%this.enemyMissile.fire();
}
function spawnMissile::onTimer(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed,%this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
%this.schedule(1000,"fireMissile",enemyMissile);
}
Im pretty new to all of this programming stuff and would like a nudge in the right direction.
About the author
#2
06/01/2011 (4:50 am)
Sweet dude that completely worked! Now my enemy ship fires automatically when it spawns.I guess I still have a lot to learn about how to code this stuff, but it seems like I'm going in the right direction with it
Torque Owner Andrew Son
change %spawnMissile.setTimerOn(100); TO %this.setTimerOn(100);
function spawnMissile::onTimer(%this) TO function enemyShip::onTimer(%this)
%this.schedule(1000,"fireMissile",enemyMissile); TO
%this.schedule(1000,fireMissile);