我不敢相信我被困在这个上面,目前我的玩家代码允许玩家四处走动,甚至允许玩家在按下Ctrl和K按钮时自杀。现在我的问题是,当玩家自杀时,它会在角落画一串文字,并带有一个有趣的陈述。但我不希望这是唯一的一个,我希望它根据你自杀,我不知道究竟如何把这个变成运动,我的继承人播放器代码3个不同的语句之间交替:如何在XNA中调用一个随机字符串?
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.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Teir_Tactical_2A
{
public class Player
{
public Random random;
public Texture2D playerunarmed;
public Texture2D playerM1911;
public Texture2D playerM4;
public KeyboardState keyState;
public SpriteFont Font1;
public KeyboardState oldKeyboardState;
public Vector2 playerPosition;
public SpriteBatch spriteBatch;
public float Angle { get; set; }
public float AngularVelocity { get; set; }
public Vector2 playerVelocity = Vector2.One;
public bool M1911;
public bool M4;
public bool player;
public bool LMBpressed;
public bool RMBpressed;
public bool isplayeralive;
public bool respawnscreen;
float angle;
public Player(ContentManager content, Vector2 location)
{
this.playerunarmed = content.Load<Texture2D>("PLAYER");
this.playerM1911 = content.Load<Texture2D>("PLAYERM1911");
this.playerM4 = content.Load<Texture2D>("PLAYERM4");
playerPosition = location;
M1911 = true;
M4 = true;
player = true;
LMBpressed = false;
random = new Random(3);
RMBpressed = false;
isplayeralive = true;
respawnscreen = false;
Font1 = content.Load<SpriteFont>("Font1");
}
public void Update()
{
MouseState curMouse = Mouse.GetState();
if (isplayeralive == false)
{
alive();
}
if (respawnscreen == true)
{
respawn();
}
if (M1911 == true)
{
armedM1911();
}
if (M4 == true)
{
armedM4();
}
if (player == true)
{
unarmed();
}
if (LMBpressed == true)
{
LMBpressedA();
}
if (RMBpressed == true)
{
RMBpressedA();
}
Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y);
Vector2 direction = mouseLoc - playerPosition;
angle = (float)(Math.Atan2(direction.Y, direction.X));
if (curMouse.LeftButton == ButtonState.Pressed)
{
LMBpressed = true;
}
if (curMouse.RightButton == ButtonState.Pressed)
{
RMBpressed = true;
}
keyState = Keyboard.GetState();
if (isplayeralive == false && LMBpressed == true)
{
isplayeralive = true;
respawnscreen = true;
}
if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyDown(Keys.K))
{
isplayeralive = false;
}
if (keyState.IsKeyDown(Keys.D1))
{
M1911 = false;
M4 = false;
player = true;
}
if (keyState.IsKeyDown(Keys.D2))
{
M1911 = true;
player = false;
}
if (keyState.IsKeyDown(Keys.D3))
{
M4 = true;
M1911 = false;
player = false;
}
if (keyState.IsKeyDown(Keys.D))
playerPosition.X += playerVelocity.X + 1;
if (keyState.IsKeyDown(Keys.A))
playerPosition.X -= playerVelocity.X + 1;
if (keyState.IsKeyDown(Keys.W))
playerPosition.Y -= playerVelocity.Y + 1;
if (keyState.IsKeyDown(Keys.S))
playerPosition.Y += playerVelocity.Y + 1;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
{
Rectangle sourceRectangle = new Rectangle(0, 0, playerunarmed.Width, playerunarmed.Height);
Vector2 origin = new Vector2(playerunarmed.Width/2, playerunarmed.Height/2);
if (M1911 == true)
{
spriteBatch.Draw(playerM1911, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
}
if (respawnscreen == true)
{
spriteBatch.DrawString(Font1, "Looks like someone commited suicide", new Vector2(968, 52), Color.Red);
}
if (player == true)
{
spriteBatch.Draw(playerunarmed, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
}
if (M4 == true)
{
spriteBatch.Draw(playerM4, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
}
if (LMBpressed == true)
{
spriteBatch.DrawString(Font1, "LMB PRESSED (shot taken prototype)", new Vector2(1000, 22), Color.GreenYellow);
}
if (RMBpressed == true)
{
spriteBatch.DrawString(Font1, "RMB PRESSED (grenade thrown prototype)", new Vector2(968, 34), Color.Red);
}
if (RMBpressed == true && LMBpressed == true)
{
spriteBatch.DrawString(Font1, "If you are seeing this, the mouse is functioning correctly", new Vector2(810, 45), Color.Black);
}
}
spriteBatch.End();
}
public void armedM1911()
{
M1911 = true;
M4 = false;
player = false;
}
public void armedM4()
{
M4 = true;
M1911 = false;
player = false;
}
public void unarmed()
{
M1911 = false;
M4 = false;
}
public void LMBpressedA()
{
LMBpressed = false;
}
public void RMBpressedA()
{
RMBpressed = false;
}
public void alive()
{
M1911 = false;
player = false;
M4 = false;
}
public void respawn()
{
player = true;
}
}
}
这里是我希望它在我的绘制方法中交替的地方。
if (respawnscreen == true)
{
spriteBatch.DrawString(Font1, "Looks like someone commited suicide", new Vector2(968, 52), Color.Red);
}
随机工作究竟如何?我将如何在这里实现它?编辑:我试着实现你说的,我得到一个错误'不能隐式转换'字符串'到'字符串[]'它奇怪的,我认为它在哪里我试图把这个,在哪里它会去吗?
EDIT2:对不起,错字就我而言,我只注意到了“[]”的第一个字符串测试后..
决赛:与您的代码后酝酿好了,我终于得到它的工作对我的期望,为了连续性,我还必须修改一些变量。
我在构造函数中加入这样的:
String suicideStr;
public bool suicide;
这到LoadContent();方法:
suicideStr = strs[random.Next(strs.Length)];
将此bool添加到Update()中;方法:
if (suicide == true)
{
suicided();
}
然后我在变量增加了它,并添加到了自杀的按钮:
if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyDown(Keys.K))
{
isplayeralive = false;
isplayerdead = true;
bloodspatter = true;
suicide = true;
}
并添加到点击重生变量:
if (isplayeralive == false && LMBpressed == true)
{
isplayeralive = true;
isplayerdead = false;
suicide = false;
suicideStr = strs[random.Next(strs.Length)];
}
然后如果自杀变量等于真,我就完成了绘图工作。
什么我认为我所做的是告诉游戏画出文本,如果玩家自杀,每次玩家点击/自杀,然后更新字符串。这是正确的想法吗?
我已经修复了你的代码,以便每次你需要一个随机数时你不会创建一个'Random'的新实例,因为这是一个*巨大的*不 - 不!但是,您的代码仍然被破坏 - 每帧显示不同的随机消息! – 2013-04-10 06:15:28
@AndrewRussell:谢谢您的通知。我会再次修改它。 – 2013-04-10 06:20:40
是的,我可以同意这一点,消息发展得很快,我以为我在矩阵中,当我回家时我会试试这个,谢谢! – 2013-04-10 16:58:40