【问题标题】:Issue adding multiple Polygon objects to canvas将多个多边形对象添加到画布的问题
【发布时间】:2013-10-12 15:25:50
【问题描述】:

我目前在 C# WPF 中编写经典街机游戏 Asteroids 以获得一些练习。 我遇到了一个似乎无法解决的问题。

我在生成小行星并添加到包含我所有游戏对象的画布元素时遇到了问题。

我有一个 generateAsteroids 方法,该方法每 20 毫秒调用一次,同时更新玩家船只位置等方法。 generateAsteroids 方法执行各种计算(函数中的 cmets)以确定要添加到 asteroidCollection 列表中的小行星数量。这一切都很好。

当我尝试将小行星多边形对象添加到游戏画布时出现问题。

我收到以下错误:“ArugementException 未被用户代码处理:指定的视觉对象已经是另一个视觉对象的子对象或 CompositionTarget 的根”

现在我明白这意味着什么(我认为),所有的小行星物体都被称为“小行星”,这显然并不理想,我研究并发现你不能动态地为物体动态创建变量名称。

我尝试在每次将多边形添加到画布时给多边形一个动态名称。

知道这个问题的任何人都可以帮助我吗?

我已经添加了所有我认为相关的代码,如果您需要查看更多内容,请告诉我。

谢谢

C#:

public void drawAsteroid(Asteroid theAsteroid)
{
    // entityShape is a Polygon object
    theAsteroid.entityShape.Name = "asteroid" + this.asteroidsAdded.ToString();
    theAsteroid.entityShape.Stroke = Brushes.White;
    theAsteroid.entityShape.StrokeThickness = 2;
    theAsteroid.entityShape.Points = theAsteroid.getEntityDimensions();
    gameCanvas.Children.Add(theAsteroid.entityShape);
}

// Called every 20 milliseconds by method that updates the game canvas. Possibly quite inefficient 
public void generateAsteroids()
{
    // Number of asteroids to add to the collection = the length of the game so far / 3, then subtract the amount of asteroids that have already been added
    int asteroidNum = Convert.ToInt32(Math.Ceiling((DateTime.Now - gameStartTime).TotalSeconds / 3));
    asteroidNum -= asteroidsAdded;

    for (int i = 0; i <= asteroidNum; i ++)
    {
        asteroidCollection.Add(new Asteroid());
        this.asteroidsAdded += 1;
    }

    foreach (Asteroid asteroid in asteroidCollection)
    {
        drawAsteroid(asteroid);
    }
}

XAML:

<Window x:Name="GameWindow" x:Class="AsteroidsAttempt2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Width="1000" Height="1000" HorizontalAlignment="Left" VerticalAlignment="Top" Loaded="GameWindow_Loaded">

<Canvas x:Name="GameCanvas" Focusable="True" IsEnabled="True" HorizontalAlignment="Left" Height="1000" VerticalAlignment="Top" Width="1000" KeyDown="GameCanvas_KeyDown"  KeyUp="GameCanvas_KeyUp">
    <Canvas.Background>
        <ImageBrush ImageSource="D:\CPIT\BCPR283\Asteroids\Asteroids\AsteroidsAttempt2\Resources\SpaceBackground.jpg" Stretch="Fill"/>
    </Canvas.Background>
</Canvas>

【问题讨论】:

    标签: c# wpf canvas addchild


    【解决方案1】:

    在每次调用drawAsteroid 方法时,您都将asteroidCollection 中的所有多边形添加到画布中,无论它们是否已经添加。但是您不能将同一对象两次添加到 WPF 面板的 Children 集合中。这就是你得到异常的原因(它与Name 无关)。

    像这样更改您的代码:

    if (!gameCanvas.Children.Contains(theAsteroid.entityShape))
    {
        gameCanvas.Children.Add(theAsteroid.entityShape);
    }
    

    当然,代码仍然缺少从画布中删除不再包含在asteroidCollection 中的多边形的逻辑。您还必须添加它。


    而且您根本不需要设置 Polygon 对象的 Name,除非您以后想通过它们的名称访问它们。

    【讨论】:

      猜你喜欢
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      相关资源
      最近更新 更多