Results 1 to 3 of 3

Thread: [RESOLVED] XNA Help

  1. #1

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Resolved [RESOLVED] XNA Help

    I have started making my game after running through some XNA tutorials but have run into a problem.

    Firstly Here is my class for creating blocks:

    Code:
    Imports Microsoft.Xna.Framework
    Imports Microsoft.Xna.Framework.Graphics
    
    Public Class Block
    
        'Define Block Objects'
        Public BlockSB As SpriteBatch = New SpriteBatch(Device)
        Public BlockTex As Texture2D = Nothing
        Public BlockRect As Rectangle = Nothing
        Public BlockTexCreationParams As TextureCreationParameters = TextureCreationParameters.Default
    
        'Define Block Variables'
        Public CanShoot As Boolean = True
        Public BlockSize As New Size(25, 25)
        Public BlockColor As String
        Public BlockPosition As Point
    
    
        Public Sub CreateBlock()
    
            BlockTex = Texture2D.FromFile(Device, "../../Resources/" & BlockColor & "Block.png", BlockTexCreationParams)
            BlockRect.Height = BlockSize.Height
            BlockRect.Width = BlockSize.Width
            BlockRect.X = BlockPosition.X
            BlockRect.Y = BlockPosition.Y
    
        End Sub
    
        Public Sub New(ByVal InitialColor As String, ByVal InitialPosition As Point)
    
            BlockColor = InitialColor
            BlockPosition = InitialPosition
    
        End Sub
    
    End Class
    Next this is my code which i am using to set up new blocks in a 2dimensional array:

    Code:
    Private Sub SetUpInitialBlocks()
    
            Dim Color As String = "Black"
    
            'First Row'
            For i As Integer = 0 To 9
    
                Blocks(i, 0) = New Block(Color, StartLoc)
                Blocks(i, 0).CreateBlock()
    
                If Color = "Black" Then
                    Color = "Red"
                Else
                    Color = "Black"
                End If
    
            Next
    
        End Sub
    Then this is my code for rendering graphics:

    Code:
    Private Sub Render()
    
            If Device Is Nothing Then Return
    
            'Clear The Back Buffer To Black'
            Device.Clear(ClearOptions.Target, Microsoft.Xna.Framework.Graphics.Color.Black, 1.0F, 0)
    
            'Draw All Non Block Textures'
            NonBlockSB.Begin(SpriteBlendMode.AlphaBlend)
            NonBlockSB.Draw(FrameTex, FrameRect, Color.White)
            NonBlockSB.Draw(GameBoardTex, GameBoardRect, Color.White)
            NonBlockSB.End()
    
            'Draw Starting Blocks'
            For X As Integer = 0 To 9
                For Y As Integer = 0 To 14
    
                    Blocks(X, Y).BlockSB.Begin(SpriteBlendMode.AlphaBlend)
                    Blocks(X, Y).BlockSB.Draw(Blocks(X, Y).BlockTex, Blocks(X, Y).BlockRect, Color.White)
                    Blocks(X, Y).BlockSB.End()
    
                Next Y
            Next X
    
            Device.Present()
    
        End Sub
    When it gets to rendering the blocks in the render sub i am getting the following error message referring to the block i am trying to draw:

    Object reference not set to an instance of an object.
    Im not sure why its throwing this message as i have the SetUpInitialBlocks Sub which is used to create the new blocks.

    I may be doing something completely wrong here as i have only just started with the XNA as advised above so if anyone has any suggestions i would appreciate it greatly.

    Thanks in advance

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: XNA Help

    Compare the SetUpInitialBlocks method and the Render method. In the first one, you have a simple loop, in the second one, you have two nested loops. Simply put, you are iterating through more elements in the second one, than in the first one. What does that tell us?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Addicted Member mouse88's Avatar
    Join Date
    Mar 2009
    Location
    South Wales, United Kingdom
    Posts
    225

    Re: XNA Help

    Problem solved, thanks a lot for the help really appreciate it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width