Sign in to follow this  
Followers 0
crohnsandme

trying to get camera working

4 posts in this topic

hi all , so i have a main class

 

HeliClass 

 

which holds my different scenes

 

then i have an actionscene which holds all my players , weapons,enemies etc

 

i have created a camera class and then i have created a new camera within my player class (i have also tried to create it within the actual actionscene) but i think its all ok apart from when it comes to the draw method when i have to include the camera within the spritebatch.begin(.............) but i am getting an error because obviously i have an spritebatch.begin in the main class which holds  the different scenes.... so i tried writing a new spritebatch.begin in the player/actionscene but getting the error obviously i cant begin a new one until the end has been called

 

 

can anyone help me with this problem. if needed to see any code i will paste it in if needed. i just wondered if this is a common problem people get?

 

i cant see how to create the new camera in the main heliClass because i need to pass the character values which are created within the player/action class?

0

Share this post


Link to post
Share on other sites

Firstly, yes you can only have one spritebatch running at a time and that's usually fine.

 

I'm assuming your camera is or contains a matrix which you intend to pass into .Begin. Now, ideally, your camera object should be accessible from the highest level it's needed  - in this case, the spritebatch.Begin call that would be effected by the camera (so not for any UI components). Without seeing the code it's more complicated to guess at what you're doing but I'll try and provide a few options for you.


A. remove the begin call from the main class and have the begin called in the scene. This makes sense if the scene manages drawing all the objects for the scene. Alternatively, End the spritebatch call in the main class before the draw is called on the scene if main drawing something that won't be overwritten by the scene, or begin it afterwards if it is drawing some UI components that would be overwritten. 

B. put your camera instance in the main class and pass it around by parameter or property to the child objects that need it. This method is less clean, but it is a viable solution.

 

If neither of those work out feel free to include your problematic code in a post or via pastebin and we can try an help :)

2

Share this post


Link to post
Share on other sites

ahh yes that is fine , at the moment i have took a step back and decided to clean the coding up of what i have so far ie, create a spritemanager to make it easier for others to read my code and help me learn more about making a spritemanager, but soon as i am sorted with that i will be back to look at the camera problem im glad its not a major problem and seems to be easy enough to sort out :)

 

thanks for looking

0

Share this post


Link to post
Share on other sites

ok this is annoying me now i decided because the only enemies for now will be same enemiy texture in list its pointless making a complex sprite manager when its only a 1 player simple game, so back to the camera problem

 

if i remove the sprite.begin and end from the main class none of the menus are drawn, i have tried a few different ways trying to end before the next begin and having no luck , here is the MAIN class code

 

 

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
 
            spriteBatch.Begin();
            switch (CurrentGameState)
            {
                case GameState.Playing:
                     actionScene.Draw(spriteBatch);//spriteBatch.Draw(actionBackTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                     break;
 
                case GameState.MainMenu:
                    spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                    spriteBatch.Draw(crosshairTexture,crosshairRect,null,Color.White,rotation,origin,SpriteEffects.None,0);
                    break;
                case GameState.Options:
                    spriteBatch.Draw(optionsTexture, new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
                    break;
            }
            spriteBatch.End();
            base.Draw(gameTime);
            
        }
 
and then here is the main actionscene where the player,enemies,bullets everything in game is drawn etc
 
 public void Draw(SpriteBatch spriteBatch)
        {
            //spriteBatch.Begin(SpriteSortMode.Deferred,
              //  BlendState.AlphaBlend,
                //null, null, null, null,
                //transform);
            spriteBatch.Draw(backgroundTexture,backgroundRect,Color.White);
            character.Draw(spriteBatch);
            crossHair.Draw(spriteBatch);
 
            if (firing)
            {
                foreach (Bullet bullet in bulletList)
                    bullet.Draw(spriteBatch);
            }
        }
 
ye at the moment there are no enemies, nor map tiles etc so for now its just the player the crosshair and bullets drawn
 
can someone help me find what/where to put the camera sprite.begin please thanks
0

Share this post


Link to post
Share on other sites

hahahaha omg it was so obvious after i wrote the previous post i basically had said what i needed to do lol :) now onto creating a  tiled map so i can take advantage of the working camera :)

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0