开发者

How to make a TextBlock work like a score counter in C# XAML

<Canvas x:Name="LayoutRoot" Background="white">
    <Image Source="level1.jpg"  Name="bg" Width="640" Heigh开发者_JAVA技巧t="480"
           Canvas.Top="10" Canvas.Left="50"/>
    <TextBlock Name="score">Scorehere</TextBlock> 
</Canvas>
void CompositionTarget_Rendering(object sender, EventArgs e)
{         
    if (DetectCollisionLeft(myCat, myZero))
    {
        LayoutRoot.Children.Remove(myZero);                    
    }
}

What I basically have is when my cat in the game collides with the number zero the number disappears. How can I get the TextBlock in the XAML to display a number that increases every time the number is collected.

Thank you


As I understand your problem statement, you want to update the score every time a collision is detected. If so then simply update the TextBlock.Text property to update the score.

void CompositionTarget_Rendering(object sender, EventArgs e)
{         
    if (DetectCollisionLeft(myCat, myZero))
    {
        if(LayoutRoot.Children.Contains(myZero))
        {
            LayoutRoot.Children.Remove(myZero);

            //Update the score as score = previousScore + 1
            int scoreAsInt;
            if(Int32.TryParse(score.Text, NumberStyles.Integer, CultureInfo.CurrentCulture, out scoreAsInt) != null)
            {
                scoreAsInt = scoreAsInt + 1;
                score.Text = scoreAsInt.ToString(CultureInfo.CurrentCulture);
            }
        }
    }
}

Note that you have to consider the scenario in which the score becomes too large for the integer range. In that case you can either reset the score or use a larger type like long for the score.


My first suggestion is that you need to separate your logic from the visuals. This can be done either by using the MVVM pattern, or by writing data model classes for the score and cat and moving the logic there. MVVM is probably a little overboard for a simple project like this, but there can quickly arise unnecessary problems and complexity when the visual data and the logic is mixed.

Having said that, here's a simple answer to your problem. If you want to keep the score on the screen and just update it, there is no reason to remove it at all. Just update the text value and move it to a new random location in the canvas. Something like this:

   if (DetectCollisionLeft(myCat, myZero))
   {
       Random rand = new Random();
       score.Text = int.Parse(score.Text) + 1;

       // Measure text for new random position
       score.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

       // Set the position of the text
       Canvas.SetLeft(score, rand.Next(640 + 10 - score.DesiredSize.Width));
       Canvas.SetTop(score, rand.Next(480 + 10 - score.DesiredSize.Height));
   }

There could be better ways to randomize the position of the score. For example, it would be better to pass in the width of the screen / parent container instead of using the hard coded values 640 and 480. Hopefully this points you in the right direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜