Hi I'm having trouble using the gesture freedrag, when I drag my displayImage rectangle which is the rectangle that display my sub-image, its just couldn't be drag, help is greatly appreciate, below are my codes:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace movingsprite
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Color color = Color.White;
Texture2D ball;
Vector2 position;
Rectangle middle;
Rectangle displaySubImage;
Point point = new Point(0, 0);
bool hit = false;
float timer = 0;
AnimationSet animationSet;
// The location of sub image
int X = 0;
int Y = 0;
// The size of sub image
int height = 0;
int width = 0;
int startX, startY, endX, endY;
Random ranX;
Random ranY;
Vector2 origin;
int currentFrame = 0, currentAnimation = 0;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Extend battery life under lock.
InactiveSleepTime = TimeSpan.FromSeconds(1);
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
animationSet = SpriteAnimationManager.Read(@"Content\SpriteDescritpion.xml");
TouchPanel.EnabledGestures = GestureType.FreeDrag;
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// ball = Content.Load<Texture2D>("Round");
ball = Content.Load<Texture2D>(Path.GetFileNameWithoutExtension(animationSet.SpriteTexture.Path));
ranX = new Random();
ranY = new Random();
startX = ball.Width;
startY = ball.Height;
endX = GraphicsDevice.Viewport.Width - ball.Width;
endY = GraphicsDevice.Viewport.Height - ball.Height;
//position = new Vector2(GraphicsDevice.Viewport.Width / 2 - ball.Width / 2, GraphicsDevice.Viewport.Height / 2 - ball.Height / 2);
// middle = new Rectangle(GraphicsDevice.Viewport.Width / 2 - ball.Width / 2, GraphicsDevice.Viewport.Height / 2 - ball.Height / 2, ball.Width, ball.Height);
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (currentFrame == animationSet.Animations[currentAnimation].Frames.Length - 1)
{
currentFrame = 0;
}
if (timer > 200 && currentFrame <= animationSet.Animations[currentAnimation].Frames.Length - 1)
{
X = animationSet.Animations[currentAnimation].Frames[currentFrame].X;
Y = animationSet.Animations[currentAnimation].Frames[currentFrame].Y;
height = animationSet.Animations[currentAnimation].Frames[currentFrame].Height;
width = animationSet.Animations[currentAnimation].Frames[currentFrame].Width;
displaySubImage = new Rectangle(X, Y, width, height);
middle = new Rectangle(GraphicsDevice.Viewport.Width / 2 - width / 2, GraphicsDevice.Viewport.Height / 2 - height / 2, width, height);
position = new Vector2(GraphicsDevice.Viewport.Width / 2 - width / 2, GraphicsDevice.Viewport.Height / 2 - height / 2);
currentFrame++;
timer = 0;
}
origin = new Vector2(displaySubImage.Width / 2, displaySubImage.Height / 2);
if (timer > 1000)
{
// middle.X = ranX.Next(startX, endX);
// middle.Y = ranY.Next(startY, endY);
if (hit)
{
hit = false;
color = Color.Wheat;
}
timer = 0;
}
TouchCollection touch = TouchPanel.GetState();
if (touch.Count > 0 && touch[0].State == TouchLocationState.Pressed)
{
point = new Point((int)touch[0].Position.X, (int)touch[0].Position.Y);
currentAnimation = (currentAnimation + 1) % SpriteAnimationManager.AnimationCount;
if (middle.Contains(point))
{
hit = true;
color = Color.Red;
}
else
{
hit= false;
color = Color.Wheat;
}
}
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.FreeDrag && hit == true)
{
displaySubImage.X += (int)gesture.Delta.X;
displaySubImage.Y += (int)gesture.Delta.Y;
position += gesture.Delta;
}
}
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin();
// spriteBatch.Draw(ball, middle, color);
spriteBatch.Draw(ball,position, displaySubImage, color, 0f, origin, 2.0f, SpriteEffects.None, 0);
//spriteBatch.Draw(ball, new Vector2(400, 240), displaySubImage, color);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
