WP8에서 요소의 절대 위치를 얻고이를 사용하여 해당 요소를 새 위치로 이동하는 방법
다음 코드를 사용하여 화면을 탭하고 예상치 못한 동작이 발생하여 이미지를 이동하려고합니다.
private void SetGame()
{
ScreenWidth = Application.Current.Host.Content.ActualWidth;
ScreenHeight = Application.Current.Host.Content.ActualHeight;
var transform = littleMan.TransformToVisual(Application.Current.RootVisual);
System.Windows.Point absolutePosition = transform.Transform(new System.Windows.Point(0, 0));
manLocY = absolutePosition.Y; //vert
manLocX = absolutePosition.X; //hori
leftControlArea = (ScreenWidth / 2);
rightControlArea = (ScreenWidth / 2); // answer to the screen width;
topControlArea = (ScreenHeight / 3);
bottomControlArea = (ScreenHeight / 3) * 2; // answer to the screen height
littleManWidth = littleMan.ActualWidth;
littleManHeight = littleMan.ActualHeight;
}
private void MoveLittleMan(Vector2 tappedWhere)
{
double tapX, tapY;
tapX = tappedWhere.X; // represent where user tapped on screen width wise
tapY = tappedWhere.Y; // represent where user tapped on screen height wise
if(tapY <= topControlArea)
{
// move top
Debug.WriteLine("Move TOP - X: " + tapX + " - Y: " + tapY);
if (manLocY > littleManHeight)
{
manLocY = manLocY - skippingSteps;
Canvas.SetTop(littleMan, manLocY);
}
}
else if(tapY >= bottomControlArea)
{
//move bottom
Debug.WriteLine("Move BOTTOM - X: " + tapX + " - Y: " + tapY);
if (manLocY < (ScreenHeight - littleManHeight))
{
manLocY = manLocY + skippingSteps;
Canvas.SetTop(littleMan, manLocY);
}
}
else if (tapX <= leftControlArea && tapY > topControlArea && tapY < bottomControlArea)
{
//move left
Debug.WriteLine("Move LEFT - X: " + tapX + " - Y: " + tapY);
if (manLocX > littleManWidth)
{
manLocX = manLocX - skippingSteps;
Canvas.SetLeft(littleMan, manLocX);
}
}
else if (tapX > rightControlArea && tapY > topControlArea && tapY < bottomControlArea)
{
//move right
Debug.WriteLine("Move RIGHT - X: " + tapX + " - Y: " + tapY);
if (manLocX < (ScreenWidth - littleManWidth))
{
manLocX = manLocX + skippingSteps;
Canvas.SetLeft(littleMan, manLocX);
}
}
}
예상되는 동작은 탭에서 시스템이 화면의 어느 쪽이 탭되었는지 파악하고 작은 사람을 그 방향으로 이동시키는 것입니다.
skippingSteps = 40;
응용 프로그램이 시작되면 setGame()
기능이 실행됩니다. 내가 가진 문제는 응용 프로그램이 시작되고 화면 오른쪽을 탭하면 littleMan이 화면의 오른쪽 경계에서 너무 멀리 점프한다는 것입니다. 왼쪽을 누르면 다시 돌아 오기 시작합니다. 중단 점으로 내가 수집 한 데이터를 잘 보이지만 때 Canvas.SetLeft
또는 Canvas.SetTop
20 × 20의 GIF 이미지는 X 평면에서 520의 위치로 이동되는 littleMan 불린다.
누군가 내가 뭘 잘못하고 있는지 이해하도록 도울 수 있습니까?
좋아, 다음과 같은 방법으로 문제를 해결했습니다.
내 XAML
<Grid x:Name="LayoutRoot" Background="Transparent">
<Canvas Margin="0" HorizontalAlignment="Left" VerticalAlignment="Top">
<Image Name="littleMan" Source="images/smileyFace.gif" Stretch="None"></Image>
</Canvas>
</Grid>
내 코드 뒤에
public Game()
{
InitializeComponent();
this.Loaded += Game_Loaded;
}
void Game_Loaded(object sender, RoutedEventArgs e)
{
SetGame();
TouchPanel.EnabledGestures = GestureType.Tap;
LayoutRoot.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(LayoutRoot_ManipulationCompleted);
}
void LayoutRoot_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
switch (gesture.GestureType)
{
case GestureType.Tap:
Dispatcher.BeginInvoke(() => MoveLittleMan(gesture.Position));
break;
}
}
}
private void SetGame()
{
ScreenWidth = Application.Current.Host.Content.ActualWidth;
ScreenHeight = Application.Current.Host.Content.ActualHeight;
// littleman image is in 20 * 20
littleManWidth = 20;
littleManHeight = 20;
manLocY = (ScreenHeight / 2) + littleManHeight; //vert
manLocX = (ScreenWidth / 2) + littleManWidth; //hori
littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
leftControlArea = (ScreenWidth / 2);
rightControlArea = (ScreenWidth / 2); // answer to the screen width;
topControlArea = (ScreenHeight / 3);
bottomControlArea = (ScreenHeight / 3) * 2; // answer to the screen height
}
private void MoveLittleMan(Vector2 tappedWhere)
{
double tapX, tapY;
tapX = tappedWhere.X; // represent where user tapped on screen width wise
tapY = tappedWhere.Y; // represent where user tapped on screen height wise
if(tapY <= topControlArea)
{
// move top
Debug.WriteLine("Move TOP - X: " + tapX + " - Y: " + tapY);
if (manLocY > littleManHeight)
{
manLocY = manLocY - skippingSteps;
littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
}
}
else if(tapY >= bottomControlArea)
{
//move bottom
Debug.WriteLine("Move BOTTOM - X: " + tapX + " - Y: " + tapY);
if (manLocY < (ScreenHeight - littleManHeight -50))
{
manLocY = manLocY + skippingSteps;
littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
}
}
else if (tapX <= leftControlArea && tapY > topControlArea && tapY < bottomControlArea)
{
//move left
Debug.WriteLine("Move LEFT - X: " + tapX + " - Y: " + tapY);
if (manLocX > littleManWidth)
{
manLocX = manLocX - skippingSteps;
littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
}
}
else if (tapX > rightControlArea && tapY > topControlArea && tapY < bottomControlArea)
{
//move right
Debug.WriteLine("Move RIGHT - X: " + tapX + " - Y: " + tapY);
if (manLocX < (ScreenWidth - littleManWidth))
{
manLocX = manLocX + skippingSteps;
littleMan.Margin = new Thickness(manLocX, manLocY, 0, 0);
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다