Results 1 to 8 of 8

Thread: [RESOLVED] Issue where a new instance isn't being declared

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Resolved [RESOLVED] Issue where a new instance isn't being declared

    I'm working on the game snake and I'm currently doing this:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private snake_head As Snake 'This will be the snake head and item(0) in snake_body
        Private snake_body As List(Of Snake) 'This will hold the snakes body
        Private r As New Random 'New instance at form level
        Private fruit As Snake 'Instance of a random fruit in the place_fruit sub
        Private playing As Boolean
    
    
        Private Sub new_game()
    
            game_timer.Stop()      'Stop the timer
            playing = False        'We aren't playing anymore
            Me.Controls.Clear()    'Get rid of any existing control
            snake_body.Clear()     'Get rid of the prior body parts
            snake_head = New Snake 'Declare a new instance of the head
    
            'Set some properties of the head
            With snake_head
                .BorderStyle = BorderStyle.FixedSingle
                .BackColor = Color.Honeydew
                .Location = New Point(CInt(Me.Width / 2), CInt(Me.Height / 2))
                .IsHead = True
                .MovingDirection = Snake.Direction.Left
                .Size = New Size(50, 50)
            End With
    
            snake_body.Add(snake_head)  'Add the head to the body list
    
            Me.Controls.Add(snake_head) 'Add the head to me
    
        End Sub
    
        Private Sub place_fruit()
    
            fruit = New Snake 'Declare a new instance of a snake
    
            'Set some properties
            With fruit
                .BackColor = Color.LightGreen
                .Location = New Point(r.Next(0, Me.Width - 50), r.Next(0, Me.Height - 50))
                .IsHead = False
                .MovingDirection = Snake.Direction.Left
                .Size = New Size(25, 25)
            End With
    
            Me.Controls.Add(fruit)
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            'The controls are:
            'Up - Up Arrow or W
            'Down - Down Arrow or S
            'Left - Left Arrow or A
            'Right - Right Arrow or D
            'Start/Pause - Spacebar
    
            Select Case e.KeyCode
                Case Keys.Up Or Keys.W 'Up
                    If playing Then 'Check to make sure we're plaing, then make the snake head go in that direction
                        snake_head.MovingDirection = Snake.Direction.Up
                    End If
                Case Keys.Down Or Keys.S 'Down
                    If playing Then
                        snake_head.MovingDirection = Snake.Direction.Down
                    End If
                Case Keys.Left Or Keys.A 'Left
                    If playing Then
                        snake_head.MovingDirection = Snake.Direction.Left
                    End If
                Case Keys.Right Or Keys.D 'Right
                    If playing Then
                        snake_head.MovingDirection = Snake.Direction.Right
                    End If
                Case Keys.Space
                    If Not (playing) Then 'If we aren't playing then start the timer
                        game_timer.Start()
    
                        Call place_fruit() 'To place a random fruit
                    End If
            End Select
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Set up some form properties
            With Me
                .DoubleBuffered = True
                .KeyPreview = True
                .StartPosition = FormStartPosition.CenterScreen
                .Text = "Snake"
            End With
    
            'Game_Timer's property. The ideal intervals are:
            '
            game_timer.Interval = 100
    
            Call new_game()
    
        End Sub
    End Class
    Snake Class:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Snake
        Inherits Panel
    
        Private head As Boolean
        Public Property IsHead() As Boolean
            Get
                Return head
            End Get
            Set(ByVal value As Boolean)
                head = value
            End Set
        End Property
    
        Public Enum Direction
            Up
            Down
            Left
            Right
        End Enum
    
        Private direct As Direction = Direction.Left
        Public Property MovingDirection() As Direction
            Get
                Return direct
            End Get
            Set(ByVal value As Direction)
                direct = value
            End Set
        End Property
    
    End Class
    My issue is in the 'new game' sub. I set breakpoints where I declare a new instance of the snake and where I set up some of it's properties, but when I go to debug my program those breakpoints don't ever come up. Everything prior like the clearing the list(of snake) and clearing me.controls fires, but everything after that doesn't. I was hoping someone could point out what I'm doing wrong.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Issue where a new instance isn't being declared

    Could you show us exactly where the breakpoints are in the code? It's a bit ambiguous.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Issue where a new instance isn't being declared

    Yeah I told you, they're where I declare a new instance of the snake and where I set up some of it's properties so these:
    Code:
    snake_head = New Snake 'Declare a new instance of the head
    
            'Set some properties of the head
            With snake_head
                .BorderStyle = BorderStyle.FixedSingle
                .BackColor = Color.Honeydew
                .Location = New Point(CInt(Me.Width / 2), CInt(Me.Height / 2))
                .IsHead = True
                .MovingDirection = Snake.Direction.Left
                .Size = New Size(50, 50)
            End With
    so everything that's after snake_body.Clear() won't fire at all.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Issue where a new instance isn't being declared

    Windows 7? 64-bit maybe?

    there's a part of me that hopes the answer is yes and yes... because that would "make" things "easier".

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Issue where a new instance isn't being declared

    Quote Originally Posted by techgnome View Post
    Windows 7? 64-bit maybe?

    there's a part of me that hopes the answer is yes and yes... because that would "make" things "easier".

    -tg
    yes and yes. Uh oh did I miss something!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Issue where a new instance isn't being declared

    Because there is an error, and it is in the Load event. 64 bit versions errors in the load event aren't captured (thrown?) I moved your load code to the Shown handler to find the error.

    Add new to

    Code:
        Private snake_body As New List(Of Snake) 'This will hold the snakes body
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Issue where a new instance isn't being declared

    Ahh, okie dokie. Yeah, I added it as a new instance and now it works. I should've caught that and not rely on VS to catch it for me!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Issue where a new instance isn't being declared

    You want to be very careful of code in the load event on 64 bit machines. See

    http://social.msdn.microsoft.com/For...e-b2959b1827e9
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

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