Game Development Community

Behavior to script?!

by Remorseless · in Technical Issues · 05/25/2010 (11:00 am) · 1 replies

Me again. Got a question now bout behaviors vs scripts. (yes i did a search and read bout this topic, but didnt find my answer).

i have a paralaxxing behavior that works great, but behaviors are on a per object basis and it seems the editor cant "group" things together to change values all at once.

so my question is, for someone like me, a total noob, is there a tutorial or anything on making a behavior a script. i guess i want more of a class. For instance, the behavior is below.

===========================================================================================================

//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
// Parallax Object Scrolling Behavior Version 1.01
// By Jeremy L. Anderson
// Thanks to Mark Shaerer, Joseph Walters, Guy Lewis, and Magnus Blikstad
// Version 2.0 Object smoothing implemented...now looks clean on use.
//-----------------------------------------------------------------------------

if (!isObject(ParallaxObjectBehavior))
{
%template = new BehaviorTemplate(ParallaxObjectBehavior);

%template.friendlyName = "Parallax Object";
%template.behaviorType = "Effects";
%template.description = "Changes object position based on camera movement.";

%template.addBehaviorField(xSpeed, "Percentage of horizontal scroll speed", float, 100);
%template.addBehaviorField(ySpeed, "Percentage of vertical scroll speed", float, 100);
}

function ParallaxObjectBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}

function ParallaxObjectBehavior::onLevelLoaded(%scenegraph)
{

%currPos = sceneWindow2d.getCurrentCameraPosition();
%this.oldPosX = getWord(%currPos, 0);
%this.oldPosY = getWord(%currPos, 1);

}

function ParallaxObjectBehavior::onUpdate(%this)
{
%currPos = sceneWindow2d.getCurrentCameraPosition();
%this.currPosX = getWord(%currPos, 0);
%this.currPosY = getWord(%currPos, 1);
%VectorX = ((%this.currPosX - %this.oldPosX)/100)*(100 - %this.xSpeed);
%VectorY = ((%this.currPosY - %this.oldPosY)/100)*(100 - %this.ySpeed);
%movSpeed = (mAbs(%VectorX) + mAbs(%VectorY))*100;
%objPosX = %this.owner.getPositionX();
%objPosY = %this.owner.getPositionY();
%targX = %VectorX + %objPosX;
%targY = %VectorY + %objPosY;
%this.owner.moveTo(%targX, %targY, %movSpeed);

%this.oldPosX = %this.currPosX;
%this.oldPosY = %this.currPosY;
}

===========================================================================================================


and it works great. but what i was hoping to do, is change this to a script so i could set numbers on a PER LAYER basis instead of a PER OBJECT basis.

is this sound or am i being stupid?

thanks

#1
05/25/2010 (4:32 pm)
You could always put all your objects from a layer in a simSet and iterate through the simSet to perform layer-wide changes.