PictureBox更改为与我想要的相反

时间:2018-11-08 13:40:38

标签: c#

因此,我正在为我的编程课进行一个二分之一的项目,并且正在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();
    }
}
    }

1 个答案:

答案 0 :(得分:2)

问题出在这一行:

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...,都应使用本地副本。