所以这是我第一次创建类并设置父子层次结构。
在这个类中,我创建了一个十字准线,然后在 PlayerClasses.cs 中创建了一个玩家身份。我成功地为十字准线设置了屏幕边界,没有任何问题。而且我认为在Updateme
for the ship 中使用相同的代码会起作用。但它没有,我有点不知道为什么。
问题是我不知道我在调用什么矩形。我以为它会像我编码的那样m_rect
,但它似乎不起作用,将不胜感激!
下面我发布了不起作用的代码部分,然后是该类的整个代码。
public void Updateme(KeyboardState kb, GameTime gt)
{
m_velocity = Vector2.Zero;
if (kb.IsKeyDown(Keys.W))
m_velocity.Y = -1;
if (kb.IsKeyDown(Keys.S))
m_velocity.Y = 1;
if (kb.IsKeyDown(Keys.A))
m_velocity.X = -1;
if (kb.IsKeyDown(Keys.D))
m_velocity.X = 1;
m_position += m_velocity;
// Extra Task 1 PlayerShip Screenbounds
#region Extra task1
if (m_rect.X > 800)
{
m_rect.X = 800;
}
if (m_rect.X < 0)
{
m_rect.X = 0;
}
if (m_rect.Y > 480)
{
m_rect.Y = 480;
}
if (m_rect.Y < 0)
{
m_rect.Y = 0;
}
#endregion Extra task1
整个代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Sidescroller;
using Microsoft.Xna.Framework.Input;
namespace Sidescroller
{
class Crosshair : StaticGraphic
{
private Color m_tint;
private Vector2 m_center;
public Crosshair(Rectangle rect, Texture2D txr, Color tint)
: base(rect, txr)
{
m_tint = tint;
m_center = new Vector2(m_rect.Width / 2, m_rect.Height / 2);
}
public void Updateme(MouseState ms_curr)
{
m_rect.X = ms_curr.X;
m_rect.Y = ms_curr.Y;
// Extra Task 1 Crosshair Red When Clicked
#region Extra task1
if (ms_curr.LeftButton == ButtonState.Pressed)
{
m_tint = Color.Red;
}
else
{
m_tint = Color.Green;
}
#endregion Extra task1
// Extra Task 2 Rotating Crosshair
#region Extra task2
#endregion Extra task2
// Extra Task 3 Crosshair Screenbounds
#region Extra task3
if (m_rect.X > 800)
{
m_rect.X = 800;
}
if (m_rect.X < 0)
{
m_rect.X = 0;
}
if (m_rect.Y > 480)
{
m_rect.Y = 480;
}
if (m_rect.Y < 0)
{
m_rect.Y = 0;
}
#endregion Extra task3
}
public override void drawme(SpriteBatch sBatch)
{
sBatch.Draw(m_txr, m_rect, null, m_tint, 0, m_center, SpriteEffects.None, 0);
}
}
class PlayerShip : MotionGraphic
{
public PlayerShip(Rectangle rect, Texture2D txrShip)
: base(rect, txrShip)
{
}
public void Updateme(KeyboardState kb, GameTime gt)
{
m_velocity = Vector2.Zero;
if (kb.IsKeyDown(Keys.W))
m_velocity.Y = -1;
if (kb.IsKeyDown(Keys.S))
m_velocity.Y = 1;
if (kb.IsKeyDown(Keys.A))
m_velocity.X = -1;
if (kb.IsKeyDown(Keys.D))
m_velocity.X = 1;
m_position += m_velocity;
// Extra Task 1 PlayerShip Screenbounds
#region Extra task1
if (m_rect.X > 800)
{
m_rect.X = 800;
}
if (m_rect.X < 0)
{
m_rect.X = 0;
}
if (m_rect.Y > 480)
{
m_rect.Y = 480;
}
if (m_rect.Y < 0)
{
m_rect.Y = 0;
}
#endregion Extra task1
}
}
}