Game Development Community

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

#1
07/03/2006 (8:28 am)
I guess more specifically I would like to know why this isn't creating the enemies for me. I know how to make things pop up in the desired locations but for some reason when I try to create an object from nothing it never works. I always have to have a pre-existing object create it for me.

Thanks
#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
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
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
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? :-)
#7
07/03/2006 (11:25 pm)
DOH!

:P
#8
07/05/2006 (9:16 am)
Switching

function 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
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.