fish tut/ random layer
by David Sinkler · in iTorque 2D · 05/30/2011 (10:18 pm) · 3 replies
i want to add a randomness to the fish swimming in-between the layers.
I know i can set each fish to a specific layer, but i want to make it so they when they flip not only do they haVE a random position, but a random layer as well.
i tried several things like adding %this.setLayer(getRandom( 1 - 15)); and even tried changing the datablock. i deleted the dso and rebuilt. in the datablock i set the layer " 1 - 15";
i searched the reference and found there is no setLayer, so does anyone have any suggestions? the more detail you can give would be good, i'm under the noob category.
i'm using 1.4.1
thanks
I know i can set each fish to a specific layer, but i want to make it so they when they flip not only do they haVE a random position, but a random layer as well.
i tried several things like adding %this.setLayer(getRandom( 1 - 15)); and even tried changing the datablock. i deleted the dso and rebuilt. in the datablock i set the layer " 1 - 15";
i searched the reference and found there is no setLayer, so does anyone have any suggestions? the more detail you can give would be good, i'm under the noob category.
i'm using 1.4.1
thanks
About the author
#2
05/31/2011 (7:46 am)
If you look at the MyProjects/Aquarium demo, that code is in there. Here is a sample from the code:// This function will be called when the object
// hits its world limits boundaries
//
// %this - The object calling the function
// %mode - The mode setting for the world limit
// %limit - Which world limit object reached, "right", "left", "top", "bottom"
function FishClass::onWorldLimit(%this, %mode, %limit)
{
// Fish has turned around, so set a new random speed
%this.setSpeed();
%layer = getRandom(0, 5);
// Set up a string comparison switch based on the %limit
switch$ (%limit)
{
// Fish hit "left" boundary
// Make it face right and go in that direction
// Set a random position in the Y axis
case "left":
%this.setLinearVelocityX(%this.speed);
%this.setLinearVelocityY(getRandom(-10, 10));
%this.setFlipX(false);
%this.setPositionY(getRandom(-134, 107));
%this.setLayer(%layer);
// Fish hit "right" boundary
// Make it face left and go in that direction
// Set a random position in the Y axis
case "right":
%this.setLinearVelocityX(-%this.speed);
%this.setLinearVelocityY(getRandom(-10, 10));
%this.setFlipX(true);
%this.setPositionY(getRandom(-134, 107));
%this.setLayer(%layer);
}
}
#3
%this.setTorqueVelocityY++.
thank you
05/31/2011 (5:37 pm)
thank you. I didn't look at the demo code. i followed the fish tut strictly. I love how the fish tut is so detailed and rich with comments. I feel as a noob, very comfortable that i'm typing in valid code and not wondering if I screwed up somewhere, or the one who wrote the tut screwed up. I'm comfortable knowing that if it don't compile then I made a mistake somewhere. I hope you plan on more tuts, cause you can never stop learning.%this.setTorqueVelocityY++.
thank you
Simon Almqvist Pettersson
Dohi Sweden
And if you want to control which objects get drawn on top of each other when they are on the same layer you can use Sort Points.