Game Development Community

Creating an object via script

by Arland (Barry) Woodham · in Torque Game Builder · 08/26/2006 (6:30 pm) · 8 replies

Ok well it's been a while since I have posted anywhere on the site. this is mainly due to my new job which a the moment is very high volume work causing a little bit of computer burnout when I get home. However I have had in mind a new twist on the breakout game for some time now. Finally I have felt like doing a little work on it and I seem to be hitting my head against a brick wall here. I actually have my world defined and I can move my paddle using the mouse and the ball bounces and breaks bricks. Here is where things take a turn for the worse. I am trying to spawn a new ball on a mouse click. The click code works and now I have been able to get rid of all the errors in the console. However, my object never appears. From the mole game tutorial it seems that I should be abled to drag an object into the editor and give it a class then using the code below spawn it. This is just to make it appear, no movement, but I get nothing.
function SceneWindow2D::onMouseDown(%this, %mod, %worldPosition)
{
	%newBall = new t2dStaticSprite()
   	{
		class = bounce;
     		sceneGraph = %this;
      
   	};
	%newBall.setPosition(%worldPosition);	
}
I have created sprites using the code only method before the level editor was even created but now getting back into T2D I feel just confused. Maybe its the last 3 months of just action scripting. Anyways after several hours of frustration I am asking for some help.

#1
08/26/2006 (10:29 pm)
One problem I see in your code is that you're trying to assign a scenewindow to a scenegraph. Try

...
    scenegraph = %this.getScenegraph();
    ....
#2
08/27/2006 (12:07 am)
Ben is correct, you are trying to assign a scenegraph to a scenewindow which will not work. %this in that call back is reffering to the SceneWindow2D that the mouse was clicked on not the scenegraph, so Ben's solution should do the trick.
#3
08/27/2006 (5:32 am)
Been looking at the code to long I should have realized the mole game was referencing a level on load and I was referencing the window on click. Stupid mistake however, that did not fix the problem. Any other ideas?
#4
08/27/2006 (7:37 am)
Don't forget to set its image map.
function SceneWindow2D::onMouseDown(%this, %mod, %worldPosition)
{
	%newBall = new t2dStaticSprite()
   	{
		class = bounce;
     		sceneGraph = %this;
      
   	};
        %newBall.setImageMap("BallImageMap");
	%newBall.setPosition(%worldPosition);	
}
#5
08/27/2006 (9:46 am)
Bingo that did it. I thought that defining the class did define the imagemap too but I guess not. Thanks to all for the help.
#6
08/28/2006 (7:00 am)
You can build a datablock like so:

datablock t2dSceneObjectDatablock(ballDatablock)
{
size = "32 32";
imageMap = "ballImageMap";
};

and where you create your new sprite:

%newBall = new t2dStaticSprite()
{
config = ballDatablock;
class = bounce;
sceneGraph = %this.getSceneGraph();
};

This will get you a way to auto-magically assign the imagemap, and other nifty settings like size etc..

I think you could even specify the class and scenegraph into the datablock as well... further simplifying your sprite creation, and centralizing the location for your sprite "definitions"
#7
08/28/2006 (2:52 pm)
That helps a lot I was trying to figure out how to do that but the datablocks I set only seemed to effect sprites added in the editor. I was just missing that config line. Thanks for the tip.
#8
09/19/2006 (10:23 am)
I found the same issue, having to set the imageMap separately from creating the object. Big pain.