Oppure

Loading
22/07/12 19:16
Nullable
Ciao ragazzi, giusto l'altro ieri mi è venuta voglia di provare il framework XNA quindi sono partito dalla guida di Totem ( totemslair.org/guide/… ), dopo aver installato XNA Game Studio 4.0, copiando il sorgente dell'esempio e provarlo sul mio computer. Tutto fila liscio eccetto la pallina...nella funzione CollisionCheck di cui vi riporto il codice leggermente modifcato :

Private Sub CollisionCheck()
        Dim BatRect As Rectangle = New Rectangle(Bat.Position.X - (Bat.Sprite.Width / 2), Bat.Position.Y - (Bat.Sprite.Width / 2), Bat.Sprite.Width, Bat.Sprite.Height)
        Dim BallRect As Rectangle = New Rectangle(Ball.Position.X - (Ball.Sprite.Width / 2), Ball.Position.Y - (Ball.Sprite.Height / 2), Ball.Sprite.Width, Ball.Sprite.Height)

        If BallRect.Intersects(BatRect) Then
            Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
        End If

        If ((Ball.Position.X > 5) Or (Ball.Position.X > Me.GraphicsDevice.Viewport.Width - 5)) And (Ball.Position.Y < Me.GraphicsDevice.Viewport.Height - 5) Then
            Ball.Velocity += New Vector2(Ball.Position.X, -Ball.Position.Y)
        End If
    End Sub


mi restituisce un'eccezione nella seconda riga ovvero quando tenta di creare il rettangolo che delimita l'area occupata dall'oggetto, l'eccezione è di tipo OverflowException ed il messaggio è il seguente :

Overflow di un'operazione aritmetica, e mi accorgo che il numero dato dall'esecuzione di queste due istruzioni : Ball.Position.X - (Ball.Sprite.Width / 2) e Ball.Position.Y - (Ball.Sprite.Height / 2) è uguale a 0...non capisco il perché, è come se non venisse disegnata e posizionata la pallina, però se provo a fare il debug step by step ( con F11 ) l'errore non si verifica...sapete dirmi perché ?

NB: La classe GameObject è la stessa di quella di Totem quindi non la posto, piuttosto vi posto l'intero codice, nonché il resto del programma perché magari sbaglio l'ordine di istruzioni ( anche se non penso...me ne sarei accorto ) :

Public Class TryGame

    Inherits Microsoft.Xna.Framework.Game

    Private WithEvents graphics As GraphicsDeviceManager
    Private WithEvents spriteBatch As SpriteBatch
    Private BatSpeed As Single = 3
    Private BallSpeed As Single = 3
    Private Bat As GameObject
    Private Ball As GameObject

#Region "My methods"

    Private Function GetTexture(ByVal Name As String) As Texture2D
        Dim ImageStream As New System.IO.FileStream(My.Application.Info.DirectoryPath & "\" & Name, IO.FileMode.Open)
        Return Texture2D.FromStream(Me.graphics.GraphicsDevice, ImageStream)
    End Function

    Private Sub KeyboardCheck()
        Dim KeyState As KeyboardState = Keyboard.GetState()

        If KeyState.IsKeyDown(Keys.Right) Then
            Bat.Position += New Vector2(BatSpeed, 0)
        End If

        If KeyState.IsKeyDown(Keys.Left) Then
            Bat.Position += New Vector2(-BatSpeed, 0)
        End If

        If KeyState.IsKeyDown(Keys.Escape) Then
            Me.Exit()
        End If
    End Sub

    Private Sub CollisionCheck()
        Dim BatRect As Rectangle = New Rectangle(Bat.Position.X - (Bat.Sprite.Width / 2), Bat.Position.Y - (Bat.Sprite.Width / 2), Bat.Sprite.Width, Bat.Sprite.Height)
        Dim BallRect As Rectangle = New Rectangle(Ball.Position.X - (Ball.Sprite.Width / 2), Ball.Position.Y - (Ball.Sprite.Height / 2), Ball.Sprite.Width, Ball.Sprite.Height)

        If BallRect.Intersects(BatRect) Then
            Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
        End If

        If ((Ball.Position.X > 5) Or (Ball.Position.X > Me.GraphicsDevice.Viewport.Width - 5)) And (Ball.Position.Y < Me.GraphicsDevice.Viewport.Height - 5) Then
            Ball.Velocity += New Vector2(Ball.Position.X, -Ball.Position.Y)
        End If
    End Sub

#End Region

    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
    End Sub

    Protected Overrides Sub Initialize()
        MyBase.Initialize()
        spriteBatch = New SpriteBatch(GraphicsDevice)
    End Sub

    Protected Overrides Sub LoadContent()
        MyBase.LoadContent()

        Bat = New GameObject(GetTexture("baton.png"))
        Bat.Position = New Vector2(Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height - 40)

        Ball = New GameObject(GetTexture("ball.png"))
        Ball.Position = New Vector2(Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height / 2)
        Ball.Velocity = New Vector2(0, BallSpeed)
    End Sub

    Protected Overrides Sub UnloadContent()
        MyBase.UnloadContent()
    End Sub

    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        KeyboardCheck()
        CollisionCheck()
        Ball.Position += Ball.Velocity
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.Exit()
        End If
        MyBase.Update(gameTime)
    End Sub

    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        GraphicsDevice.Clear(Color.Blue)

        spriteBatch.Begin()

        Bat.Draw(spriteBatch)
        Ball.Draw(spriteBatch)

        spriteBatch.End()
        MyBase.Draw(gameTime)
    End Sub

End Class
aaa