private void setLeft(int i, int j) { sign1: for (int h = 0; h < 4; h++) { if (i == h && j != 0) { for (int w = j; w > 0; w--) { TextView ahead = findViewById(name[h][w - 1]);/*前面的TextView*/ TextView local = findViewById(name[h][w]);/*后面的TextView*/ if (ahead.getText().toString() != "" && ahead.getText().toString().equals(local.getText().toString())) { SoundPlayUtils.play(1); int num = parse(ahead.getText().toString()); ahead.setText(num + num + ""); ahead.startAnimation(compot); local.setText(""); local.setBackgroundResource(R.drawable.text_bg); isOver[h][w - 1] = true; isOver[h][w] = false; ChangStyle(ahead, local); setScore(ahead); break sign1; } if (ahead.getText().toString() == "") { isOver[h][w - 1] = true; isOver[h][w] = false; ahead.setText(local.getText().toString() + ""); local.setText(""); local.setBackgroundResource(R.drawable.text_bg); ChangStyle(ahead, local); } } } } }
时间: 2024-02-10 13:35:29 浏览: 23
这是一个 Java 代码段,看起来是在移动一个类似于2048的游戏的方块。其中方法 setLeft(i,j) 是用来将 (i,j) 格子上的方块往左移动的。在移动过程中,如果方块旁边的格子与其数字相同,就将两个方块合并,同时播放音效,增加分数,并改变视觉效果。如果旁边的格子为空,就将本格子的方块移动到那个空格子上,同时改变视觉效果。
相关问题
private void AddThumb(ref Thumb thumb, Cursor cursor) { thumb = new Thumb(); thumb.Cursor = cursor; thumb.Width = thumb.Height = 10; thumb.Background = new SolidColorBrush(Colors.White); thumb.BorderBrush = new SolidColorBrush(Colors.Black); thumb.BorderThickness = new Thickness(1); thumb.DragDelta += Thumb_DragDelta; visualBrush.Visual = thumb; visualBrush.Viewbox = new Rect(0, 0, 10, 10); visualBrush.Viewport = new Rect(0, 0, 10, 10); visualBrush.Stretch = Stretch.None; Canvas.SetLeft(thumb, -5); Canvas.SetTop(thumb, -5); thumbCanvas.Children.Add(thumb); }
这段代码看起来是在向一个 Canvas 中添加一个 Thumb 控件,并对其进行了一些属性设置。具体来说,代码中创建了一个 Thumb 对象,设置了其宽度、高度、背景颜色、边框颜色和边框厚度等属性,并为其绑定了 DragDelta 事件处理程序。此外,还通过 VisualBrush 将 Thumb 控件添加到 Canvas 中,并设置了其在视觉上的位置和大小。最后,将 Thumb 添加到 ThumbCanvas 中。
private void MyControl_MouseMove(object sender, MouseEventArgs e) { if(sender is UIElement uIElement) { if (isDragging) { Point newPoint = e.GetPosition(canvas); double deltaX = newPoint.X - startPoint.X; double deltaY = newPoint.Y - startPoint.Y; Canvas.SetLeft(uIElement, Canvas.GetLeft(uIElement) + deltaX); Canvas.SetTop(uIElement, Canvas.GetTop(uIElement) + deltaY); startPoint = newPoint; } } } private void MyControl_MouseDown(object sender, MouseButtonEventArgs e) { if (sender is UIElement uIElement) { isDragging = true; startPoint = e.GetPosition(canvas); uIElement.CaptureMouse(); } } private void MyControl_MouseUp(object sender, MouseButtonEventArgs e) { if (sender is UIElement uIElement) { isDragging = false; uIElement.ReleaseMouseCapture(); } }令移动的控件不超过Canvas的边界
可以在 `MyControl_MouseMove` 方法中添加一些代码来检查移动的控件是否超过了 `Canvas` 的边界。具体实现方法如下:
```
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (sender is UIElement uIElement)
{
if (isDragging)
{
Point newPoint = e.GetPosition(canvas);
double deltaX = newPoint.X - startPoint.X;
double deltaY = newPoint.Y - startPoint.Y;
// 计算控件新的位置
double newX = Canvas.GetLeft(uIElement) + deltaX;
double newY = Canvas.GetTop(uIElement) + deltaY;
// 检查控件是否超过 Canvas 的边界
if (newX < 0)
{
newX = 0;
}
else if (newX + uIElement.ActualWidth > canvas.ActualWidth)
{
newX = canvas.ActualWidth - uIElement.ActualWidth;
}
if (newY < 0)
{
newY = 0;
}
else if (newY + uIElement.ActualHeight > canvas.ActualHeight)
{
newY = canvas.ActualHeight - uIElement.ActualHeight;
}
Canvas.SetLeft(uIElement, newX);
Canvas.SetTop(uIElement, newY);
startPoint = newPoint;
}
}
}
```
在上述代码中,我们首先计算出控件的新位置 `newX` 和 `newY`,然后通过一些判断来确保控件不超过 `Canvas` 的边界。如果控件超过了边界,则将其位置设置为边界的位置。最后,我们调用 `Canvas.SetLeft` 和 `Canvas.SetTop` 方法来更新控件的位置。