15使用处理程序检查拼图图像

时间:2013-04-25 04:05:09

标签: vb.net properties handler default picturebox

我现在要明确这个IS作业。我不希望得到完整的答案,但欢迎他们。

我被指派在Visual Basic中制作15个谜题。这是一个游戏外观的例子(这是我到目前为止):

A normal 15 puzzle game.

我需要帮助的是检查和移动图像。当我点击右下角看到的PictureBox 1时,它应该检查它下面和下面的图像,看看该位置的图像是否是My.Resources.NONE。 (NONE是我导入的png图像)。如果相邻单元格为NONE,则两个图像将相互交换。每个PictureBox在地址处都有一个处理程序,所有图像都在二维PictureBox数组(4x4)中。

我已经为它设置了一个sub,但我不确定要放在里面。有什么建议吗?

Private Sub mypic_Click(ByVal sender As Object, ByVal e As EventArgs)

End Sub

更新:

我做了一些研究并想出了一些事情。这是我目前的做法:

 Private Sub mypic_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim x, y, w, z As Integer
    ClickedPicture = DirectCast(sender, PictureBox)
    x = ClickedPicture.Left
    y = ClickedPicture.Top
    If ClickedPicture(x + 1, y).Image = Nothing Then
        ClickedPicture(x + 1, y).Image = ClickedPicture(x, y).Image
        ClickedPicture(x, y).Image = My.Resources.NONE
    End If
End Sub

我现在遇到的问题是它说ClickedPicture无法编入索引,因为它没有默认属性。这究竟意味着什么?

1 个答案:

答案 0 :(得分:0)

我建议将显示屏与逻辑分开。不要移动图片框,只需更改它们显示的图像。创建一个整数为0 - 15的4X4数组。整数可以指示从图像数组中显示哪个图片。设索引0为空方。然后当你需要“移动”一个时,只需交换数组中的值。在表单的OnPaint方法中,循环遍历4x4数组并将pictureBox图像属性设置为正确的图像。

您可以在语言规范的第9.7.3节中找到VB的默认属性:

9.7.3 Default Properties
A property that specifies the modifier Default is called a default property. Any type 
that allows properties may have a default property, including interfaces. The default 
property may be referenced without having to qualify the instance with the name of the 
property. 

我认为ClickedPicture是一个PictureBox。您正尝试使用数组表示法调用它:ClickedPicture(x + 1,y)但由于它不是数组,因此它会尝试查看是否存在可以获取索引的Default属性,但没有。