简体   繁体   English

我对阵列的照片路径有疑问

[英]I have a problem with a photo path for array

So Im working on a project in c# which is a space shooting game.所以我在 c# 做一个太空射击游戏的项目。 Anyways Im trying to put the bullets on an array and so far so good but when I run the game and I shoot, instead of bullets it spawns squares with x in the middle(error).无论如何,我试图将子弹放在一个阵列上,到目前为止一切顺利,但是当我运行游戏并射击时,它生成的不是子弹而是中间有 x 的正方形(错误)。 Can anyone help me with that?任何人都可以帮我吗? Here's the part of the code:这是代码的一部分:

private void shotfired(int firX)
        {
            PictureBox shot = new PictureBox();
            shot.ImageLocation = "bullet.png"; ***<-This is the photo i try to use***
            shot.Location = new Point(firX+50 , Player.Location.Y - 20);
            shot.Size = new Size(30, 40);
            shot.SizeMode = PictureBoxSizeMode.StretchImage;
            Controls.Add(shot);
            shooting.Add(shot);
            firing.Play();

        }

I tried using the whole path of the photo instead of its name but it still wouldn't work.我尝试使用照片的整个路径而不是它的名称,但它仍然不起作用。 The photo is on the program file on the PC照片在 PC 上的程序文件中

The problem is most likely that your photo is not in the same directory as your executable file.问题很可能是您的照片与可执行文件不在同一目录中。 When you use a relative path like "bullet.png", the program will look for the file in the same directory as the executable.当您使用像“bullet.png”这样的相对路径时,程序将在与可执行文件相同的目录中查找该文件。 If the photo is not there, it will not be found and the program will not be able to display it.如果照片不存在,则无法找到,程序也无法显示。

To fix this, you can either move the photo to the same directory as the executable, or you can use an absolute path to the photo.要解决此问题,您可以将照片移动到与可执行文件相同的目录,或者您可以使用照片的绝对路径。 An absolute path would be something like "C:\Users\YourName\Documents\MyGame\bullet.png".绝对路径类似于“C:\Users\YourName\Documents\MyGame\bullet.png”。 Keep in mind absolute paths are absolutely only for testing purposes as they most likely won't work on other computers请记住,绝对路径绝对仅用于测试目的,因为它们很可能不适用于其他计算机

If moving to the executable file's directory doesn't work try moving it to your solution's directory, sometimes Visual Studio will use that as the active directory, see what works.如果移动到可执行文件的目录不起作用,请尝试将其移动到解决方案的目录,有时 Visual Studio 会将其用作活动目录,看看有什么用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM