Game Development Community

Having trouble mounting the camera onto the player

by Kirsten Kane · in Torque 2D Beginner · 07/29/2013 (11:36 am) · 2 replies

My code is basically the getting started tutorial with the spaceship changed around just a little bit, except for this game I kind of want to make it more like a platformer. I can get the player to move just fine but the camera just isn't mounting onto the character like I want it to.

Here's the main.cs code

function Level1::create( %this )
{
exec("./scripts/scenewindow.cs");
exec("./scripts/scene.cs");
exec("./gui/guiProfiles.cs");


exec("./scripts/blobby.cs");


exec("./scripts/controls.cs");

Level1.ObstacleFriction = 1.5;
Level1.CameraWidth = 20;
Level1.CameraHeight = 15;
Level1.WorldWidth = Level1.CameraWidth * 10;
Level1.WorldLeft = Level1.WorldWidth * -0.5;
Level1.WorldRight = Level1.WorldWidth * 0.5;
Level1.FloorLevel = -4.5;
Level1.BackdropDomain = 31;
Level1.BackgroundDomain = 25;
Level1.TruckDomain = 20;
Level1.GroundDomain = 18;
Level1.ObstacleDomain = 15;
Level1.ProjectileDomain = 16;
Level1.ForegroundDomain = 10;
Level1.RotateCamera = true;

createSceneWindow();
createScene();
mySceneWindow.setScene(myScene);

myScene.clear();

myScene.setGravity(0, -9.8);



%this.createBackground();
%this.createFloor();


createBlobby();

mySceneWindow.mount( Level1.TehBlob, "0 0", 3, true, true );

%force = 20;
mySceneWindow.mount(player, "0 0", %force, true);
Level1.mount(sceneWindow2d, "0 0", 0, true);
new ScriptObject(InputManager);
mySceneWindow.addInputListener(InputManager);
InputManager.Init_controls();




}

function Level1::destroy( %this )
{
shipcontrols.pop();
destroySceneWindow();
InputManager.delete();
}

function Level1::createFloor(%this)
{
// Ground
%obj = new Scroller();
%obj.setBodyType( "static" );
%obj.setImage( "ToyAssets:woodGround" );
%obj.setSize( Level1.WorldWidth, 3 );
%obj.setPosition( 0, Level1.FloorLevel - (%obj.getSizeY()/2) );
%obj.setRepeatX( Level1.WorldWidth / 12 );
%obj.setSceneLayer( Level1.ObstacleDomain );
%obj.setSceneGroup( Level1.GroundDomain );
%obj.setDefaultFriction( Level1.ObstacleFriction );
%obj.setCollisionGroups( none );
%obj.createEdgeCollisionShape( -100, 0.5, 100, 0.5);
%obj.createEdgeCollisionShape( Level1.WorldWidth/-2, -2, Level1.WorldWidth/-2, 25 );
%obj.createEdgeCollisionShape( Level1.WorldWidth/2, 0, Level1.WorldWidth/2, 25 );

myScene.add( %obj );
}

function Level1::createBackground(%this)
{
%obj = new Scroller();
%obj.setBodyType( "static" );
%obj.setImage( "Level1:skyBackground" );
%obj.setPosition( 0, 0 );
%obj.setSize( "200 75" );

%obj.setSceneLayer( Level1.BackgroundDomain);
%obj.setSceneGroup( Level1.BackgroundDomain);
%obj.setCollisionSuppress();
%obj.setAwake( false );
%obj.setActive( false );
myScene.add( %obj );


}


I tried following the trucktoy tutorial but the code was really hard to follow, and whenever I try to call the scenewindow.mount function it doesn't work or crashes.

Any help would be much appreciated

About the author

Fourth year computer science student at University of California Irvine, working as an intern in web development. I've always had a passion for games and have just begun the jump into game development


#1
07/29/2013 (11:56 am)
Hi Kirsten,

One thing that might be problematic is that you seem to call the mount function several times :

...
mySceneWindow.mount( Level1.TehBlob, "0 0", 3, true, true );
mySceneWindow.mount(player, "0 0", %force, true);
Level1.mount(sceneWindow2d, "0 0", 0, true); 
...

Calling .mount several times will yield errors as the Scenewindow will not allow you to mount it to an object if it is already mounted to another object. You should make sure to call mySceneWindow.dismount(); before mounting the camera to another object.

Also, I don't think Level1.mount would work as the object Level1 does not exist in the scene; it is a container object which simply ensures that everything it contains is destroyed when Level1 is destroyed.

The following line *should* work as is :
mySceneWindow.mount(player, "0 0", %force, true);

Just bear in mind that the %force variable will change how laggy the camera will be when following your player.

Let us know if this helps!
#2
07/29/2013 (12:18 pm)
Ah, I see. I guess I couldn't keep track of different code from different attemps at trying this. That last bit of mount code worked after I mounted it to the correct instance of the player sprite. Turns out it was too early in the code and it wasn't seeing it.

Thanks for the guidance!