【问题标题】:Button on drawn object绘制对象上的按钮
【发布时间】:2013-03-23 23:08:56
【问题描述】:

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

当前地图正在其上绘制,因此该按钮看起来不可见。有谁知道我该如何解决这个问题?

按钮switchScreen 在代码前面已创建,但未初始化。

这是我添加按钮的代码:

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);
}

backgroundTexture.2DPositionVector2

【问题讨论】:

    标签: c# silverlight xna


    【解决方案1】:

    我认为,由于您是手动创建按钮,因此您可能需要改用此代码(我假设您使用的是 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 窗体的?据我所知,你不能添加一个没有底层表单的按钮。

    【讨论】: