Results 1 to 20 of 20

Thread: animation not working???

  1. #1

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Angry animation not working???

    i am doing a maze game for a project for class. I have character selection, so the code is very extensive. I am not sure why but when i press an arrow key, the character flickers between two characters. i am not sure why this is happening. this is my code:

    Code:
        Public PicCharacter As Image
    
    
    
        Private Sub frmLevel1_KeyPress(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    
            If intCharacter = 1 Then
                If Keys.Left OrElse Keys.A Then
                    charDirection = WEST
                    PicCharacterLvl1.Image = ChangeImage(Character1WalkWEST, 0.1)
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X - 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Right OrElse Keys.D Then
                    charDirection = EAST
                    PicCharacter = Character1WalkEAST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X + 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Up OrElse Keys.W Then
                    charDirection = NORTH
                    PicCharacter = Character1FlyUp
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y - 1)
                ElseIf Keys.Down OrElse Keys.S Then
                    charDirection = SOUTH
                    PicCharacter = Character1FlyDown
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y + 1)
                End If
            ElseIf intCharacter = 2 Then
                If Keys.Left OrElse Keys.A Then
                    charDirection = WEST
                    PicCharacter = Character2WalkWEST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X - 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Right OrElse Keys.D Then
                    charDirection = EAST
                    PicCharacter = Character2WalkEAST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X + 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Up OrElse Keys.W Then
                    charDirection = NORTH
                    PicCharacter = Character2FlyUp
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y - 1)
                ElseIf Keys.Down OrElse Keys.S Then
                    charDirection = SOUTH
                    PicCharacter = Character4FlyDown
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y + 1)
                End If
            ElseIf intCharacter = 3 Then
                If Keys.Left OrElse Keys.A Then
                    charDirection = WEST
                    PicCharacter = Character3WalkWEST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X - 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Right OrElse Keys.D Then
                    charDirection = EAST
                    PicCharacter = Character3WalkEAST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X + 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Up OrElse Keys.W Then
                    charDirection = NORTH
                    PicCharacter = Character3FlyUp
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y - 1)
                ElseIf Keys.Down OrElse Keys.S Then
                    charDirection = SOUTH
                    PicCharacter = Character4FlyDown
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y + 1)
                End If
            ElseIf intCharacter = 4 Then
                If Keys.Left OrElse Keys.A Then
                    charDirection = WEST
                    PicCharacter = Character3WalkWEST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X - 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Right OrElse Keys.D Then
                    charDirection = EAST
                    PicCharacter = Character4WalkEAST
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X + 1, PicCharacterLvl1.Location.Y)
                ElseIf Keys.Up OrElse Keys.W Then
                    charDirection = NORTH
                    PicCharacter = Character4FlyUp
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y - 1)
                ElseIf Keys.Down OrElse Keys.S Then
                    charDirection = SOUTH
                    PicCharacter = Character4FlyDown
                    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X, PicCharacterLvl1.Location.Y + 1)
                End If
            End If
    
        Private Sub frmLevel1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
    
            If intCharacter = 1 Then
                If charDirection = WEST Then
                    PicCharacter = Character1StandWEST
                ElseIf charDirection = EAST Then
                    PicCharacter = Character1StandEAST
                End If
            ElseIf intCharacter = 2 Then
                If charDirection = WEST Then
                    PicCharacter = Character2StandWEST
                ElseIf charDirection = EAST Then
                    PicCharacter = Character2StandEAST
                End If
            ElseIf intCharacter = 3 Then
                If charDirection = WEST Then
                    PicCharacter = Character3StandWEST
                ElseIf charDirection = EAST Then
                    PicCharacter = Character3StandEAST
                End If
            ElseIf intCharacter = 4 Then
                If charDirection = WEST Then
                    PicCharacter = Character4StandWEST
                ElseIf charDirection = EAST Then
                    PicCharacter = Character4StandEAST
                End If, it will
            End If
    
        End Sub
    
       Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick
    
            PicCharacterLvl1.Image = ChangeImage(PicCharacter, 0.1)
    
        End Sub

    The first is where my piccharacter is declared. the next is my keypress function. I am saying that if the character is the first, and you press left, the direction changes to west, the animation changes to walk west and the picture box moves to the left. I have this for every possible character and direction. the next is what i put in the key up function, saying that if i let go of the key, it will go back to stand. this standing works, but when i press a key to walk, it flickers between two characters. the last is just the line i have in the timer tick function, saying that every tick, the change image function runs. please note this is my first year in vb, so i am not that educated and if i could get a detailed explanation on my mistake, i would greatly appreciate it. Thanks so much.
    Last edited by dday9; May 30th, 2017 at 09:50 AM. Reason: Added Code Tags

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: animation not working???

    Time to add a breakpoint and do a bit of code stepping. The flickering is pretty interesting, and will take a bit of study on your part. What's really intriguing is that for flickering to happen, the display must have a chance to refresh, which probably means that MORE code is running than you are expecting to be running. Breakpoints are the way to get at this, but they may not be something you have dealt with, yet. If you haven't dealt with them, now's the time to learn, as there is no more valuable tool at your disposal than breakpoints.

    To add a breakpoint, put the cursor on the line you want to break on (the first If statement in that first method would be my first choice), and press F9. This will put a maroon ball in the left margin and color the line maroon. From then on, you can always set/remove breakpoints by clicking in the left margin. This is also fairly standard across lots of different languages/development environments.

    Next, run the program. When execution reaches a line with a breakpoint on it, the code will pause, and you'll be taken to the line. At that point, you can look at the contents of any variable in scope by either mousing over them, or clicking on them and pressing Shift+F9. You can also step forwards through the code using either F10 or F11, but more importantly, in this case, you can return to normal functioning using F5.

    The thing is, since you are seeing flickering, then the code is setting one image, then setting another, and not in the same function. If it was in the same function, the screen wouldn't get a chance to draw the first image before the second one was drawn. So, what you are looking for is multiple things happening in short order, but not in the same method. You can put breakpoints in all three of those methods and see whether they work the way you expect. A breakpoint in the timer tick will be hit every time the timer ticks, which is likely to be REALLY annoying, so you might not want a breakpoint there. However, a breakpoint in the KeyPress event handler may get hit more than once, or the KeyDown and KeyUp may be happening in ways you didn't expect. So, put breakpoints in both of those methods, and when one breakpoint is hit, just press F5 and see what happens next.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    right, this does seem to be useful, but the new problem in my other thread stops the program from even reaching the first level anymore. I have a splash screen for the level, then the level comes up for half a second and closes and the error for the changeimage comes up.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: animation not working???

    I don't really understand what you're describing. Let's talk about simplifying the code so it's easier to understand.

    Does the problem happen with only one character? If so, then please delete the other three characters. If it's not working with all of them, then it's 4x as hard to debug or read with all 4 of them present. When you get one character working, you can add the "more than one character" feature. In the future, you should think about just loading the appropriate character images into one set of variables, then you don't have to repeat the code 4x.

    Let's talk about how your code works. I think you've overcomplicated things, but I have to make several guesses to get there.

    The first problem is I don't know what ChangeImage() does. You didn't show me that code. It's important to know what it does to know the right answer, but I think it might be irrelevant. Let's keep going.

    Every timer tick, if i guess what your code does right, you set the PictureBox's image to whatever is in PicCharacter. I think you want to ensure these things happen:
    • Before walking, PicCharacter is the correct "standing" image.
    • While walking, PicCharacter is the correct "walking" image.
    • After walking, PicCharacter is the correct "standing" image.


    I don't see any proof that "before walking" is true in this code. But the KeyUp handler ensures the "after walking" part is true.

    This is not universally true in your KeyPress handler. Here's one that gets it right:
    Code:
    charDirection = EAST
    PicCharacter = Character3WalkEAST
    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X + 1, PicCharacterLvl1.Location.Y)
    Here's one that doesn't:
    Code:
    charDirection = WEST
    PicCharacterLvl1.Image = ChangeImage(Character1WalkWEST, 0.1)
    PicCharacterLvl1.Location = New Point(PicCharacterLvl1.Location.X - 1, PicCharacterLvl1.Location.Y)
    In that case, instead of setting PicCharacter, you directly set the image in the PictureBox every KeyPress event. But then, when the Timer ticks, it sets the image to what's in PicCharacter, which is the standing sprite. If your timer is fast, you'll see it flicker between the two.

    This was very hard to pick out because there are 4 cases for 4 characters for a total of 16 cases. Who knows how many of them are wrong? Here's a simple way to fix it. (More complex things are more elegant, but this is easy to understand.)

    I'd have a set of variables for the "right" images:
    Code:
    Private _standingLeft As Image
    Private _standingRight As Image
    Private _standingUp As Image
    Private _standingDown As Image
    Private _walkingLeft As Image
    Private _walkingRight As Image
    Private _walkingUp As Image
    Private _walkingDown As Image
    After character select, I'd want to call some method like this:
    Code:
    Private Sub InitializeImages(ByVal characterIndex As Integer)
        If characterIndex = 1 Then
            _standingLeft = Character1StandWEST
            _standingRight = Character1StandEAST
            ...
        Else If characterIndex = 2 Then
            _standingLeft = Character2StandWEST
            _standingRight = Character2StandEAST
            ...
        Else If...
            ...
        End If
    End Sub
    It's tedious. But when it's done, you only have one set of variables to worry about. It'd be "cleaner" to use/abuse arrays to get here, but somewhere there's going to be some tedious code to set it up. Let's live with it.

    After InitializeImages() is called, you don't need to worry about the character index anymore, just directions. So KeyPress could just be:
    Code:
    If Keys.Left OrElse Keys.A Then
        charDirection = WEST
        PicCharacter = _walkingLeft
    Else If Keys.Left OrElse Keys.D Then
        charDirection = EAST
        PicCharacter = _walkingRight
    ...
    It doesn't have to worry about "which character" because it knows the right images for the character are already loaded! KeyUp could be similarly easier:
    Code:
    If charDirection = WEST Then
        PicCharacter = _standingLeft
    Else If...
    That'll cut your tedium by 75%, and make it easier to deal with errors.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    yes, i now i could have been much more efficient, but i really didn't learn that many different methods of doing this. thanks very much for the simplification. i made a mistake in the first character west and forgot to change it, since they were all the .image method originally. although there is just one build error i am not sure how to fix.

    Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick

    PicCharacterLvl1.Image = ChangeIMage(PicCharacter, 0.1)

    End Sub

    Piccharacter is underlined and says that "Value of type 'Image' cannot be converted to 'Image()'"
    Last edited by rparikh10; May 30th, 2017 at 04:28 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    although there is just one build error i am not sure how to fix.

    Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick

    PicCharacterLvl1.Image = ChangeIMage(PicCharacter, 0.1)

    End Sub

    Piccharacter is underlined and says that "Value of type 'Image' cannot be converted to 'Image()'"

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: animation not working???

    I have to make a psychic guess because you STILL haven't showed me what ChangeImage() looks like. But I can make a guess, and you can too. Let's talk about reading error messages.

    You can't convert lead to gold. So let's use Lead and Gold as our types. Suppose I write this function:
    Code:
    Public Sub DoSomething(ByVal input As Gold)
    End Sub
    Now imagine I call it this way:
    Code:
    Dim item As New Lead()
    DoSomething(item)
    I will get an error message very similar to what you got:
    Value of type 'Lead' cannot be converted to 'Gold'.
    Do you see how they line up? Let's look at the relevant line again:
    Code:
    DoSomething(item)
    "Value of type 'Lead' is talking about the variable 'item'. It is defined as a variable with type Lead. I am trying to pass it to DoSomething(), and for some reason that caused VB to try to convert 'Lead' to 'Gold'. Why? Let's examine DoSomething() again:
    Code:
    Public Sub DoSomething(ByVal input As Gold)
    The parameter input is of type Gold. But I passed it a variable of type Lead. VB tried to convert Lead to Gold so DoSomething() could be satisfied, but it can't convert Lead to Gold.

    So in your case:

    ChangeImage() is a function that takes a parameter of type Image(), that is "Array of Image". But your PicCharacter variable is of type Image. So you have a hole that is shaped like "Array of Image", but tried to give it "Image", and you can't do that.

    Either your ChangeImage() method is asking for the wrong thing, or you're giving it the wrong thing. I can't tell you which is right. There's not a magic thing to type to make this work: you have to think about the problem, decide what you wanted to do, and rewrite the code so it does the right thing.

    (Get used to reading Noun() as "array of noun". That can get confusing because Word() could also mean "a Sub or Function named Word". VB decided to use parenthesis for both array indices and parameter lists. Other languages, which VB developers claim are hard to learn, use [] square brackets so you can't get Word[] (array of word) mixed up with Word() (a Function/Sub named Word).)
    Last edited by Sitten Spynne; May 30th, 2017 at 04:47 PM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    imgArray was Nothing.

    This error comes up for my changeimage function for a game i am making. This is the code:


    Public Function ChangeIMage(ByVal imgArray() As Image, ByVal imgIncrement As Decimal) As Image
    '
    imgCounter += imgIncrement

    If CInt(imgCounter) > imgArray.GetUpperBound(0) Then
    imgCounter = 0
    End If

    Return imgArray(CInt(imgCounter))
    End Function


    It was working up until now when i was working on the project. not sure what the error means and how fix it. sorry, but i have this on another thread. it is also a problem that no one can seem to figure out.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: animation not working???

    But that's the other thread.

    A NullReferenceException means that a reference type variable was Nothing, and you tried to access it. In the code you posted, the ONLY variable that is reference type is imgArray. The other variables are all value types. Value types are simple things, like numbers, dates....and kind of strings, though their weird. So, if imgArray is Nothing, then when you try to access the GetUpperBound(0) method, you are trying to access a method on Nothing, and Nothing has no members, so you get that exception.

    (by the way, .Length() would be easier to type than GetUpperBound(0), and would do the same thing).
    My usual boring signature: Nothing

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: animation not working???

    No one can figure it out because you aren't posting enough code, and you aren't using the forum's code tags to make it easy for people to read it.

    Imagine if I asked you to name the item on my desk next to my left hand. Can you? Why not? You can't see my desk, can you?

    The thing causing your error is in your code. It's on your monitor, and on your hard drive. I can't see either. The only window into your world I get is what you put into your post.

    But you're asking, "Why does my left hand hurt?" and showing me a picture of your right hand or something else. Depending on what picture I get, I might be able to deduce what's happening, but it wholly depends on the quality of the picture.

    ChangeImage() is one piece of the puzzle. The error is being caused by whatever parameter you are passing to ChangeImage(), so we need to see the code that calls it. But if that variable is indeed Nothing, we also need to see all of the code that could change that variable. And if any of that is not in an event handler, we need to see anything that calls it.

    You could've saved yourself a few hours if you posted ALL of your code at the start, instead of trying to narrow it down. And at this point, you've probably made it worse because the code you have now has changed since you last posted it, so what we're looking at isn't even what you're working with.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  11. #11

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    okay okay sorry ill post it all:

    FRMCHARACTER(form where character is selected):
    Code:
    Public Class frmCharacter
    
        Private Sub frmCharacter_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            PicCharacter1.Image = Character1StandWEST(4)
            PicCharacter2.Image = Character2StandWEST(4)
            PicCharacter3.Image = Character3StandWEST(4)
            PicCharacter4.Image = Character4StandWEST(4)
    
            Character1StandWEST(0) = My.Resources.stand1_0
            Character1StandWEST(1) = My.Resources.stand1_1
            Character1StandWEST(2) = My.Resources.stand1_2
            Character1StandWEST(3) = My.Resources.stand1_3
            Character1StandWEST(4) = My.Resources.stand1_4
    
            Character1StandWEST(0) = New Bitmap(My.Resources.stand1_0)
            Character1StandWEST(1) = New Bitmap(My.Resources.stand1_1)
            Character1StandWEST(2) = New Bitmap(My.Resources.stand1_2)
            Character1StandWEST(3) = New Bitmap(My.Resources.stand1_3)
            Character1StandWEST(4) = New Bitmap(My.Resources.stand1_4)
    
            Character2StandWEST(0) = My.Resources.stand1_01
            Character2StandWEST(1) = My.Resources.stand1_11
            Character2StandWEST(2) = My.Resources.stand1_21
            Character2StandWEST(3) = My.Resources.stand1_31
            Character2StandWEST(4) = My.Resources.stand1_41
    
            Character2StandWEST(0) = New Bitmap(My.Resources.stand1_01)
            Character2StandWEST(1) = New Bitmap(My.Resources.stand1_11)
            Character2StandWEST(2) = New Bitmap(My.Resources.stand1_21)
            Character2StandWEST(3) = New Bitmap(My.Resources.stand1_31)
            Character2StandWEST(4) = New Bitmap(My.Resources.stand1_41)
    
            Character3StandWEST(0) = My.Resources.stand1_02
            Character3StandWEST(1) = My.Resources.stand1_12
            Character3StandWEST(2) = My.Resources.stand1_22
            Character3StandWEST(3) = My.Resources.stand1_32
            Character3StandWEST(4) = My.Resources.stand1_42
    
            Character3StandWEST(0) = New Bitmap(My.Resources.stand1_02)
            Character3StandWEST(1) = New Bitmap(My.Resources.stand1_12)
            Character3StandWEST(2) = New Bitmap(My.Resources.stand1_22)
            Character3StandWEST(3) = New Bitmap(My.Resources.stand1_32)
            Character3StandWEST(4) = New Bitmap(My.Resources.stand1_42)
    
            Character4StandWEST(0) = My.Resources.stand1_03
            Character4StandWEST(1) = My.Resources.stand1_13
            Character4StandWEST(2) = My.Resources.stand1_23
            Character4StandWEST(3) = My.Resources.stand1_33
            Character4StandWEST(4) = My.Resources.stand1_43
    
            Character4StandWEST(0) = New Bitmap(My.Resources.stand1_03)
            Character4StandWEST(1) = New Bitmap(My.Resources.stand1_13)
            Character4StandWEST(2) = New Bitmap(My.Resources.stand1_23)
            Character4StandWEST(3) = New Bitmap(My.Resources.stand1_33)
            Character4StandWEST(4) = New Bitmap(My.Resources.stand1_43)
    
            Character1WalkWEST(0) = My.Resources.walk1_0
            Character1WalkWEST(1) = My.Resources.walk1_1
            Character1WalkWEST(2) = My.Resources.walk1_2
            Character1WalkWEST(3) = My.Resources.walk1_3
    
            Character1WalkWEST(0) = New Bitmap(My.Resources.walk1_0)
            Character1WalkWEST(1) = New Bitmap(My.Resources.walk1_1)
            Character1WalkWEST(2) = New Bitmap(My.Resources.walk1_2)
            Character1WalkWEST(3) = New Bitmap(My.Resources.walk1_3)
    
            Character2WalkWEST(0) = My.Resources.walk1_01
            Character2WalkWEST(1) = My.Resources.walk1_11
            Character2WalkWEST(2) = My.Resources.walk1_21
            Character2WalkWEST(3) = My.Resources.walk1_31
    
            Character2WalkWEST(0) = New Bitmap(My.Resources.walk1_01)
            Character2WalkWEST(1) = New Bitmap(My.Resources.walk1_11)
            Character2WalkWEST(2) = New Bitmap(My.Resources.walk1_21)
            Character2WalkWEST(3) = New Bitmap(My.Resources.walk1_31)
    
            Character3WalkWEST(0) = My.Resources.walk1_02
            Character3WalkWEST(1) = My.Resources.walk1_12
            Character3WalkWEST(2) = My.Resources.walk1_22
            Character3WalkWEST(3) = My.Resources.walk1_32
    
            Character3WalkWEST(0) = New Bitmap(My.Resources.walk1_02)
            Character3WalkWEST(1) = New Bitmap(My.Resources.walk1_12)
            Character3WalkWEST(2) = New Bitmap(My.Resources.walk1_22)
            Character3WalkWEST(3) = New Bitmap(My.Resources.walk1_32)
    
            Character4WalkWEST(0) = My.Resources.walk1_03
            Character4WalkWEST(1) = My.Resources.walk1_13
            Character4WalkWEST(2) = My.Resources.walk1_23
            Character4WalkWEST(3) = My.Resources.walk1_33
    
            Character4WalkWEST(0) = New Bitmap(My.Resources.walk1_03)
            Character4WalkWEST(1) = New Bitmap(My.Resources.walk1_13)
            Character4WalkWEST(2) = New Bitmap(My.Resources.walk1_23)
            Character4WalkWEST(3) = New Bitmap(My.Resources.walk1_33)
    
            Character1StandEAST(0) = My.Resources.stand1_0
            Character1StandEAST(1) = My.Resources.stand1_1
            Character1StandEAST(2) = My.Resources.stand1_2
            Character1StandEAST(3) = My.Resources.stand1_3
            Character1StandEAST(4) = My.Resources.stand1_4
    
            Character1StandEAST(0) = New Bitmap(My.Resources.stand1_0)
            Character1StandEAST(1) = New Bitmap(My.Resources.stand1_1)
            Character1StandEAST(2) = New Bitmap(My.Resources.stand1_2)
            Character1StandEAST(3) = New Bitmap(My.Resources.stand1_3)
            Character1StandEAST(4) = New Bitmap(My.Resources.stand1_4)
    
            Character2StandEAST(0) = My.Resources.stand1_01
            Character2StandEAST(1) = My.Resources.stand1_11
            Character2StandEAST(2) = My.Resources.stand1_21
            Character2StandEAST(3) = My.Resources.stand1_31
            Character2StandEAST(4) = My.Resources.stand1_41
    
            Character2StandEAST(0) = New Bitmap(My.Resources.stand1_01)
            Character2StandEAST(1) = New Bitmap(My.Resources.stand1_11)
            Character2StandEAST(2) = New Bitmap(My.Resources.stand1_21)
            Character2StandEAST(3) = New Bitmap(My.Resources.stand1_31)
            Character2StandEAST(4) = New Bitmap(My.Resources.stand1_41)
    
            Character3StandEAST(0) = My.Resources.stand1_02
            Character3StandEAST(1) = My.Resources.stand1_12
            Character3StandEAST(2) = My.Resources.stand1_22
            Character3StandEAST(3) = My.Resources.stand1_32
            Character3StandEAST(4) = My.Resources.stand1_42
    
            Character3StandEAST(0) = New Bitmap(My.Resources.stand1_02)
            Character3StandEAST(1) = New Bitmap(My.Resources.stand1_12)
            Character3StandEAST(2) = New Bitmap(My.Resources.stand1_22)
            Character3StandEAST(3) = New Bitmap(My.Resources.stand1_32)
            Character3StandEAST(4) = New Bitmap(My.Resources.stand1_42)
    
            Character4StandEAST(0) = My.Resources.stand1_03
            Character4StandEAST(1) = My.Resources.stand1_13
            Character4StandEAST(2) = My.Resources.stand1_23
            Character4StandEAST(3) = My.Resources.stand1_33
            Character4StandEAST(4) = My.Resources.stand1_43
    
            Character4StandEAST(0) = New Bitmap(My.Resources.stand1_03)
            Character4StandEAST(1) = New Bitmap(My.Resources.stand1_13)
            Character4StandEAST(2) = New Bitmap(My.Resources.stand1_23)
            Character4StandEAST(3) = New Bitmap(My.Resources.stand1_33)
            Character4StandEAST(4) = New Bitmap(My.Resources.stand1_43)
    
            Character1WalkEAST(0) = My.Resources.walk1_0
            Character1WalkEAST(1) = My.Resources.walk1_1
            Character1WalkEAST(2) = My.Resources.walk1_2
            Character1WalkEAST(3) = My.Resources.walk1_3
    
            Character1WalkEAST(0) = New Bitmap(My.Resources.walk1_0)
            Character1WalkEAST(1) = New Bitmap(My.Resources.walk1_1)
            Character1WalkEAST(2) = New Bitmap(My.Resources.walk1_2)
            Character1WalkEAST(3) = New Bitmap(My.Resources.walk1_3)
    
            Character2WalkEAST(0) = My.Resources.walk1_01
            Character2WalkEAST(1) = My.Resources.walk1_11
            Character2WalkEAST(2) = My.Resources.walk1_21
            Character2WalkEAST(3) = My.Resources.walk1_31
    
            Character2WalkEAST(0) = New Bitmap(My.Resources.walk1_01)
            Character2WalkEAST(1) = New Bitmap(My.Resources.walk1_11)
            Character2WalkEAST(2) = New Bitmap(My.Resources.walk1_21)
            Character2WalkEAST(3) = New Bitmap(My.Resources.walk1_31)
    
            Character3WalkEAST(0) = My.Resources.walk1_02
            Character3WalkEAST(1) = My.Resources.walk1_12
            Character3WalkEAST(2) = My.Resources.walk1_22
            Character3WalkEAST(3) = My.Resources.walk1_32
    
            Character3WalkEAST(0) = New Bitmap(My.Resources.walk1_02)
            Character3WalkEAST(1) = New Bitmap(My.Resources.walk1_12)
            Character3WalkEAST(2) = New Bitmap(My.Resources.walk1_22)
            Character3WalkEAST(3) = New Bitmap(My.Resources.walk1_32)
    
            Character4WalkEAST(0) = My.Resources.walk1_03
            Character4WalkEAST(1) = My.Resources.walk1_13
            Character4WalkEAST(2) = My.Resources.walk1_23
            Character4WalkEAST(3) = My.Resources.walk1_03
    
            Character4WalkEAST(0) = New Bitmap(My.Resources.walk1_03)
            Character4WalkEAST(1) = New Bitmap(My.Resources.walk1_13)
            Character4WalkEAST(2) = New Bitmap(My.Resources.walk1_23)
            Character4WalkEAST(3) = New Bitmap(My.Resources.walk1_33)
    
            Character1FlyUp(0) = My.Resources.fly_0
            Character1FlyUp(1) = My.Resources.fly_1
    
            Character1FlyUp(0) = New Bitmap(fly_0)
            Character1FlyUp(1) = New Bitmap(fly_1)
    
            Character2FlyUp(0) = My.Resources.fly_01
            Character2FlyUp(1) = My.Resources.fly_11
    
            Character2FlyUp(0) = New Bitmap(fly_01)
            Character2FlyUp(1) = New Bitmap(fly_11)
    
            Character3FlyUp(0) = My.Resources.fly_02
            Character3FlyUp(1) = My.Resources.fly_12
    
            Character3FlyUp(0) = New Bitmap(fly_02)
            Character3FlyUp(1) = New Bitmap(fly_12)
    
            Character4FlyUp(0) = My.Resources.fly_03
            Character4FlyUp(1) = My.Resources.fly_13
    
            Character4FlyUp(0) = New Bitmap(fly_03)
            Character4FlyUp(1) = New Bitmap(fly_13)
    
            Character1FlyDown(0) = My.Resources.fly_0
            Character1FlyDown(1) = My.Resources.fly_1
    
            Character1FlyDown(0) = New Bitmap(fly_0)
            Character1FlyDown(1) = New Bitmap(fly_1)
    
            Character2FlyDown(0) = My.Resources.fly_01
            Character2FlyDown(1) = My.Resources.fly_11
    
            Character2FlyDown(0) = New Bitmap(fly_01)
            Character2FlyDown(1) = New Bitmap(fly_11)
    
            Character3FlyDown(0) = My.Resources.fly_02
            Character3FlyDown(1) = My.Resources.fly_12
    
            Character3FlyDown(0) = New Bitmap(fly_02)
            Character3FlyDown(1) = New Bitmap(fly_12)
    
            Character4FlyDown(0) = My.Resources.fly_03
            Character4FlyDown(1) = My.Resources.fly_13
    
            Character4FlyDown(0) = New Bitmap(fly_03)
            Character4FlyDown(1) = New Bitmap(fly_13)
    
        End Sub
    
        Private Sub tmrStand_Tick(sender As Object, e As EventArgs) Handles tmrStand.Tick
    
            PicCharacter1.Image = ChangeIMage(Character1StandWEST, 0.1)
            PicCharacter2.Image = ChangeIMage(Character2StandWEST, 0.1)
            PicCharacter3.Image = ChangeIMage(Character3StandWEST, 0.1)
            PicCharacter4.Image = ChangeIMage(Character4StandWEST, 0.1)
    
        End Sub
    
        Private Sub PicCharacter1_Click(sender As Object, e As EventArgs) Handles PicCharacter1.Click
    
            intcharacter = 1
            Me.Close()
            Level1SplashScreen.Show()
    
        End Sub
    
        Private Sub PicCharacter2_Click(sender As Object, e As EventArgs) Handles PicCharacter2.Click
    
            intcharacter = 2
            Me.Close()
            Level1SplashScreen.Show()
    
        End Sub
    
        Private Sub PicCharacter3_Click(sender As Object, e As EventArgs) Handles PicCharacter3.Click
    
            intcharacter = 3
            Me.Close()
            Level1SplashScreen.Show()
    
        End Sub
    
        Private Sub PicCharacter4_Click(sender As Object, e As EventArgs) Handles PicCharacter4.Click
    
            intcharacter = 4
            Me.Close()
            Level1SplashScreen.Show()
    
        End Sub
    
        Private Sub InititalizeImages(ByVal intcharacter As Integer)
    
            If intcharacter = 1 Then
                WalkWest = Character1WalkWEST(0 - 3)
                WalkEast = Character1WalkEAST(0 - 3)
                StandWest = Character1StandWEST(0 - 4)
                StandEast = Character1StandEAST(0 - 4)
                Flyup = Character1FlyUp(0 - 1)
                FlyDown = Character1FlyDown(0 - 1)
            ElseIf intcharacter = 2 Then
                WalkWest = Character2WalkWEST(0 - 3)
                WalkEast = Character2WalkEAST(0 - 3)
                StandWest = Character2StandWEST(0 - 4)
                StandEast = Character2StandEAST(0 - 4)
                Flyup = Character2FlyUp(0 - 1)
                FlyDown = Character2FlyDown(0 - 1)
            ElseIf intcharacter = 3 Then
                WalkWest = Character3WalkWEST(0 - 3)
                WalkEast = Character3WalkEAST(0 - 3)
                StandWest = Character3StandWEST(0 - 4)
                StandEast = Character3StandEAST(0 - 4)
                Flyup = Character3FlyUp(0 - 1)
                FlyDown = Character3FlyDown(0 - 1)
            ElseIf Intcharacter = 4 Then
                WalkWest = Character4WalkWEST(0 - 3)
                WalkEast = Character4WalkEAST(0 - 3)
                StandWest = Character4StandWEST(0 - 4)
                StandEast = Character4StandEAST(0 - 4)
                Flyup = Character4FlyUp(0 - 1)
                FlyDown = Character4FlyDown(0 - 1)
            End If
    
        End Sub
    End Class
    Last edited by Shaggy Hiker; May 30th, 2017 at 07:31 PM. Reason: Added CODE tags.

  12. #12

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    level1:
    Code:
    Public Class frmLevel1
    
        Private Walls(7)
    
        Private Sub frmLevel1_KeyPress(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    
            If Keys.Left OrElse Keys.A Then
                charDirection = WEST
                PicCharacter = WalkWest
            ElseIf Keys.Right OrElse Keys.D Then
                charDirection = EAST
                PicCharacter = WalkEast
            ElseIf Keys.Up OrElse Keys.W Then
                charDirection = NORTH
                PicCharacter = Flyup
            ElseIf Keys.Down OrElse Keys.S Then
                charDirection = SOUTH
                PicCharacter = FlyDown
            End If
    
            If WallCollision() Then
                PicCharacterLvl1.Location = New Point(643, 387)
            End If
        End Sub
    
        Private Sub frmLevel1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Walls(0) = lblLvl1Wall1
            Walls(1) = lblLvl1Wall2
            Walls(2) = lblLvl1Wall3
            Walls(3) = lblLvl1Wall4
            Walls(4) = lblLvl1Wall5
            Walls(5) = lblLvl1Wall6
            Walls(6) = lblLvl1Wall7
            Walls(7) = lblLvl1Wall8
    
            intScore = 1000
    
        End Sub
    
        Private Function WallCollision() As Boolean
            For i As Integer = 0 To Walls.GetUpperBound(0)
                If PicCharacterLvl1.Bounds.IntersectsWith(Walls(i).Bounds) Then
                    Return True
                End If
            Next
            Return False
        End Function
    
        Private Sub tmrMove_Tick(sender As Object, e As EventArgs) Handles tmrMove.Tick
    
            PicCharacterLvl1.Image = ChangeIMage(PicCharacter, 0.1)
    
        End Sub
    
        Private Sub tmrScore_Tick(sender As Object, e As EventArgs) Handles tmrScore.Tick
    
            intScore = intScore - 1
            lblScore.Text = "Score:" & intScore.ToString()
    
        End Sub
    End Class
    Last edited by Shaggy Hiker; May 30th, 2017 at 07:32 PM.

  13. #13

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    Code:
    Option Strict On
    Module Animation
    
        Public Character1StandWEST(4) As Image
        Public Character2StandWEST(4) As Image
        Public Character3StandWEST(4) As Image
        Public Character4StandWEST(4) As Image
    
        Public Character1StandEAST(4) As Image
        Public Character2StandEAST(4) As Image
        Public Character3StandEAST(4) As Image
        Public Character4StandEAST(4) As Image
    
        Public Character1WalkWEST(3) As Image
        Public Character2WalkWEST(3) As Image
        Public Character3WalkWEST(3) As Image
        Public Character4WalkWEST(3) As Image
    
        Public Character1WalkEAST(3) As Image
        Public Character2WalkEAST(3) As Image
        Public Character3WalkEAST(3) As Image
        Public Character4WalkEAST(3) As Image
    
        Public Character1FlyUp(1) As Image
        Public Character2FlyUp(1) As Image
        Public Character3FlyUp(1) As Image
        Public Character4FlyUp(1) As Image
    
        Public Character1FlyDown(1) As Image
        Public Character2FlyDown(1) As Image
        Public Character3FlyDown(1) As Image
        Public Character4FlyDown(1) As Image
    
        Public StandWest As Image
        Public StandEast As Image
        Public WalkWest As Image
        Public WalkEast As Image
        Public Flyup As Image
        Public FlyDown As Image
    
        Public PicCharacter() As Image
    
        Public intcharacter As integer
    
        Public intScore As Integer = 0
    
        Public Const EAST As Integer = 0
        Public Const NORTH As Integer = 90
        Public Const WEST As Integer = 180
        Public Const SOUTH As Integer = 270
    
        Public charDirection As Integer = WEST
        Public charSpeed As Integer = 5
    
        Public imgCounter As Integer = 0
        Public Function ChangeIMage(ByVal imgArray() As Image, ByVal imgIncrement As Integer) As Image
    
            imgCounter += imgIncrement
    
            If imgCounter > imgArray.Length() Then
                imgCounter = 0
            End If
    
            Return imgArray(imgCounter)
        End Function
    
    End Module
    Last edited by Shaggy Hiker; May 30th, 2017 at 07:32 PM.

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: animation not working???

    AHA! Form Load. There are things to clean up there, but one thing you need to know: If that throws an exception, it will quit without you ever knowing about it...at least on 64-bit operating systems, which are pretty common these days. Any exception thrown in there will quietly go away, and execution will simply leave the method. This would be a total disaster for your code, and would result in the exception you are reporting (later...AFTER you got out of the load event).

    So, there are two things you can do to see if this is happening. One thing you can do is put a breakpoint on the very first line in the load event. If execution gets there, then press F10 repeatedly. If you don't step through each line in that method, but suddenly jump somewhere else...then you have a problem.

    A better solution would be to wrap the whole contents of that method in a Try...Catch block. Any exception being thrown is a bug that needs to be fixed, so you don't have to do much with the exception, since you want to squash the bug, at which point there won't be exceptions anymore. So, in the catch block, put either a messagebox showing the exception message, or even just a line to put a breakpoint on.

    The very first line in that event may be throwing an exception, too, because you are setting a picturebox image to one index in an array...which has nothing in it, yet, since you don't load the array until later. So, you are setting the picturebox image to Nothing, which may not be allowed. I don't see a specific mention that it is not allowed, but I do see a mention that certain things can cause exceptions. Once again, if an exception is thrown on that line....the rest all makes sense.

    So, look for a bug in there. The exception handler method will give you more information, while the breakpoint approach is faster to try out.
    My usual boring signature: Nothing

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: animation not working???

    By the way, you got some basic stuff right that lots of people do not. The use of Option Strict is excellent. That will save you pain in the long run. You are also using OrElse rather than Or, which is often overlooked when starting out.

    So, good job.
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    right, so when i enter the breakpoint, it does go line to line. I am just having trouble finding the problem with the animation. I have fixed the imgarray is nothing error. Since there are no errors or anything when it loads and when i press a key, i figure it must have been when i declared all the images for the animation. i have checked it over with the files a dozen times and they are correct. what has been happening is first off, the stand animation doesnt work since it is staying idle, and when i press a key, it switches to the first image of walk and stays like that. Also, for some characters, it ends up a different one in the first level. I have no clue why this is happening and im am certain all the files are matching up correctly. It could also be problem in changeimage, but there is no error coming from there. there is more information on my newest thread, please check there. also, thank you so much for your help so far.

  17. #17
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: animation not working???

    Which is easier, to paint one fencepost or to paint a fence?

    You are trying to paint a fence. By that I mean your character selection code causes repetition. Everything you write is repeated four or five times, which means finding out how anything works involves 80% of the code being repetition that slows the reader down. With more work, you shouldn't have to repeat anything to have multiple characters. But that's not your problem right now.

    Delete everything related to having more than one character. Right now. All of it. Get your code working as if character select just doesn't exist. If your game doesn't work with one character, it won't work with five. So you should get your game working with one character first. Then two. After two, moving up to five should be easy.

    But if you try to start with five characters, you can't tell if the problem is "the animation code is wrong" or "the character select code is wrong", and having 5x the code to read makes it dreadfully hard to proceed.

    I try not to write more than ten lines of code before trying out my program to see if they work. When I do that, I know that any new problem must involve the ten lines I wrote. When I was younger, I didn't think about this, and would work for an hour before trying anything out. Hundreds of lines. It is much easier to debug 10 lines than 100.

    I can't be certain the bugs you're describing have anything to do with the code in this post anymore. So make the cut I suggested ad trim the program down to just one character. Then debug that. If you're still stumped, post EXACTLY the code you have. It should be about 100 lines shorter, which means it will take me 20-30 less minutes to decipher. Given that I usually only have about an hour to putter around on the forum, that's a big deal.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  18. #18

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    alright i took out anything to do with characters 2, 3, and 4. ill make a new thread with the fixed code.

  19. #19
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: animation not working???

    Quote Originally Posted by rparikh10 View Post
    alright i took out anything to do with characters 2, 3, and 4. ill make a new thread with the fixed code.
    You have the Line 'Option Strict On' at the top of one of your code files. It should be present in all your code files.

    Better still, turn Option Strict on in the IDE's options:
    Tools / Options / Projects and Solutions / VB Defaults.
    Then restart VS.

    If you had Option Strict turned On, you'd be led straight to the cause of your problem. You'd get an error shown for lines that make a call to 'ChangeIMage', such as:
    Code:
    PicCharacter1.Image = ChangeIMage(Character1StandWEST, 0.1)
    and you'd see a squigly line under the 0.1.

    Why is that line wrong? Well look to how you declared the ChangeIMage method:
    Code:
    Public Function ChangeIMage(ByVal imgArray() As Image, ByVal imgIncrement As Integer)
    The second parameter expects an Integer to be passed, but you are passing a fraction (0.1) as an argument.

    With Option Strict turned Off, you are forcing the compiler to implicitly convert that 0.1 to an Integer, and it converts it to 0, so the value of your 'imgCounter' variable never increases.

  20. #20

    Thread Starter
    Member
    Join Date
    May 2017
    Posts
    32

    Re: animation not working???

    right, i didnt realize it was only in that form. the error now came up. thanks.

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