Object Creation
by John Klotz · in Torque Game Builder · 07/03/2006 (8:20 am) · 11 replies
function createenemy()
{
%enemy = new t2dStaticSprite()
{
scenegraph = t2dscenegraph;
class = enemygroup;
};
%enemy.setWorldLimit( clamp, "-55.000 -420.000 600.000 40" );
%enemy.setLinearVelocityY(50);
%enemy.setPosition(40,40);
%type = getRandom(2);
if( %type == 0)
{
%enemy.setimagemap(dog2imagemap);
}
if( %type == 1)
{
%enemy.setimagemap(owlimagemap);
}
if( %type == 2)
{
%enemy.setimagemap(foximagemap);
}
%enemy.setSize(22, 10);
%enemy.setCollisionActive(true, true);
%enemy.setCollisionPhysics(false, false);
%enemy.setCollisionCallback(true);
}Anyone know why that isnt creating an enemy for me in the specified position?
I cant create enemies from other objects fairly easily but I just want enemies to pop up in random positions around the map for now. Can someone help steer me in the right direction to accomplish this.
Thank you very much
#2
07/03/2006 (8:35 am)
You have to add the object to the scenegraph. Use the addToScene method or whatever it's called. Also there's a semicolon missing from the line in your first function.
#3
It keeps saying the error is taking place on line 0. Anyone have any ideas why this isn't working for me?
07/03/2006 (9:25 am)
Added t2dscenegraph.addtoscene(%enemy);in every place imaginable and I keep receiving the error 'Unable to find object 't2dScenegraph' attempting to call function 'addtoscene'
It keeps saying the error is taking place on line 0. Anyone have any ideas why this isn't working for me?
#4
Of Course SceneWindow2D is the SceneWindow, yours may be named different, depending upon how far you diverged from the T2D examples. or using a gui window different than mainscreengui...
HTH....
07/03/2006 (5:16 pm)
Try this..%sceneGraph = SceneWindow2D.getSceneGraph(); %sceneGraph.addToScene(%enemy); or... %enemy.addToScene(%sceneGraph);
Of Course SceneWindow2D is the SceneWindow, yours may be named different, depending upon how far you diverged from the T2D examples. or using a gui window different than mainscreengui...
HTH....
#5
%scenegraph.addToScene(%object);
OR
%object.addToScene(%scenegraph);
07/03/2006 (7:33 pm)
Not that it matters, but addToScene works both ways. It's defined for both objects. You can do%scenegraph.addToScene(%object);
OR
%object.addToScene(%scenegraph);
#6
07/03/2006 (7:35 pm)
@Ben - isn't that what I just said? :-)
#8
To this
would probably be a cleaner solution
07/05/2006 (9:16 am)
Switchingfunction createenemy()
{
%enemy = new t2dStaticSprite()
{
scenegraph = t2dscenegraph;
class = enemygroup;
};To this
function createenemy()
{
%enemy = new t2dStaticSprite()
{
scenegraph = SceneWindow2D.getScenegraph();
class = enemygroup;
};would probably be a cleaner solution
#9
I tend to use functions to delay addition to my scenegraphs programatically or set an sceneObjects characteristics.. It guess its more of a personal preference for me. Your way is definitely cleaner though.
07/05/2006 (9:20 am)
@Matthew.I tend to use functions to delay addition to my scenegraphs programatically or set an sceneObjects characteristics.. It guess its more of a personal preference for me. Your way is definitely cleaner though.
#10
07/05/2006 (9:24 am)
I can definately see how that would be useful, delaying the object being added to your scenegraph.
#11
07/07/2006 (7:05 am)
Glad I could spark such a debate. =) Thanks for all the help guys.
Torque Owner John Klotz
Thanks