AI Tutorial - Coding the Background (Camera)
 
It is time to add the background which will switch between day and night. This time we will code it first and then add the objects to Torque Game Builder.

The first behavior we will make it a custom scrolling camera that only scrolls left and right according to the UFO's position. Make a new script file and save it as CameraMovement.cs. Add the following code to the script file.
Next Page
Previous Page
if (!isObject(CameraScrollerX))
{
%template =
new BehaviorTemplate(CameraScrollerX);
  
%template.friendlyName =
"Camera X Scroller";
%template.behaviorType =
"Camera";
%template.description  =
"Camera X Scroller control";
  
%template.addBehaviorField(MinX,
"MinX (no units)", float, -70);
%template.addBehaviorField(MaxX,
"MaxX (no units)", float, 70);
}

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

function CameraScrollerX::onBehaviorRemove(%
this)
{
if (!isObject(moveMap))
return;

%
this.owner.disableUpdateCallback();
}

function CameraScrollerX::onUpdate(%
this)
{
%CameraPointX = %
this.owner.getpositionX();

if(%CameraPointX < %this.MinX)
{
%CameraPointX = %
this.MinX;
//makes it so the camera doesn't go off the background
}
if(%CameraPointX > %this.MaxX)
{
%CameraPointX = %
this.MaxX;
}
sceneWindow2D.setCurrentCameraPosition(%CameraPointX*  0.5, 0);
// the * 0.5 makes the camera move a little smoother
// it also makes the camera move only %50 so if you where to add this code to a platformer delete the 0.5
}
The code above simply moves the camera to the object it is mounted to. It caps out at the MinX and the MaxX. If we tested this code we couldn't tell if it was working because the background is black so let's add the background.