准备写个WP版打小鸟的游戏。
当鼠标点击屏幕的时候要炮火方向旋转的角度适应点击的角度;
如图:
逻辑代码:
double stageHeight = 60;//炮底座高度
Point touchPoint = new Point();//记录点击是点坐标
Size PanelSize = new Size();//保存舞台的大小
private void ContentPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
PanelSize = ContentPanel.RenderSize;
touchPoint = e.GetPosition(ContentPanel);
//弧度转角度:除以Math.PI乘以180
double res = 0;
if (touchPoint.X >( PanelSize.Width/2))
{
res = Math.Atan(((PanelSize.Height - stageHeight - touchPoint.Y) / ( touchPoint.X- PanelSize.Width/2))) / Math.PI * 180d;
t1.Rotation =(90- res);
}
else
{
res = Math.Atan(((PanelSize.Height - stageHeight - touchPoint.Y) / ( PanelSize.Width / 2- touchPoint.X))) / Math.PI * 180d;
t1.Rotation = -(90d-res );
}
txt.Text = "" + res;//弧度抓角度:处以Math.PI乘以180
}
AXML代码
<phone:PhoneApplicationPage
x:Class="Birds.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot 是包含所有页面内容的根网格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="我的应用程序"
Style="{StaticResource PhoneTextNormalStyle}"
Margin="12,0"
Name="txt"
/>
</StackPanel>
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel"
Grid.Row="1" Background="Aqua"
MouseLeftButtonDown="ContentPanel_MouseLeftButtonDown" >
<Rectangle VerticalAlignment="Bottom"
Fill="Red"
Height="60"
Width="20"
Margin="0,0,0,50"
RenderTransformOrigin="0.5,1">
<Rectangle.RenderTransform>
<CompositeTransform x:Name="t1"
Rotation="11"/>
</Rectangle.RenderTransform>
</Rectangle>
<Rectangle VerticalAlignment="Bottom"
Fill="Red"
Height="60"
RenderTransformOrigin="0.5,0.5">
</Rectangle>
</Grid>
<!--取消注释,以显示对齐网格,从而帮助确保
控件在公用边界上对齐。图像在系统栏中显示时的边距
上为 -32px。如果隐藏了系统栏,则将此值设为 0
(或完全删除边距)。
在发送之前删除此 XAML 和图像本身。-->
<!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
</Grid>