1

我真的在尝试我的第一个 2D 上下滚动条,但我似乎无法让背景大小完全正常工作。无论出于何种原因,它都很小,而且没有任何帮助。我认为在视差滚动类的开头,我被告知要初始化变量,并且我知道我必须更改哪个变量。

public Texture2D picture; 
        public Vector2 position = Vector2.Zero; 
        public Vector2 offset = Vector2.Zero; 
        public float depth = 0.0f; 
        public float moveRate = 0.0f; 
        public Vector2 pictureSize = Vector2.Zero; 
        public Color color = Color.White; 

我相信我必须更改pictureSize 以使其更大,因为我得到了当前尺寸!

图片

我的最后一个问题是,如何从船舶图像中删除空白?谢谢!如果需要更多代码,请告诉我,我没有把它放在那里的原因是由于长度。

编辑*:这是实际船舶渲染的代码

public void Render(SpriteBatch batch)
{
    batch.Begin();
    batch.Draw(shipSprite, position, null, Color.White, 0.0f, spriteOrigin, 1.0f,
    SpriteEffects.None, 0.0f);
    batch.End();
}

空白似乎是由 XNA 提供的,因为我已经使用 Photoshop 多次清除它。因为我不能直接上传到我把它放在这里的网站:http ://www.justbeamit.com/nk95n 而对于背景渲染它

public void Draw()        
{           
    layerList.Sort(CompareDepth);  
    batch.Begin();  
    for (int i = 0; i < layerList.Count; i++)       
    {
       if (!moveLeftRight)
       {
           if (layerList[i].position.Y < windowSize.Y)
           {
               batch.Draw(layerList[i].picture, new Vector2(0.0f,       
               layerList[i].position.Y), layerList[i].color);
           }
           if (layerList[i].position.Y > 0.0f)
               batch.Draw(layerList[i].picture, new Vector2(0.0f,   
               layerList[i].position.Y - layerList[i].pictureSize.Y),  
               layerList[i].color);
           else
               batch.Draw(layerList[i].picture, new Vector2(0.0f, 
               layerList[i].position.Y + layerList[i].pictureSize.Y), 
               layerList[i].color);
       }
       else
       {
           if (layerList[i].position.X < windowSize.X)
           {
               batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X,
               0.0f), layerList[i].color);
           }
           if (layerList[i].position.X > 0.0f)
               batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X - 
               layerList[i].pictureSize.X, 0.0f), layerList[i].color);
           else
               batch.Draw(layerList[i].picture, new Vector2(layerList[i].position.X + 
           layerList[i].pictureSize.X, 0.0f), layerList[i].color);
       }                
   }             
   batch.End();       
}  

^ 这不是我的代码,而是来自我正在阅读的关于如何执行 XNA 的书(第一次尝试 C# 到:/)

4

2 回答 2

0

Without all the relevant code and details it is a bit hard to tell the problem, But I assume your scrolling code has a spriteBatch.Draw() call simlar to this:

spriteBatch.Draw(Texture, Destination, Source, Color.White);

TheDestination is probably equal too new Rectangle(0,0,pictureSize.X,pictureSize.Y), I'm assume with no code shown that the pictureSize is the size of the picture (No really!), with the spritebatch Destination parameter, you can "stretch" your image to your viewport.

With this your PictureSize should be the size of your viewport

pictureSize = new Vector2(graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight);

You can also use the Scale parameter of the SpiteBatch to enlarge it (However it will get blurred) or just create a bigger image for your needs.

Now for the spaceship having a white background, is XNA doing this or is the image white? I assume the latter, you will need a photo editing tool such as GIMP to remove the white background of the image.

于 2013-07-03T18:45:57.837 回答
0

如何从船舶图像中删除空白?

您可以尝试使用Bitmap.MakeTransparent 方法使白色透明。像这样的东西可能会起作用:

        this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);            
        Bitmap test = new Bitmap("C:\Myimage.jpg");
        test.MakeTransparent(Color.White);
于 2013-07-03T19:10:15.010 回答