无法正确设置精灵的速度

时间:2015-05-06 01:03:05

标签: ios swift sprite-kit skphysicsbody

我在iOS8中制作类似asteriods的应用程序。我有代码创建激光射击并根据船的面部设置其速度矢量。当船在屏幕上朝上时,代码可以正常工作。当船在屏幕上向下定向时,精灵沿y轴没有或几乎没有速度。也就是说,physicsBody.velocity的.dy组件表现得好像接近于零; .dx组件行为正常。 LLDB报告.dy组件应该是什么,但精灵移动不正确。以下是相关代码:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If VideoSourcePlayer1.GetCurrentVideoFrame() IsNot Nothing Then
            Dim img As New Bitmap(VideoSourcePlayer1.GetCurrentVideoFrame)
            Dim results As String() = BarcodeReader.read(img, BarcodeReader.QRCODE)
            img.Dispose()
            If results IsNot Nothing AndAlso results.Count > 0 Then
                If results(0).IndexOf("event") <> -1 Then 
                    'Remove verification code and post to listbox
                    results(0) = results(0).Replace("event", "")
                    Dim code As String = results(0)
                    Try
                        If DataGridView2.Rows.Count >= 0 Then
                            For i As Integer = 0 To DataGridView2.Rows.Count - 1
                                Dim CellChange As String = DataGridView2.Rows(i).Cells("CODE").Value.ToString
                                If CellChange.Contains(results(0)) = True Then
                                    Timer1.Stop()
                                    MsgBox("Ticket already in system")
                                    If MsgBoxResult.Ok Then
                                        Timer1.Start()
                                    End If
                                Else
                                    Timer1.Stop()
                                    DataGridView2.Rows.Add(code)
                                    Timer1.Start()
                                End If
                            Next
                        End If
                    Catch ex As Exception
                        MessageBox.Show(e.ToString())
                    End Try
                End If
            End If
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

laserBeam.physicsBody?.velocity.dx = kLaserSpeed * cos(shipNode.zRotation)
laserBeam.physicsBody?.velocity.dy = kLaserSpeed * sin(shipNode.zRotation)