Newbie seeks help creating a camera behavior (TGB)
by maanto · in Technical Issues · 06/04/2008 (12:47 pm) · 0 replies
Hi there.
I'm trying to create a behaviour in TGB that moves the camera when the player touches their world bounds.
Secondarily, I want the player's world bounds to be changed to the new "window" that the camera shifts to.
The camera dimensions I'm using are 640x480.
In other words, I want to make a platformer-style game where the camera shifts to another area on the map only when the player approaches the edges of the screen. See the Knytt game by Nifflas for an example.
The problem I'm having:
Whenever I move left or right a screen it works fine, but the second time I move, the camera doesn't change back to the previous area.
Oh, and did I mention I'm a newbie? I know my code must look very messy right now.
This is what I have so far.
I'm trying to create a behaviour in TGB that moves the camera when the player touches their world bounds.
Secondarily, I want the player's world bounds to be changed to the new "window" that the camera shifts to.
The camera dimensions I'm using are 640x480.
In other words, I want to make a platformer-style game where the camera shifts to another area on the map only when the player approaches the edges of the screen. See the Knytt game by Nifflas for an example.
The problem I'm having:
Whenever I move left or right a screen it works fine, but the second time I move, the camera doesn't change back to the previous area.
Oh, and did I mention I'm a newbie? I know my code must look very messy right now.
This is what I have so far.
if (!isObject(FollowCameraBehavior))
{
%template = new BehaviorTemplate(FollowCameraBehavior);
%template.friendlyName = "Follow Camera 1";
%template.behaviorType = "Camera";
%template.description = "Puts the camera on another screen when the player reaches world limit bounds.";
}
function FollowCameraBehavior::onBehaviorAdd(%this)
{
%this.owner.worldLimitMode = "NULL";
%this.owner.worldLimitCallback = true;
}
function FollowCameraBehavior::onWorldLimit(%this, %limitMode, %limit)
{
switch$ (%limit)
{
case "left":
if (%this.owner.getLinearVelocityX() < 0)
// change the camera position left 640 pixels from where it is currently
sceneWindow2D.setCurrentCameraPosition(getWord(sceneWindow2D.getCurrentCameraPosition(), 1) - 640, getWord(sceneWindow2D.getCurrentCameraPosition(), 2));
// set the owner's world limits left 640 pixels of where it is currently
%this.owner.setWorldLimit("null", "getword(%this.owner.getWorldLimit(), 1) - 640 getword(this.owner.getWorldLimit(), 2) getword(%this.owner.getWorldLimit(), 3) - 640 getword(%this.owner.getWorldLimit(), 4)", "true"); //
case "right":
if (%this.owner.getLinearVelocityX() > 0)
sceneWindow2D.setCurrentCameraPosition(getWord(sceneWindow2D.getCurrentCameraPosition(), 1) + 640, getWord(sceneWindow2D.getCurrentCameraPosition(), 2));
%this.owner.setWorldLimit("null", "getword(%this.owner.getWorldLimit(), 1) + 640 getword(%this.owner.getWorldLimit(), 2) getword(%this.owner.getWorldLimit(), 3) + 640 getword(%this.owner.getWorldLimit(), 4)", "true");
case "top":
if (%this.owner.getLinearVelocityY() < 0)
sceneWindow2D.setCurrentCameraPosition(getWord(sceneWindow2D.getCurrentCameraPosition(), 2) - 480, getWord(sceneWindow2D.getCurrentCameraPosition(), 1));
%this.owner.setWorldLimit("null", "getword(%this.owner.getWorldLimit(), 1) getword(%this.owner.getWorldLimit(), 2) - 480 getword(%this.owner.getWorldLimit(), 3) getword(%this.owner.getWorldLimit(), 4) - 480", "true");
case "bottom":
if (%this.owner.getLinearVelocityY() > 0)
sceneWindow2D.setCurrentCameraPosition(getWord(sceneWindow2D.getCurrentCameraPosition(), 2) + 480, getWord(sceneWindow2D.getCurrentCameraPosition(), 1));
%this.owner.setWorldLimit("null", "getword(%this.owner.getWorldLimit(), 1) getword(%this.owner.getWorldLimit(), 2) + 480 getword(%this.owner.getWorldLimit(), 3) getword(%this.owner.getWorldLimit(), 4) + 480", "true");
}
}