Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
XNA 4.0 Game Development by Example: Beginner's Guide

You're reading from   XNA 4.0 Game Development by Example: Beginner's Guide The best way to start creating your own games is simply to dive in and give it a go with this Beginner‚Äôs Guide to XNA. Full of examples, tips, and tricks for a solid grounding.

Arrow left icon
Product type Paperback
Published in Sep 2010
Publisher Packt
ISBN-13 9781849690669
Length 428 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Kurt Jaegers Kurt Jaegers
Author Profile Icon Kurt Jaegers
Kurt Jaegers
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

XNA 4.0 Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
Preface
1. Introducing XNA Game Studio FREE CHAPTER 2. Flood Control – Underwater Puzzling 3. Flood Control – Smoothing Out the Rough Edges 4. Asteroid Belt Assault – Lost in Space 5. Asteroid Belt Assault – Special Effects 6. Robot Rampage – Multi-Axis Mayhem 7. Robot Rampage – Lots and Lots of Bullets 8. Gemstone Hunter – Put on Your Platform Shoes 9. Gemstone Hunter – Standing on Your Own Two Pixels Index

Time for action – staying in bounds


  1. Create a region called "Movement Limitations" in the Player class:

    #region Movement Limitations
    #endregion
  2. Inside the Movement Limitations region, add the clampToWorld() method:

    private static void clampToWorld()
    {
        float currentX = BaseSprite.WorldLocation.X;
        float currentY = BaseSprite.WorldLocation.Y;
    
        currentX = MathHelper.Clamp(
            currentX, 
            0, 
            Camera.WorldRectangle.Right - BaseSprite.FrameWidth);
    
        currentY = MathHelper.Clamp(
            currentY,
            0,
            Camera.WorldRectangle.Bottom - BaseSprite.FrameHeight);
    
        BaseSprite.WorldLocation = new Vector2(currentX, currentY);
    }
  3. Add a declaration to the Player class to define the area in which the camera should attempt to keep the player:

    private static Rectangle scrollArea = 
        new Rectangle(150, 100, 500, 400);
  4. Add the repositionCamera() helper method to the Movement Limitations region of the Player class:

    private static void repositionCamera(
        GameTime gameTime...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image