1

我正在尝试添加一个按钮,以便它出现在我正在绘制的屏幕顶部,在 XNA/Silverlight Windows 手机游戏中。

目前地图正在它上面绘制,所以按钮看起来是不可见的。有谁知道我该如何解决这个问题?

ButtonswitchScreen是在代码的前面创建的,但未初始化。

这是我添加按钮的代码:

private void OnDraw(object sender, GameTimerEventArgs e)
{
    #region CommonStuff
    SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
    #endregion CommonStuff
    if (is3D)
    {
        OnDraw3D(sender, e);
    }
    else
    {
        OnDraw2D(sender, e);
    }
    switchScreen = new Button();
    switchScreen.Height = 20.0;
    switchScreen.Width = 100.0;
    switchScreen.Content = "Switch to Shooting Screen";
    switchScreen.Margin = new Thickness(phoneScreen.Height - switchScreen.Width - 
        20.0, 20.0, 20.0, phoneScreen.Width - switchScreen.Height - 20.0);
    switchScreen.Visibility = System.Windows.Visibility.Visible;
}

我只是测试OnDraw2D所以这里的代码:

private void OnDraw2D(object sender, GameTimerEventArgs e)
{
    spriteBatch.Begin();
    // TODO: Add your drawing code here
    map.Draw(e.ElapsedTime, e.TotalTime);

    // npc.Draw(gameTime);
}

map.Draw在这里

public override void Draw(TimeSpan elapsedTime, TimeSpan totalTime)
{
    // Draw the Sky
    gamePage.getSpriteBatch().Draw(background, Position, Color.White);

    foreach (Person person in people)
    {
        person.Draw(elapsedTime, totalTime);
    }

    base.Draw(elapsedTime, totalTime);
}

background是一个Texture.2D并且Position是一个Vector2

4

1 回答 1

0

我认为,由于您是手动创建按钮,因此您可能需要改用此代码(我假设您使用的是 Windows 窗体Button控件):

switchScreen.Location = new System.Drawing.Point(xLocation, yLocation);
switchScreen.Name = name;
switchScreen.Size = new System.Drawing.Size(xSize, ySize);
switchScreen.TabIndex = 0;
switchScreen.Text = text;
switchScreen.UseVisualStyleBackColor = true;

Controls.Add(switchScreen);

你是如何实现 Windows 窗体的?据我所知,你不能添加一个没有底层表单的按钮。

于 2013-09-05T01:14:11.923 回答