Vb6在2d侧卷轴中移动平台

时间:2011-11-07 23:47:49

标签: vb6 2d platform

好吧所以我开发了一个2d侧卷轴(平台游戏),这是非常高效的IMO,使用1个计时器,我试图让移动平台成为可能。这是我尝试和调试

 Private Sub Timer1_Timer()
'moving platforms
For f = 0 To Platform.Count - 1
    If Platform(f).Tag = "moving" Then
        For j = 0 To Platform.Count - 1
            If Collision(Platform(f), Platform(j)) And j <> f Then
                Speed(f) = Speed(f) * -1
            End If
        Next j
        Platform(f).Left = Platform(f).Left + Speed(f)
    End If
Next f

基本上,这是代码的作用: 对于所有平台,它会检查哪个平台的标签“正在移动”,如果它有标签,则移动它,但在移动它之前,看看它是否需要改变方向,因此它再次循环到所有平台看它是否需要再次改变,如果需要,应该这样做,但在这段代码中它不起作用:(

可能是什么问题?所有速度的初始值都是1,我将scalemode设置为像素,这就是为什么它如此之小。任何帮助表示赞赏

碰撞功能:

Public Function Collision(Shape1 As Control, Shape2 As Control) As Boolean
If (Shape1.Left + Shape1.Width) > Shape2.Left _
And Shape1.Left < (Shape2.Left + Shape2.Width) _
And (Shape1.Top + Shape1.Height) > Shape2.Top _
And (Shape1.Top + Shape1.Height) < Shape2.Top + Shape2.Height Then
    Collision = True
Else
    Collision = False
End If
End Function

3 个答案:

答案 0 :(得分:1)

您需要考虑几个优化:

1)与其他语言不同,vb会在语句中计算所有表达式。换句话说:

如果FunctionA = true且FunctionB = true则为DoSomething

即使FunctionA为false,也总是要运行FunctionB。在你的计时器中,你可以在碰撞检查之前在if语句中检查j是否不等于f,以避免在你已经知道你将忽略它时浪费时间检查该碰撞。

2)如果我在外循环和内循环中从1循环到3,我最终会比较:

    1 - 1
    1 - 2
    1 - 3
    2 - 1
    2 - 2
    2 - 3
    3 - 1
    3 - 2
    3 - 3

如果我很聪明,我会让我的内循环开始高于我的外循环。然后我最终得到以下结论:

    1 - 2
    1 - 3
    2 - 3

这个循环少得多,而且只有3个数字。

尝试用以下方法替换for循环行: 对于j = f + 1到Platform.Count - 1

如果你这样做,你还有额外的好处,就是不需要检查j&lt;&gt; f也是。

答案 1 :(得分:0)

此代码看起来很好。 我认为错误发生在您的碰撞代码中,因为该代码设置了您的速度。

速度(f)= - 速度(f);

你是否真的达到了速度变化? 碰撞后的速度值是多少?

答案 2 :(得分:0)

好吧,问题已经解决了。观察此碰撞功能:

Public Function Collision(Shape1 As Control, Shape2 As Control) As Boolean
    If (Shape1.Left + Shape1.Width) > Shape2.Left _
    And Shape1.Left < (Shape2.Left + Shape2.Width) _
    And (Shape1.Top + Shape1.Height) > Shape2.Top _
    And (Shape1.Top + Shape1.Height) < Shape2.Top + Shape2.Height Then
        Collision = True
    Else
        Collision = False
    End If
End Function

注意问题?在这种情况下,我正在测试碰撞的2个平台处于相同的高度。想想看,如果你在碰撞函数中看到最后一个条件:

(Shape1.Top + Shape1.Height) < Shape2.Top + Shape2.Height

由于Shape1与Shape2具有相同的Top,因此该条件为false,因为我起诉的是小于符号,而不是小于或等于签名。为了确保没有问题,我将所有符号替换为等于或等于。

Public Function Collision(Shape1 As Control, Shape2 As Control) As Boolean
    If (Shape1.Left + Shape1.Width) >= Shape2.Left _
    And Shape1.Left <= (Shape2.Left + Shape2.Width) _
    And (Shape1.Top + Shape1.Height) >= Shape2.Top _
    And (Shape1.Top + Shape1.Height) <= Shape2.Top + Shape2.Height Then
        Collision = True
    Else
        Collision = False
    End If

结束功能

你会看到那个!移动平台,在它旁边的平台之间反弹。美丽。