在Canvas边缘停球 [英] Stop ball at edge of Canvas

查看:100
本文介绍了在Canvas边缘停球的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<phone:PhoneApplicationPage

    x:Class="BallGame.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" d:DesignWidth="480" d:DesignHeight="768"

    FontFamily="{StaticResource PhoneFontFamilyNormal}"

    FontSize="{StaticResource PhoneFontSizeNormal}"

    Foreground="{StaticResource PhoneForegroundBrush}"

    SupportedOrientations="Portrait" Orientation="Portrait"

    shell:SystemTray.IsVisible="True">

    <Canvas x:Name="Can">
        <Ellipse x:Name="Ball" ManipulationStarted="Ellipse_ManipulationStarted_1" Fill="Brown" HorizontalAlignment="Left" Height="89" Stroke="Black" VerticalAlignment="Top" Width="93" Canvas.Left="181" Canvas.Top="0"/>
        <Rectangle x:Name="Rect" ManipulationDelta="Rect_ManipulationDelta_1" ManipulationStarted="Rectangle_ManipulationStarted_1" Fill="Blue" Height="100" Canvas.Left="138" Stroke="Black" Canvas.Top="416" Width="100">
            <Rectangle.RenderTransform>
                <TranslateTransform x:Name="RecTrans"></TranslateTransform>
            </Rectangle.RenderTransform>
        </Rectangle>
    </Canvas>

</phone:PhoneApplicationPage>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using System.Windows.Controls.Common;

namespace BallGame
{
    public partial class MainPage : PhoneApplicationPage
    {
        Accelerometer AccReader;
        double distanceToTravel;
        Boolean BallFlag;
        double AccelerationFactor;

        private double ballDirectionX = 0;
        private double ballDirectionY = 0;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            BallFlag = false;
        }

        private void Ellipse_ManipulationStarted_1(object sender, ManipulationStartedEventArgs e)
        {
            AccReader = new Accelerometer();
            AccReader.CurrentValueChanged +=AccReader_CurrentValueChanged;
            AccReader.Start();

            if (BallFlag == false)
            {
                BallFlag = true;
                Ball.Fill = new SolidColorBrush(Colors.Red);
                AccReader = new Accelerometer();
                AccReader.CurrentValueChanged += AccReader_CurrentValueChanged;
                AccReader.Start();
                BallFlag = true;
            }
            else
            {
                Ball.Fill = new SolidColorBrush(Colors.Brown);
                BallFlag = false;
            }

            if (Ball.Cursor.Equals(0))
            {
                Canvas.GetTop(Ball);
                Canvas.GetLeft(Ball);
                Canvas.GetZIndex(Ball);
            }

           }

        void AccReader_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() => ProcessBall(e));
        }

        void ProcessBall(SensorReadingEventArgs<AccelerometerReading> e)
        {
            AccelerationFactor = Math.Abs(e.SensorReading.Acceleration.Z) == 0 ? 0.1 : Math.Abs(e.SensorReading.Acceleration.Z);
            distanceToTravel = 2;

            Canvas.SetLeft(Ball, Canvas.GetLeft(Ball) + e.SensorReading.Acceleration.X / AccelerationFactor * distanceToTravel);
            Canvas.SetTop(Ball, Canvas.GetTop(Ball) - e.SensorReading.Acceleration.Y / AccelerationFactor* distanceToTravel);
        }

        private void Rectangle_ManipulationStarted_1(object sender, ManipulationStartedEventArgs e)
        {

        }

        private void Rect_ManipulationDelta_1(object sender, ManipulationDeltaEventArgs e)
        {
            //Canvas.SetLeft(Rect, Canvas.GetLeft(Rect) + e.ManipulationOrigin.X);
            //Canvas.SetTop(Rect, Canvas.GetTop(Rect) + e.ManipulationOrigin.Y);

            RecTrans.X += e.DeltaManipulation.Translation.X;
            RecTrans.Y += e.DeltaManipulation.Translation.Y;
        }

    }
}







一旦到达画布的末尾,如何保持球停滞。




How to keep the ball stagnant once it reaches the end of the canvas.

推荐答案

没有人会为你编写代码。



关于你将得到的最好的提示,它与你得到的相同,是否需要一些 if ProcessBall方法中的语句。如果想知道那些 if 语句会检查什么?
Nobody is going to write your code for you.

About the best hint you're going to get, and it's the same as you got on SO, is you're going to need a couple of if statements in your ProcessBall method. If wonder what those if statements would be checking for??


这篇关于在Canvas边缘停球的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆