PictureBox 更改为与我想要的相反

斯蒂芬·莱丝

所以我正在为我的编程课做一个第 2 季度的项目,我正在 Visual Studio 中制作一个游戏。我知道,它可能不适合游戏,但我真的没有时间将所有工作转移到 Unity 上。所以我的问题是,我设置了代码,这样当你按下空格键时,如果玩家(顺便说一句,我正在用图片和 GIF 做的所有这些)面向右侧,他会向右挥舞他的剑,如果他在左侧,他会向左摆动。有一个计时器,完成后,将播放器动画更改回空闲状态。我测试了一下,当他面向左侧并且我按下空间时,他向左摆动并向左结束。问题是,他在面对正确的时候做同样的事情。我已经尝试了我所知道的一切,而老师却忙于帮助其他人完全学习我的编码。有人可以帮我吗?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace Sprite_The_Knight
    {
        public partial class Form1 : Form
        {

    public Form1()
    {
        InitializeComponent();
        this.BackgroundImage = Properties.Resources.Title;
        titleLabel.Show();
        startLabel.Show();
        KeyDown += new KeyEventHandler(Form1_KeyDown);
        spritePC.Image = Properties.Resources.KnightIdle;
    }

    private void EnemiesReady()
    {
        enemyNPC1.Image = Properties.Resources.Zombie;
        enemyNPC2.Image = Properties.Resources.Zombie;
        enemyNPC3.Image = Properties.Resources.Skeleton;
        enemyNPC4.Image = Properties.Resources.ZombieFlip;
        enemyNPC5.Image = Properties.Resources.ZombieFlip;
        enemyNPC6.Image = Properties.Resources.SkeletonFlip;
    }

    private void EnemiesGo()
    {

    }

    private void SpriteAttack()
    {
        if (spritePC.Image == Properties.Resources.KnightIdle)
        {
            SpriteSwingRight();
        }
        else
        {
            SpriteSwingLeft();
        }

        AttackTimer.Enabled = true;
    }

    private void SpriteSwingRight()
    {
        spritePC.Image = Properties.Resources.KnightSwing;
    }

    private void SpriteSwingLeft()
    {
        spritePC.Image = Properties.Resources.KnightSwingFlip;
    }

    private void SpriteTurnRight()
    {
        spritePC.Image = Properties.Resources.KnightIdle;
    }

    private void SpriteTurnLeft()
    {
        spritePC.Image = Properties.Resources.KnightIdleFlip;
    }


    private void SpriteShield()
    {

    }

    private void SpriteDeath()
    {

    }

    private void startLabel_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = Properties.Resources.Intro;
        titleLabel.Hide();
        startLabel.Hide();
        introLabel.Show();
    }

    private void introLabel_Click(object sender, EventArgs e)
    {
        this.BackgroundImage = Properties.Resources.Land;
        introLabel.Hide();
        spritePC.Show();
        EnemiesReady();

    }

    void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Right)
        {
            SpriteTurnRight();
        }
        else if (e.KeyCode == Keys.Left)
        {
            SpriteTurnLeft();
        }
        if (e.KeyCode == Keys.Space)
        {
            // i dunno he swings his soouurd or something
            SpriteAttack();
        }
    }

    private void AttackTimer_Tick(object sender, EventArgs e)
    {
        AttackTimer.Tick += new System.EventHandler(AttackTimer_Tick);

        AttackTimer.Start();
        if (spritePC.Image == Properties.Resources.KnightSwing)
        {
            spritePC.Image = Properties.Resources.KnightIdle;
        }
        else
        {
            spritePC.Image = Properties.Resources.KnightIdleFlip;
        }

        AttackTimer.Stop();
    }
}
    }
罗兹贝

问题出在这一行:

if (spritePC.Image == Properties.Resources.KnightIdle)

你不能像这样比较图像。因为当你打电话时,你Properties.Resources...总是会得到图像的新实例。但是,您可以保留这些图像的本地副本,以便能够比较它们。将这些字段放在表单上:

public partial class Form1 : Form
{
    Bitmap _KnightSwingFlip = Properties.Resources.KnightSwingFlip;
    Bitmap _KnightSwing = Properties.Resources.KnightSwing;
    Bitmap _KnightIdle = Properties.Resources.KnightIdle;
    // rest of code
}

然后在表格的任何地方使用它们

public Form1()
{
     // rest of code
     spritePC.Image = _KnightIdle;
}

private void SpriteAttack()
{
    if (spritePC.Image == _KnightIdle)
    // rest of the code here
}

请记住,您应该相应地更改其余代码。即无论您在哪里使用,请Properties.Resources...改用本地副本。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章