public class Player
{
//当前角色
public static int current_player = 0;
public int x = 0;
public int y = 0;
public int face = 1;
//是否激活
public int is_active = 0;
//图像
public Bitmap bitmap;
public Player()
{
bitmap = new Bitmap(@"Grey.png");
bitmap.SetResolution(96, 96);
}
public static void key_ctrl(Player[] player,KeyEventArgs e)
{
Player p = player[current_player];
//切换角色
if (e.KeyCode == Keys.Tab) { key_change_player(player); }
//移动处理
if (e.KeyCode == Keys.Left) { p.x = p.x - 5; p.face = 1; }
if (e.KeyCode == Keys.A) { p.x = p.x - 5; p.face = 1; }
if (e.KeyCode == Keys.Right) { p.x = p.x + 5; p.face = 2; }
if (e.KeyCode == Keys.D) { p.x = p.x + 5; p.face = 2; }
}
public static void draw(Player[]player,Graphics g, int animation_ctrl)
{
Player p = player[current_player];
Rectangle crazycoderRgl = new Rectangle(p.bitmap.Width / 7 * (animation_ctrl % 7), p.bitmap.Height / 2 * (p.face - 1), p.bitmap.Width / 7, p.bitmap.Height / 2);//定义区域
Bitmap bitmap0 = p.bitmap.Clone(crazycoderRgl, p.bitmap.PixelFormat);//复制小图
g.DrawImage(bitmap0, p.x, p.y, 280, 585);
}
//----------------------------------------------------------------
// 切换角色
//----------------------------------------------------------------
public static void key_change_player(Player[] player)
{
for (int i = current_player + 1; i < player.Length; i++)
if (player[i].is_active == 1)
{
set_player(player, current_player, i);
return;
}
for (int i = 0; i < current_player; i++)
if (player[i].is_active == 1)
{
set_player(player, current_player, i);
return;
}
}
public static void set_player(Player[] player, int oldindex, int newindex)
{
current_player = newindex;
player[newindex].x = player[oldindex].x;
player[newindex].y = player[oldindex].y;
player[newindex].face = player[oldindex].face;
}
}
我正在边学边做游戏,学的书的是罗培羽的《手把手教你用c#制作rpg游戏》,其中这一段:Player p =player[current_player],赋值的是另一个变量。之前明明都没问题,后来再打开一看p的赋值始终都是null,试过很多办法了就是没用,求解决办法,一目了然的那种。(实在是没办法了,智商太低了,在网上找了很多办法去试,就是不见效)
就这些线索,神来了也看不出来问题在哪