Click here to Skip to main content
15,893,381 members
Articles / Mobile Apps

Windows Phone Labyrinth

Rate me:
Please Sign up or sign in to vote.
4.95/5 (53 votes)
31 Jan 2012CPOL10 min read 131.1K   53.8K   115  
A Windows Phone application using accelerometer emulator and Farseer physics engine
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace FarseerPhysics.SamplesFramework
{
    public sealed class VirtualButton
    {
        private Texture2D _sprite;
        private Vector2 _origin;
        private Rectangle _normal;
        private Rectangle _pressed;
        private Vector2 _position;

        public bool Pressed;

        public VirtualButton(Texture2D sprite, Vector2 position, Rectangle normal, Rectangle pressed)
        {
            _sprite = sprite;
            _origin = new Vector2(normal.Width / 2f, normal.Height / 2f);
            _normal = normal;
            _pressed = pressed;
            Pressed = false;
            _position = position;
        }

        public void Update(TouchLocation touchLocation)
        {
            if (touchLocation.State == TouchLocationState.Pressed ||
                touchLocation.State == TouchLocationState.Moved)
            {
                Vector2 delta = touchLocation.Position - _position;
                if (delta.LengthSquared() <= 400f)
                {
                    Pressed = true;
                }
            }
        }

        public void Draw(SpriteBatch batch)
        {
            batch.Draw(_sprite, _position, Pressed ? _pressed : _normal, Color.White, 0f, _origin, 1f, SpriteEffects.None, 0f);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions