Game Development Community

Using arrays??

by Joynisha Sumpter · in Torque Game Builder · 12/15/2006 (1:45 pm) · 11 replies

Hello! I am trying to build the game snake and I wanted to put the image of a block I have into an array so I can build the snake. How can you do this using torque script. Here is my source code below!


//$MyArray[4];
$num=4;
$j=25;


function playerSnake::onLevelLoaded(%this, %scenegraph)
{
$SnakePlayer = %this;
%this.startPosition = %this.getPosition();

$MyArray[0]= %this.setPosition(400,300);

for(%i=1;%i<sizeof($MyArray);%i++)
{
$myarray[%i]=%this.clone(true);
%this.setPosition(40+$j,40);
$j++;
}

#1
12/16/2006 (12:21 am)
Is there no one that can help, I really need help ;_;.....................
#2
12/16/2006 (2:37 am)
Is your script not worked ????
try $myarray[0] until $myarray[3] .dump is there any function come out to the console
if not then your array doesn't contain a block of image
#3
12/16/2006 (3:37 am)
No, my script is not working. Would you write that in a for loop $Myarray[0] -Myarray[3].dump?

I decided maybe using the mount function would be better? Mounting an object correct me if I am wrong will allow another object to follow the first object you mounted it to perfectly. Which would give me the chain effect I am looking for inorder to build my snake without having to do all that excess coding????

Anyways, do you know how to mount an object to another, I wrote some code but the other object is not showing up. I am a newbie to torque script so I don't know when I am setting something up right or wrong. Also I look for tutorials or examples but those are limited, the only place I can hopefully get some help is the forums so thanks for helping.

Here is the code I tried to write in order to mount an object to another.

function playerSnake::onLevelLoaded(%this, %scenegraph)
{
   $SnakePlayer=%this;
   attachSnake( %this, "50 50", 0 );
                         .....
                          .......
}

function attachSnake(%mountObj, %mountPosition, %angle)
{
 %snake.mount( %mountObj, %mountPosition, 0, true );
}
#4
12/18/2006 (12:57 am)
Yes so this is the script,

function playerSnake::onLevelLoaded(%this, %scenegraph)
{
$SnakePlayer = %this;
%this.startPosition = %this.getPosition();

$MyArray[0]= %this.setPosition(400,300);

for(%i=1;%i {
$myarray[%i]=%this.clone(true);
$myarray[%i].dump();
%this.setPosition(40+$j,40);
$j++;
}

} ------>>>>>and u miss this


or why you not tried create on the fly like this

function playerSnake::onLevelLoaded(%this, %scenegraph)
{
$SnakePlayer = %this;
%this.startPosition = %this.getPosition();

$MyArray[0]= %this.setPosition(400,300);

for(%i=1;%i {
$myarray[%i]= new T2dStaticSprite ()
{
imageMap = "yoursnakeimagemap";
scenegraph= scenewindow2d.getscenegraph();
frame = "0";
size="20 20";
layer="1";
};
$myarray[%i].dump();
%this.setPosition(40+$j,40);
$j++;
}


you could write the

{
imageMap = "yoursnakeimagemap";
scenegraph= scenewindow2d.getscenegraph();
frame = "0";
size="20 20";
layer="1";
};

by copying the script on your T2d file , and open it on textpad
but don't forget to add this
scenegraph= scenewindow2d.getscenegraph();
#5
12/18/2006 (1:13 am)
Sorry i make a wrong code on this part

%this.setPosition(40+$j,40); =====> $myarray[%i].setposition(40+$j,40);
oooooowwwww maybe it's your error why you can't create the snake with your first script
because you use %this.setposition not $myarray[%i].setposition


anyway this is how you mount an object ( i use create on the fly technique again , because i 'dont know how
to use clone function )

function playerSnake::onLevelLoaded(%this, %scenegraph)
{%head=%this
for(%i=1;%i {
%body= new T2dStaticSprite ()
{
imageMap = "yoursnakeimagemap";
scenegraph= scenewindow2d.getscenegraph();
frame = "0";
size="20 20";
layer="1";
};
%body.setPosition(40+$j,40);
%head.mount(%body,yourxoffsetposition,youryoffsetposition,0,true,true,true,true);
$j++;
%head=%body;
}

}


try it
#6
12/18/2006 (3:06 am)
So I tried the last code, I keep getting an error though. Some parse error, since I am using torsion at the moment.

Here is the code

$j=25;


function playerSnake::onLevelLoaded(%this, %scenegraph)
{
%head=%this
for(%i=1;%i<4;%i++)[b]<-----this is where I get the parse error[/b]
   {
   %body= new T2dStaticSprite ()
      {
      imageMap = GreentileImageMap;
      scenegraph= scenewindow2d.getscenegraph();
      frame = "0";
      size="20 20";
      layer="1";
      };
   %body.setPosition(40+$j,40);
   %head.mount(%body,1,1,0,true,true,true,true);
   $j++;
   %head=%body; [b]<------Question: why does the head need to equal the body at the end?[/b]
   }
}
#7
12/18/2006 (8:29 am)
You're getting a parse error because there's a missing semicolon at the end of the previous line.
#8
12/18/2006 (10:05 am)
O_O!!! Gosh! I really should have saw that one LOL! Thanks!!!!
#9
12/18/2006 (4:46 pm)
Yea that corrected the error but now no images are showing up on the field at all. >.<;;; gonna play around with it a bit.
#10
12/18/2006 (9:15 pm)
The head is equal body to attach the next body on the previous body
. hmmm strange
, ok i think first you must try create on the fly 1 image for example just head only
to do it
on your TGB
create it save , your level
open your file *.t2d on textpad
then copy
the snake's head creation script
but don't forget to add

scenegraph= scenewindow2d.getscenegraph();

is it worked??
#11
12/19/2006 (3:49 am)
Ummm! YAY!!! So I got it to work a little differently then you showed me but it worked YAYAY!!! >.<;;; problem is it doesn't work exactly as I hoped. I hope the other objects would move like they do in the game snake. So you know the best way to achieve that???? Can I use mount force or something????

Anyways here is the code I used to get it to work!!! THANKS FOR YOUR HELP!! =O!

$j=25;

function playerSnake::onLevelLoaded(%this, %scenegraph)
{
$head=%this;
for(%i=1;%i<4;%i++)
   {
   %body= new T2dStaticSprite()
       {
       scenegraph= scenewindow2d.getscenegraph();
       };
         
   %body.setImageMap(GreentileImageMap);
   %body.setSize("3.728 3.659"); 
   %body.setPosition("-25+$j -25");
   $head.mount(%body,2,0,0,true,true,true,true);
   $j++;
   $head=%body; 
   }
 }