Shooter - use of onUpdateScene()
by Benjamin LIU · in Torque Game Builder · 08/13/2006 (1:29 am) · 19 replies
I have done a few time this tutorial, and now just want to make the enemy behaviour a bit different.
I would have the eShip go down and than go up after it reach the $top or $bottompoint.
I try to do a frame by frame checking for the eShip ( enemyShip ),
my code ( enemy.cs ) is the following:
//=================================================
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
$ySpeed = 10;
$top = -30;
$bottom = 30;
%this.setPositionY( 0 );
%this.setLinearVelocityY( $ySpeed );
}
function enemyShip::onUpdateScene(%this){
if( %this.getPositionY() < $top || %this.getPositionY() > $bottom ){
$ySpeed = -$ySpeed;
%this.setLinearVelocityY( $ySpeed );
}
}
//=================================================
I have successfully made the eShip move down in "$ySpeed" speed.
but the onUpdateScene function just not working.
can someone tell me what I have done wrong?
thanks in advance
I would have the eShip go down and than go up after it reach the $top or $bottompoint.
I try to do a frame by frame checking for the eShip ( enemyShip ),
my code ( enemy.cs ) is the following:
//=================================================
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
$ySpeed = 10;
$top = -30;
$bottom = 30;
%this.setPositionY( 0 );
%this.setLinearVelocityY( $ySpeed );
}
function enemyShip::onUpdateScene(%this){
if( %this.getPositionY() < $top || %this.getPositionY() > $bottom ){
$ySpeed = -$ySpeed;
%this.setLinearVelocityY( $ySpeed );
}
}
//=================================================
I have successfully made the eShip move down in "$ySpeed" speed.
but the onUpdateScene function just not working.
can someone tell me what I have done wrong?
thanks in advance
About the author
#2
...I am new in T2D ^^;
I see.... is there any easy way, to link up the scenegraph callback?
should I make something like below?
//=============================================
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
$SG = %scenegraph
...
}
function SG::onUpdateScene(%this){
...
}
//=============================================
Can anyone tell me the code, how to do the frame by frame action by onUpdateScene
I would keep checking the status of the enemy.
(or even the enemy-missile. So I could do something like "Aiming-Missile" following the playerShip. later)
Anyone please help?
08/13/2006 (5:58 am)
Neo, Thanks for the replay....I am new in T2D ^^;
I see.... is there any easy way, to link up the scenegraph callback?
should I make something like below?
//=============================================
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
$SG = %scenegraph
...
}
function SG::onUpdateScene(%this){
...
}
//=============================================
Can anyone tell me the code, how to do the frame by frame action by onUpdateScene
I would keep checking the status of the enemy.
(or even the enemy-missile. So I could do something like "Aiming-Missile" following the playerShip. later)
Anyone please help?
#3
You might want to check if %this == $SG or whatever
(or use the name of the actual scenegraph) as there can be more than one scenegraph.
08/13/2006 (6:19 am)
Here is an example (it will echo the current value of count on each update):$count = 0;
function t2dSceneGraph::onUpdateScene( %this )
{
$count += 1;
echo( $count );
}You might want to check if %this == $SG or whatever
(or use the name of the actual scenegraph) as there can be more than one scenegraph.
#4
but still don't know how to link up the $SG ( or the actual scenegraph ) with my enemyShip?
...this result the $eShip.ySpeed never found.
can you give me some more hint ^^?
by the way, I have to restart the T2D.exe every time I change the code.
is there any easy way to make my life easier?
thanks in advance.
08/13/2006 (7:05 am)
Cool, I see the counter output to the console.but still don't know how to link up the $SG ( or the actual scenegraph ) with my enemyShip?
...this result the $eShip.ySpeed never found.
can you give me some more hint ^^?
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
$eShip = %this;
$ySpeed = 10;
$SG = %scenegraph;
}
function t2dSceneGraph::onUpdateScene(%this){
if( %this == $SG ){
$eShip.setPositionY( $eShip.setPositionY() + $eShip.ySpeed );
echo("$SG is found!!");
}else{
echo("$SG is not found!!");
}
}by the way, I have to restart the T2D.exe every time I change the code.
is there any easy way to make my life easier?
thanks in advance.
#5
Remember that you don't have to update the position constantly yourself,
you could just use setLinearVelocityX(), setLinearVelocityY() or setLinearVelocity().
You can simply restart the project view from the editor or use endGame() then startGame() from
the console.
~neo
08/13/2006 (8:25 am)
Your code above seems fine, is it not working?Remember that you don't have to update the position constantly yourself,
you could just use setLinearVelocityX(), setLinearVelocityY() or setLinearVelocity().
You can simply restart the project view from the editor or use endGame() then startGame() from
the console.
~neo
#6
Thanks for the great help Neo
I have tuned up my code and it is now wroking.
thanks again.
your advice really help me out.
ben
08/13/2006 (10:22 am)
Cool I got a easier and faster way to do the debuging now.Thanks for the great help Neo
I have tuned up my code and it is now wroking.
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
$eShip = %this;
$SG = %scenegraph;
$ySpeed = 30;
$top = -30;
$bottom = 30;
%this.startX = %this.getPositionX();
$eShip.setLinearVelocityY($ySpeed);
}
function t2dSceneGraph::onUpdateScene(%this){
if( %this == $SG ){
if( $eShip.getPositionY() < $top || $eShip.getPositionY() > $bottom ){
if( $eShip.getPositionY() < $top ) $eShip.setPositionY( $top );
if( $eShip.getPositionY() > $bottom ) $eShip.setPositionY( $bottom );
$ySpeed = -$ySpeed;
$eShip.setLinearVelocityY($ySpeed);
}
}
}thanks again.
your advice really help me out.
ben
#7
on each update is to set the world limit for your enemyShip and add a onWorldLimit callback.
~neo
08/13/2006 (11:23 am)
Glad that worked for you... another way to do the wrapping stuff instead of checking iton each update is to set the world limit for your enemyShip and add a onWorldLimit callback.
function enemyShip::onWorldLimit(%this, %limitMode, %limit)
{
// check which limit you hit and reset velocity
}~neo
#8
Thanks Neo
08/17/2006 (8:09 am)
Really.. I think the onWorldLimt function, should work better to redece the engine loading.Thanks Neo
#9
I try to use the onUpdateScene, to make the playerMissile to trace the $eShip.
there is the code under the onUpdateScene function
( it basically keep updating the target ( $eShip ) Y and trace it. )
I have successfully made the playerMissile trace the eShip
, but not after other playerMissile is created.
please check the code below:
As the playerMissile reference to the $pShip and it has been rewrite
, after a new playerMissile is created.
So my Question is how to handle a list of playerMissle instances?
or any other way?
Can anyone give advice to me.
thanks in advance.
08/17/2006 (8:36 am)
New Question: how to reference list of playerMissile?I try to use the onUpdateScene, to make the playerMissile to trace the $eShip.
there is the code under the onUpdateScene function
( it basically keep updating the target ( $eShip ) Y and trace it. )
$pShip.playerMissile.setPositionY( $pShip.playerMissile.getPositionY() + ( $eShip.getPositionY() - $pShip.playerMissile.getPositionY() ) / 16 );
I have successfully made the playerMissile trace the eShip
, but not after other playerMissile is created.
please check the code below:
function playerShip::createMissile(%this)
{
%this.[b]playerMissile[/b] = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = playerMissile;
}
}As the playerMissile reference to the $pShip and it has been rewrite
, after a new playerMissile is created.
So my Question is how to handle a list of playerMissle instances?
or any other way?
Can anyone give advice to me.
thanks in advance.
#10
08/17/2006 (9:08 am)
You could either create a simset to hold the handles to the missiles and add and delete them as they are spawned/destroyed, or have each missile call a schedule on itself every few milliseconds to update it's position.
#11
that sound's great I have try the code and just .... a bit problem with the traceEmeny function.
Can someone help me out?
please help, ^^;
08/17/2006 (9:35 am)
Thanks Philipthat sound's great I have try the code and just .... a bit problem with the traceEmeny function.
Can someone help me out?
function playerShip::createMissile(%this)
{
%this.[b]traceMissile[/b]= new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = [b]traceMissile[/b];
}
if(!isObject(traceMissileSet)) new SimSet(traceMissileSet);
traceMissileSet.add( %this.[b]traceMissile[/b]);
%this.[b]traceMissile[/b].schedule( 1000, "traceEmeny" );
}function traceMissile::traceEmeny(%this){
echo("traceMissile::traceEmeny");
%this.setPositionY( %this.getPositionY() + ( $eShip.getPositionY() - %this.getPositionY() ) / 16 );
}the echo("traceMissile::traceEmeny"); is not been call...please help, ^^;
#12
is missing a semi-colon ; ... so it should look like this
08/17/2006 (11:24 am)
%this.traceMissile= new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = traceMissile;
}is missing a semi-colon ; ... so it should look like this
%this.traceMissile= new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = traceMissile;
};
#13
08/17/2006 (11:25 am)
If you exec your scripts in the startGame() function (or in the game.cs in general) then they should re-exec each time you hit play and this then should've prompted a script error and warned you :)
#14
...still don't know how to make my code work.
the code function traceEmeny still not being call by the schedule.
there is no error output in console, nor the echo("traceMissile::traceEmeny"); either
Is that something I miss?
Can someone help me in this issue
Thanks ^^;
08/18/2006 (3:59 am)
Oh, thanks I think I have the semi-colon fixed....still don't know how to make my code work.
the code function traceEmeny still not being call by the schedule.
there is no error output in console, nor the echo("traceMissile::traceEmeny"); either
function playerShip::createMissile(%this)
{
%this.traceMissile= new t2dStaticSprite()
{
...
}
...
[b]%this.traceMissile.schedule( 1000, "traceEmeny" );[/b]
}
function traceMissile::traceEmeny(%this){
[b]echo("traceMissile::traceEmeny");[/b]
%this.setPositionY( %this.getPositionY() + ( $eShip.getPositionY() - %this.getPositionY() ) / 16 );
}Is that something I miss?
Can someone help me in this issue
Thanks ^^;
#15
Ohhhhhh.... sorry guys
I miss the following code to make the script available in main.cs
exec("./gameScripts/traceMissile.cs");
^^;
thanks and sorry for the trouble.
08/18/2006 (4:30 am)
The problem is solved for the schedule functionOhhhhhh.... sorry guys
I miss the following code to make the script available in main.cs
exec("./gameScripts/traceMissile.cs");
^^;
thanks and sorry for the trouble.
#16
I have successfully made the sim group of traceMissile to trace the eShip
, but the result is not smooth... it is not as smooth as I do it in onUpdateScene function.
I think the problem is the schedule function don't update the rendering like the onUpdateScene does.
can I call an update rendering function in schedule... like flash does for the updateAfterEvent() function?
just wondering if I have to switch back to use onUpdateScene to make it smooth..
any suggestion will be appreciated
ben
08/18/2006 (8:17 am)
I have a problem with the schedule function.I have successfully made the sim group of traceMissile to trace the eShip
, but the result is not smooth... it is not as smooth as I do it in onUpdateScene function.
I think the problem is the schedule function don't update the rendering like the onUpdateScene does.
can I call an update rendering function in schedule... like flash does for the updateAfterEvent() function?
just wondering if I have to switch back to use onUpdateScene to make it smooth..
any suggestion will be appreciated
ben
#17
You're updating your traceEnemy function every second with the schedule(1000, "traceEnemy"), rather than every frame in the onUpdateScene. The advantage of this is that it doesn't require so much processing, but the disadvantage is that once a second isn't frequent enough for smooth animation.
Try setting the schedule to something like 100 for 10 updates per second and see how much smoother that makes things: schedule(100, "traceEnemy"), or even lower numbers if needed.
08/18/2006 (9:19 am)
Benjamin,You're updating your traceEnemy function every second with the schedule(1000, "traceEnemy"), rather than every frame in the onUpdateScene. The advantage of this is that it doesn't require so much processing, but the disadvantage is that once a second isn't frequent enough for smooth animation.
Try setting the schedule to something like 100 for 10 updates per second and see how much smoother that makes things: schedule(100, "traceEnemy"), or even lower numbers if needed.
#18
I have overlook it, the schedule time.
I have in make the schedule time from 1000 to 30 or 15, it is smooth now.
But if I make such short schedule it will ending up create the same loading, as the onUpdateScene did, am I right?
if so, should I just use the onUpdateScene instead of schedule to due with moving obj, like the traceMissile now.
... what do you guys think?
By the way, where I could make control of the frame rate in under Game Build or the script?
08/18/2006 (8:04 pm)
Thanks Sebastain.I have overlook it, the schedule time.
I have in make the schedule time from 1000 to 30 or 15, it is smooth now.
But if I make such short schedule it will ending up create the same loading, as the onUpdateScene did, am I right?
if so, should I just use the onUpdateScene instead of schedule to due with moving obj, like the traceMissile now.
... what do you guys think?
By the way, where I could make control of the frame rate in under Game Build or the script?
#19
It is better fit for what I want to do.
thanks guys. I did ending up have learnt something useful and made some good progress by this Thread
and I did bought the T2D of course!!
many thanks
ben
08/19/2006 (8:05 am)
I think I just use the onUpdateScene instead of the Schedule function.It is better fit for what I want to do.
thanks guys. I did ending up have learnt something useful and made some good progress by this Thread
and I did bought the T2D of course!!
many thanks
ben
Torque Owner Neo Binedell
~neo