Mounting a 2D camera to Player
by Sam Pay · in Torque X 2D · 11/18/2009 (9:14 am) · 12 replies
Im trying to mount the camera on my player as in chapter 9 of the complete guide to TorqueX. I have done the Animated sprite tutorial and the camera mounted fine. When i try and mount the camera in chapter 9 Chopper Stike i get this error;
this is the code i put in
Private fields:
T2DSceneCamera _camera;
T2DAnimatedSprite _player;
base.BeginRun();
//get camera object
T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("scenecamera")
//get player object
T2DAnimatedSprite _player = TorqueObjectDatabase.Instance.FindObject<T2DAnimatedSprite>("player");
_camera.Mount(_player, "", true);
But it throws a NullReferenceException was unhandled and Object reference not set to an instance of an object. is all highlighted in yellow
I have looked at the Animated sprite (chapter 8) sample code and i really cannot see where i am going wrong. any help would be great
cheers
Sam
this is the code i put in
Private fields:
T2DSceneCamera _camera;
T2DAnimatedSprite _player;
base.BeginRun();
//get camera object
T2DSceneObject _camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("scenecamera")
//get player object
T2DAnimatedSprite _player = TorqueObjectDatabase.Instance.FindObject<T2DAnimatedSprite>("player");
_camera.Mount(_player, "", true);
But it throws a NullReferenceException was unhandled and Object reference not set to an instance of an object. is all highlighted in yellow
I have looked at the Animated sprite (chapter 8) sample code and i really cannot see where i am going wrong. any help would be great
cheers
Sam
About the author
doing a MSc in Software Enterprise and setting up my own Small games company. Currently working on a release for XBL Indie games and PC.
#2
a little strange because i thought when you reference somthing in Script in TorqueX it would recognize this is C# Express 2008
but thanks anyway, i can finally carry on with my work
11/18/2009 (9:55 am)
the name "scenecamera" is the name i gave my camera in the script area of TorqueX so i am pointing towards that camera. Although saying that i took out my scenecamera name from my scprit and renamed my object just camera and hey presto it works. a little strange because i thought when you reference somthing in Script in TorqueX it would recognize this is C# Express 2008
but thanks anyway, i can finally carry on with my work
#3
11/18/2009 (10:24 am)
You're welcome. I'm still digging to understand how the whole system works (I'm a TX newbie) but I've seen that the script name is good for your own objects only. To use a different camera object you need to create (programmatically) your own camera.
#4
11/18/2009 (5:15 pm)
Yeah, I think this a bug in the Torque X builder. If you change the name of your camera in the Builder, when you save the scene the camera is still called "Camera". So, like Giuseppe said, you'd have to create the camera programmitcally to name it what you want.
#5
cheers
11/18/2009 (6:09 pm)
haha cheers guys, might aswell just leave the name as "Camera" then haha. works okay for me now, and i only plan to have one camera in my game anywaycheers
#6
11/18/2009 (7:31 pm)
Well... if you want to implement a HUD it'll be a good idea to have another camera :)
#7
http://www.garagegames.com/community/forums/viewthread/97315
11/20/2009 (3:16 pm)
I love, LOVE, Johns book but it has quite a few bugs. This thread helped me out.http://www.garagegames.com/community/forums/viewthread/97315
#8
11/20/2009 (6:14 pm)
Yeah, this kind of enigmatic thing got my team mad many times. I'm glad we are pass this problems... Torque X 2d only really "works" when you got pass this issues. The same with TGB. Keep up!
#9
11/21/2009 (3:44 pm)
I like the example in the torque Docs, the airplane game shows how to mount a camera.
#10
02/16/2010 (10:19 pm)
hello, hope all are well. im having the same NullReferenceException was unhandled, Object reference not set to an instance of an object, then it highlights the _camera.Mount(_player, "", true); in yellow. what was the fix supposed to be again? thanks much.
#11
02/16/2010 (11:47 pm)
never mind, for some reason Torque changed "player" to "Player" with a capital "P", and this actually fixed it. :)
#12
02/17/2010 (10:17 am)
yes that was the same problem and soloution as what i had.
Associate Giuseppe De Francesco
DFT Games Ltd