XNA实践鼠标篇--根据点击坐标移动图片(四)下
2010-11-29 00:23
375 查看
/// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; /// <summary> /// 图片的位置 /// </summary> private Vector2 picPosition; /// <summary> /// 当前鼠标点击的位置 /// </summary> private Vector2 CurrentPosition; /// <summary> /// 移动因子 /// </summary> private Vector2 MovePix; /// <summary> /// 定义2D的纹理图片 /// </summary> private Texture2D texture; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true;//显示鼠标 //初始化鼠标点击游戏界面的坐标 picPosition = new Vector2(); MovePix = new Vector2(1.0f, 1.0f);//移动一个像素 } /// <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 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); texture = Texture2D.FromFile(graphics.GraphicsDevice, "test.jpg"); // 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(); MouseState mouseState = Mouse.GetState(); if (mouseState.LeftButton == ButtonState.Pressed) { Viewport vp = graphics.GraphicsDevice.Viewport;//获取视窗 if (mouseState.X < vp.X || mouseState.X > vp.X + vp.Width || mouseState.Y < vp.Y || mouseState.Y > vp.Y + vp.Height) { //点击坐标不在窗体内就不管了 } else { if (CurrentPosition.X != mouseState.X || CurrentPosition.Y != mouseState.Y) { CurrentPosition.X = mouseState.X; CurrentPosition.Y = mouseState.Y; } } } Mover(); // 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(texture, picPosition, Color.White); spriteBatch.End(); base.Draw(gameTime); } /// <summary> /// 开始移动 /// </summary> void Mover() { Vector2 v = picPosition - CurrentPosition;//向量相减 if (v.X > 0) picPosition.X -= MovePix.X;//向左移动 else if (v.X + texture.Width < 0) picPosition.X += MovePix.X;//向右移动 if (v.Y > 0) picPosition.Y -= MovePix.Y;//向上移动 else if (v.Y + texture.Height < 0) picPosition.Y += MovePix.Y;//向一移动 } }
图片还是使用上次的2维图片,现在在非图片覆盖区按住鼠标的左键并移动鼠标看看,图片是不是跟着鼠标的方向慢慢移动了,看到这里,大家会想到什么了么?
对了,就是游戏中的角色对怪的位置跟踪,但在这张图上只体现了F(G+H),因为窗体没有任何的障碍物。
相关文章推荐
- XNA实践鼠标篇--根据点击坐标移动图片(四)
- jQuery获取当前鼠标相对位置坐标和点击图片跳转上一张或下一张功能
- js根据鼠标移动速度背景图片自动旋转的方法
- opencv学习笔记11 在图片中添加文字,点击鼠标显示位置坐标
- 2张图片叠放,根据鼠标的移动来显示
- 连接图片(二),实现根据鼠标方向,平滑移动
- qt通过点击按钮跳转当前图片坐标实现图片移动
- 图片的倾斜,点击按钮和鼠标移动到图片上
- 让图片直线移动到鼠标点击位置,类似游戏里的人物走动!
- Jquery之鼠标移动到图片上显示文字并且文字可以点击
- 鼠标点击图片后移动事件
- 鼠标移动到图片出现按钮,点击删除图片
- 鼠标移动改变背景色 鼠标点击后给据设定的值添加图片到指定的列
- 获取鼠标在图片中点击的坐标
- 监听鼠标移动和点击事件并且通过发送广播的形式发出这个绝对坐标
- 分享一个Unity3D点击模型跟随鼠标移动的小脚本(包括屏幕视图到三维空间的坐标转换)
- 让图片直线移动到鼠标点击位置,类似游戏里的人物走动!
- js根据鼠标移动速度背景图片自动旋转的方法
- 根据鼠标点击位置移动物体
- MFC实现当鼠标移动到button按钮上时是显示一张图片,移开显示另外一张,点击是显示另外一张