Question

How would I record last mouse click ?

I have a program where I have images called red_balloon and green_balloon.

I give the user points depending if they click the balloons in the right order, red_balloon then green_balloon.

If the order is not correct they loose a point.

So I need a way to record last click, is there a simple way of doing this.

Here is my code so far..

Code XAML

<Image Height="53" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="red_balloon" Stretch="Fill" VerticalAlignment="Top" Width="48" Source="red.png" MouseLeftButtonDown="redballoon_click" Canvas.Left="194" Canvas.Top="161" RenderTransformOrigin="0.938,0.536" />
<Image Height="53" HorizontalAlignment="Left" Margin="0,0,0,0" x:Name="green_balloon" Stretch="Fill" VerticalAlignment="Top" Width="48" Source="green.png" MouseLeftButtonDown="redballoon_click" Canvas.Left="194" Canvas.Top="161" RenderTransformOrigin="0.938,0.536" />

Code c#

 private void greenballoon_click(object sender, MouseButtonEventArgs e)
        {
            if (last_click =="red_balloon") // need a way to identify the last click was red_balloon
             { 
                 PopBalloonCount++;
             }
            else 
             { 
                 PopBalloonCount--;
             }
            score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
        }
Was it helpful?

Solution 2

Why not just store the color of the last balloon click in an instance variable in the same class as your handlers:

// I've used a string here to match your posted code. However, you would likely
// be better off with a "BalloonType" enum
private string lastBalloonClickColor;

 private void greenballoon_click(object sender, MouseButtonEventArgs e)
        {
            if (this.lastBalloonClickColor =="red_balloon")
             { 
                 PopBalloonCount++;
             }
            else 
             { 
                 PopBalloonCount--;
             }
            score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
            this.lastBalloonClickColor = "green_balloon"; // register the last click
        }

 private void redballoon_click(object sender, MouseButtonEventArgs e)
        {
            if (this.lastBalloonClickColor =="green_balloon")
             { 
                 PopBalloonCount++;
             }
            else 
             { 
                 PopBalloonCount--;
             }
            score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
            this.lastBalloonClickColor = "red_balloon"; // register the last click
        }

OTHER TIPS

You need several things:

1) Add the a variable at the top of your class to hole the value of the last click. You can decide the best type for the variable, but I will use string here for simplicity:

public class xxx {
    Private string last_click = null;

2) In the click event of the red balloon, assign the value to the variable:

private void redballoon_click(object sender, MouseButtonEventArgs e) {
    last_click = "red_balloon";
}

3) In the click event if the green balloon, you can now check the value of the variable, and don't forget to assign the new value to the variable after that:

private void greenballoon_click(object sender, MouseButtonEventArgs e) {
    if (last_click =="red_balloon")
    { 
        PopBalloonCount++;
    }
    else 
    { 
        PopBalloonCount--;
    }
    last_click = "green_ballon";
    score.Content = "Your Score" + " " + Convert.ToString(PopBalloonCount);
}

How about using a boolean? If you don't need more than 2 possiblities ( = baloons), a boolean would fit perfectly.

If you need more than 2 possibilities, use an enumeration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top