Alternating Images
Alternating Images
Alternating Images
Alternating Images
Alternating Images
Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images
Alternating Images Alternating Images
Alternating Images
Go Back  Xtreme Visual Basic Talk > > > Alternating Images


Reply
 
Thread Tools Display Modes
  #1  
Old 06-25-2011, 06:28 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default Alternating Images


Probably rather ambitious but I am trying to alternate each of the Aliens images, so that each alien has two pictures associated with it.

The reasoning behind this is so I can make each alien look like its legs are moving like in the original Space Invaders game. I have added the second lot of images in the code below

Code:
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim LocationX As Integer = 0
        Dim LocationY As Integer = 70

        For columns As Integer = 0 To 10 'Move next column of aliens right
            LocationX += 100
            For rows As Integer = 0 To 4 'Move next row of aliens down
                LocationY += 70
                Dim s As Sprite
                Select Case rows
                    Case Is = 0
                        s = New Sprite(My.Resources.AlienType1B)
                    Case Is = 1
                        s = New Sprite(My.Resources.AlienType2A)
                    Case Is = 2
                        s = New Sprite(My.Resources.AlienType2B)
                    Case Is = 3
                        s = New Sprite(My.Resources.AlienType3A)
                    Case Is = 4
                        s = New Sprite(My.Resources.AlienType3B)
                End Select
                s.Location = New Point(LocationX, LocationY) 'Set the various starting postions
                s.Movement = New Point(10, 0) 'Move horizontally by this much
                things.Add(s)
                Select Case rows
                    Case Is = 0
                        s = New Sprite(My.Resources.AlienType1A)
                    Case Is = 1
                        s = New Sprite(My.Resources.AlienType2B)
                    Case Is = 2
                        s = New Sprite(My.Resources.AlienType2A)
                    Case Is = 3
                        s = New Sprite(My.Resources.AlienType3B)
                    Case Is = 4
                        s = New Sprite(My.Resources.AlienType3A)
                End Select
                s.Location = New Point(LocationX, LocationY) 'Set the various starting postions
                s.Movement = New Point(10, 0) 'Move horizontally by this much
                things.Add(s)
            Next
            LocationY = 70 'Reset ready for next pass
        Next

    End Sub
Stepping through the code I figure I need to alternate the image being drawn somewhere here, but not sure how to do it. Probably need 2 For Each Next with some way to alternate between them.

Code:
    Private Sub Main_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbMain.Paint

        Dim g As Graphics = e.Graphics
        g.Clear(Color.Gray)

        For Each thing In things
            thing.Draw(g)
        Next

    End Sub
Code:
    Public Sub Draw(ByVal g As Graphics)

        g.DrawImageUnscaled(image, Location)

    End Sub
Reply With Quote
  #2  
Old 06-25-2011, 01:17 PM
PlausiblyDamp's Avatar
PlausiblyDampAlternating Images PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Personally I would change the sprite class so instead of having a single image it maintains a list of images (the Sub New would need to be changed to accept a List(Of Image) as well).

You would also need to maintain the index of the current image you are drawing, every frame you draw just increment this counter. If you only need two images then it would be a list of just two, by allowing a list though you are allowing for a more flexible sprite that could contain far more than just two if needed.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 06-25-2011, 07:43 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Good suggestion... By maintaining a list of images I could also make an explosion image for when the alien is hit for each alien (3 different alien types = 3 different alien explosion images).

Should I be trying to make a sub or function for each of the alien types i.e. AlienType1, another for AlienType2 etc, or simply load all the images into a list at the start?

I think you answered my issue of how to alternate the images... a image list will give me an index I can use to alternate the picture loaded, but not sure if the picture loading section of the code can take an index overload to provide the desired image.

These are the images I have so far, as you can see I have an A and a B for each alien, to simulate the alien walking like in the original game.
Attached Images
File Type: jpg Reseources.jpg (52.1 KB, 9 views)
Reply With Quote
  #4  
Old 06-25-2011, 07:47 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Probably a silly question.... but up to now I only have one picture box the main window, do each of these aliens need their own picture box to detect a collision, or are they being dynamically created at run time?
Reply With Quote
  #5  
Old 06-25-2011, 08:27 PM
PlausiblyDamp's Avatar
PlausiblyDampAlternating Images PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Each sprite is being drawn on the one picturebox on the main window, this means you don't need one picturebox per alien, bullet or explosion.

When it comes to collision detection you will know the size and location of every sprite so checking if a bullet has collided with an alien is just a matter of looping through all remaining aliens and checking if the alien's co-ordinates overlap the bullet's co-ordinates.

In regards to
Quote:
Should I be trying to make a sub or function for each of the alien types i.e. AlienType1, another for AlienType2 etc, or simply load all the images into a list at the start?
things really depend on what is needed next... If all aliens have pretty much the same behaviour and it is only the images that change then you will probably be fine as things are. One possibility though is to create classes that inherit from the Sprite class and provide specific behaviour for each type and simply use the Sprite class as a base that takes care of the essentials (painting etc.)
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #6  
Old 06-26-2011, 03:11 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Wasn't sure if I was doing the right thing by using just the one picture box or if the people using multiple picture boxes were doing it right.

It seems making seperate picture boxes is easier so is that kind of the start place and the end goal to have just one?

I don't think I am ready for "Inherit" just yet, I vaugely understand it but don't think I am quite ready for that leap.

Made some good headway today for all the hours spent (good for me anyway)...
Added a few declarations

Code:
Public Class Main

    Dim somesprites As New List(Of Sprite)
    Dim someimages As New List(Of Image)
    Dim shooter As Sprite
    Dim bullet As Sprite
And in the form load event added the images to a Image list.
Assigned some values to bullet and shooter.

Code:
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Question --- Possible to loop through all images in the resource folder to load instead of individual lines??? much later job... 
        someimages.Add(My.Resources.AlienType1A) '0 - Add all used images and comment indexed positions
        someimages.Add(My.Resources.AlienType1B) '1
        someimages.Add(My.Resources.AlienType1C) '2
        someimages.Add(My.Resources.AlienType2A) '3
        someimages.Add(My.Resources.AlienType2B) '4
        someimages.Add(My.Resources.AlienType2C) '5
        someimages.Add(My.Resources.AlienType3A) '6
        someimages.Add(My.Resources.AlienType3B) '7
        someimages.Add(My.Resources.AlienType3C) '8
        someimages.Add(My.Resources.Shooter)     '9
        someimages.Add(My.Resources.Bullet)      '10

        Dim LocationX As Integer = 0
        Dim LocationY As Integer = 70
        Dim sprote As Sprite = Nothing

        For columns As Integer = 0 To 10
            LocationX += 100                                            'Move each column of aliens 100 to the right 
            For rows As Integer = 0 To 4
                LocationY += 70                                         'Move each row of aliens a further 70 down
                Select Case rows
                    Case Is = 0
                        sprote = New Sprite(someimages(0)) '1A
                    Case Is = 1
                        sprote = New Sprite(someimages(3)) '2A
                    Case Is = 2
                        sprote = New Sprite(someimages(4)) '2B
                    Case Is = 3
                        sprote = New Sprite(someimages(6)) '3A
                    Case Is = 4
                        sprote = New Sprite(someimages(7)) '3B
                End Select
                sprote.Location = New Point(LocationX, LocationY)       'Set all the various alien starting postions
                sprote.Movement = New Point(10, 0)                      'Move all aliens horizontally by this much
                somesprites.Add(sprote)
            Next
            LocationY = 70                                              'Reset from the top of frame down ready for next pass
        Next

        shooter = New Sprite(someimages(9))                             'Set an image to shooter (Sprite), Set the shooter starting position, Move the shooter by this much
        shooter.Location = New Point(World.WorldSize.Width / 2 - 90, World.WorldSize.Height - someimages(9).Height - 10)
        shooter.Movement = New Point(10, 0)

        bullet = New Sprite(someimages(10))                             'Set an image to bullet (Sprite), Set the bullet starting position, Move the bullet by this much  
        bullet.Location = New Point(shooter.Location.X + 16, World.WorldSize.Height - someimages(9).Height - someimages(10).Height - 5)
        bullet.Movement = New Point(10, 0)

    End Sub
Updated the paint event.

Code:
Private Sub Main_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbMain.Paint

        Dim g As Graphics = e.Graphics
        g.Clear(Color.Gray) ' Black?
        For Each sprit In somesprites
            sprit.Draw(g)
        Next
        shooter.Draw(g)
        bullet.Draw(g)

    End Sub
And started on the key press event: It’s not a smooth as I would like (quite jerky). Wondering if I need to try and run it on a separate thread so it isn’t effected by the Timer1.Timer / moving aliens...

Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Select Case e.KeyCode
            Case Is = Keys.Left
                shooter.Location = New Point(shooter.Location.X - 10, shooter.Location.Y)
                bullet.Location = New Point(shooter.Location.X + 16, shooter.Location.Y - 18)
                'move left unless hit boundry
            Case Is = Keys.Right
                shooter.Location = New Point(shooter.Location.X + 10, shooter.Location.Y)
                bullet.Location = New Point(shooter.Location.X + 16, shooter.Location.Y - 18)
                'move right unless hit boundry
            Case Is = Keys.Space
                'Fire the bullet if available
        End Select

    End Sub
Reply With Quote
  #7  
Old 06-26-2011, 10:54 AM
AtmaWeapon's Avatar
AtmaWeaponAlternating Images AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You're making it too complicated. I think the question's still "How do I alternate between two frames?"

Right now, a sprite's image is immutable. If you want to change it, you have to create a new sprite with a different image and replace the old image. That's going to be a pain to maintain. What if each sprite knew about both frames of animation? Then you could let the sprite keep track of its current frame, and the main code loop would only have to worry about getting the image for the next frame. Here's a real simple mockup that excludes all other properties:
Code:
Class Alien
    Public Property Frames() As Image()
    Public Property FrameCounter() As Integer
    
    Public Function GetNextFrame() As Image
        ' Increment frame counter; if it's > the last frame reset it to 0
        FrameCounter += 1
        If FrameCounter > Frames.Length Then
            FrameCounter = 0
        End If
        
        Return Frames(FrameCounter)
    End Sub
End Class
I can't see your drawing code, so I can only hint at what to do. Armed with a class like Alien (and other properties that should exist), this would update the frames every timer tick:
Code:
Sub Timer_Tick(...)
    Invalidate()
End Sub

Overrides Sub OnPaint(...)
    For Each alien As Alien in _aliens
        g.DrawImage(alien.GetNextFrame(), alien.Location)
    Next
End Sub
Each time you draw, you ask for the next alien frame. If you want to draw more frequently than you need the animation frames to change, you could add a GetCurrentFrame() to the class and use that on the frames that shouldn't change. For example, suppose you want to animate the bullet 10 frames for every 1 change of the alien; you could use logic like this to determine the frame:
Code:
Private _frameCounter As Integer

Overrides Sub OnPaint()
    _frameCounter += 1
    If _frameCounter Mod 10 = 0
        _frameCounter = 0
    End If
    
    For Each alien As Alien in _aliens
        Dim alienImage As Image
        If _frameCounter <> 0
            alienImage = alien.GetCurrentFrame()
        Else
            alienImage = alien.GetNextFrame()
        End If
        
        g.DrawImage(...
    ...
End Sub
Make your code work for you, not the other way around. If something seems hard, you're doing it wrong. Ask yourself who needs information. Do they have it? If not, give it to them. Is that still hard? Maybe the wrong entity has the information. Maybe you're missing an entity. In this case, someone needed to know about two images and when to use which one. It's easier to let the individual sprites manage this than have the game loop try to manage multiple instances of each alien.

Don't get hung up on finding the "right" answer though. Such a thing only exists for specifically-designed programming puzzles. Most professionals settle for, "the easiest thing that satisfies all requirements". Unless it's something that might change frequently; that becomes "an architectural nightmare that has to be handled with delicacy."

*edit*
Whoops, forgot to comment on some pieces of your post:
Quote:
Wasn't sure if I was doing the right thing by using just the one picture box or if the people using multiple picture boxes were doing it right.

It seems making seperate picture boxes is easier so is that kind of the start place and the end goal to have just one?
Using multiple picture boxes gets you hit testing and a good bit of rendering for free. But it starts to break down as your requirements get more complex. Rendering controls is slower than rendering bitmaps. PictureBox in particular can be slow or flickery depending on how it's used. For 20-30 picture boxes things might work out. But something like a Pac-Man game with 100 pictureboxes or more is likely to have troubles. There *is* a maximum number of controls allowed, and your form starts having performance problems far before you reach it. So minimizing controls can have a big impact. Plus, I have an irrational personal bias against picture boxes so I suggest avoiding them whenever possible.

So yes, most people start with pictureboxes and end up drawing everything themselves.
Quote:
I don't think I am ready for "Inherit" just yet, I vaugely understand it but don't think I am quite ready for that leap.
Get ready. It's a powerful tool that can save you a lot of trouble (or create even more trouble!) No good programmer is afraid to learn a new concept. Being afraid to *use* it can be healthy, but that's a different story. Some of these features are like a rocket launcher: highly effective at destroying complexity but suicidal if your aim is clumsy.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 06-26-2011, 10:32 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Thanks for the very useful insight AtmaWeapon. I think it’s a bit like having a single spanner in your toolbox, until you slowly increase your toolset the spanner acts as a hammer, a screw driver and a spanner...
I haven’t used “Overrides Sub” On Paint() before, or Public Property Frames() As Image()

But I guessing the Overrides does as the name implies and overrides the OnPaint event.

And never used Frames before... Public Property Frames() As Image() this looks like you are assigning multiple frames to each of the multiple images, like a 2 dimensional array.

I am at work at the moment so I can’t review my code, but I don’t see where you are loading the images... but just trying to think what the images would be attached to.

If we have at least 2 frames (~2 images) associated with the same screen space that one sprite occupies, are we no longer attaching the image to the sprite?

Are we creating a new framework that holds the 2 images per slot with this Public Property Frames() As Image()?

And somehow calling upon the selected index at paint time? I will need to play with this in code to get my head around it.

Yes the paint event was pretty much what you typed, it was straight out of PlausiblyDamp’s code example which hasn’t been modified.
Reply With Quote
  #9  
Old 06-26-2011, 11:41 PM
AtmaWeapon's Avatar
AtmaWeaponAlternating Images AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I'm sort of confused as well. My code samples are meant as a blueprint to show off the concepts. I could be more specific if I could see your code. But I can't, so all I can do is wave my hands and try to describe the concepts. It's like trying to give someone directions without a map; it's a heck of a lot easier if you have the map.

I don't know what code you're using or where PlausiblyDamp's code you mentioned is located, nor am I inclined to spend a while digging through multiple threads to work out which version is the one we're talking about. People are lazy, make it easy to answer your questions!
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #10  
Old 06-27-2011, 07:43 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Easy fixed... I will try update with your suggestions (or at least try to see if I can get it to work) and then post a fresh version of the app. Problably doesn't help that I have renamed a few things to try make it a bit more user name friendly either.
Reply With Quote
  #11  
Old 06-27-2011, 09:39 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Didn’t get chance to play with VB last night as work got in the road. Just re-reading through yesterdays post and the penny just dropped, I now see how you are alternating the frames.
Code:
If _frameCounter <> 0
            alienImage = alien.GetCurrentFrame()
        Else
            alienImage = alien.GetNextFrame()
        End If
I was thinking that I would have to alternate the
g.DrawImage(...

but the frame switch is taking place before it is presented to Draw.. very clever.
Reply With Quote
  #12  
Old 06-28-2011, 09:55 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Inserted the code last night but it is so untidy with stuff commented out everywhere I didn't want to post it yet.

I did encounter a problem with this though...

For Each alien As Alien in _aliens

I have done For Each x In xx before, but the As x In bit threw me.

I created an Alien Class as instructed...

Code:
Class Alien
    Public Property Frames() As Image()
    Public Property FrameCounter() As Integer
...
End Class
The alien in For Each alien I believe is a variable used only within the For Each Next loop.

And the Alien in As Alien I think refers to the Alien Class, but I don't know where the _alien comes into play.
Reply With Quote
  #13  
Old 06-29-2011, 04:34 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Found what I was looking for on MSDN http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

Generally I find their examples more confusing then the problem, but should be able to work this one out.

Edit: Ok worked it out

Code:
For Each alien As Sprite In somesprites
'is the same as  
For Each alien In somesprites
'if alien is declared as Sprite elsewhere

Last edited by CodeCruncher; 06-29-2011 at 05:42 AM.
Reply With Quote
  #14  
Old 07-05-2011, 04:55 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Ok learned a bit... but feel I have wandered off path again.

Attached is an up to date copy. I just noticed that when I move the shooter across the screen the aliens stop alternating.

I think I need seperate paint events but couldn't work out how to do it. I also think I have mucked up the whole class structure again.

I had to use a second picturebox as I couldn't get it to work as part of the main picturebox.
Attached Files
File Type: zip Space Invaders.zip (39.4 KB, 4 views)
Reply With Quote
  #15  
Old 07-05-2011, 05:44 AM
PlausiblyDamp's Avatar
PlausiblyDampAlternating Images PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I am not entirely sure as to the purpose of the Alien_Paint method in the form, it is never called from anywhere but it looks like you are trying to get the form to pain the various aliens - is this correct? If so I would suggest you look back at Atma's suggestion in post #7 (Alternating Images) above as he explains a way of allowing each sprite to have multiple frames of animation and for the sprite to handle it's own drawing logic.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #16  
Old 07-05-2011, 09:07 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

The paint method is working but it is alternating images, I tried cutting and pasting AtmaWeapons code above into the project and tweaking it to work but I just couldn't get it to work.

I go through phases where I write some decent code and times where I write utter crap, I think I am currently experiencing an all time low for rubbish.

I might need to park this game for 6 months and hope it comes back to me because I am struggling to get my head around returning data from called functions in different classes.

I was hoping to get a bit further than I have but it is consuming way too much time and the level of code I am writing is extremely sub standard.
Reply With Quote
  #17  
Old 07-05-2011, 09:09 AM
AtmaWeapon's Avatar
AtmaWeaponAlternating Images AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It is called: it handles the main picture box's Paint event. Still, I'm disappointed that it doesn't look like anything I've talked about has been implemented. I've got a holiday today and I'm thinking about making a reference implementation to show what I've meant. It's so much easier to speak with code than words. It'll get me in the mood for the WPF tutorial I want to start.

The above post was made before I saw #16; it's preserved for continuity. Now I see I was hasty in my assertion I was being ignored; this is code born of frustration, not haste. I still want to try and make the application today; I need to actually finish a project. But now I know to focus *harder* on how the classes make interaction between entities clear. This should be a nice tutorial.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #18  
Old 07-08-2011, 06:28 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

If this is what you were talking about I was way off the mark...
http://java.sun.com/javame/reference...-external=true
Reply With Quote
  #19  
Old 07-08-2011, 10:42 AM
AtmaWeapon's Avatar
AtmaWeaponAlternating Images AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You have to be careful when looking at game engines for design ideas. A game engine is designed to handle scenarios that come up in many different games; this is useful for companies that develop more than one kind of game. If you're trying to write just one kind of game, you might be confused by all of the features of the engine.

For example, that page describes how their sprite class lets you segment one image into frames, specify out-of-order sequencing for the frame animations, set the origin for any transforms, and apply transforms. You don't need any of this. Your sprites have a location and only three frames: two for animation and one for being destroyed. You're well-served by writing your own simple sprite class.

Get over the idea that there's some right way to write it. Especially for something with so many definitions as "sprite". You may want to ignore PD's example if only because so far it seems to have served only to confuse you. Think about what you need, write that. If you can't understand if the classes you are using provide that, stop using them and write them yourself. I'll walk through some of the considerations. I tried and failed an implementation last weekend. It's an interesting failure and I learned some lessons. That's for another thread.

You've got three kinds of on-screen entities: ship, alien, bullet. Let's focus on the alien since it's the most complex. An alien has three frames: moving 1, moving 2, and exploding. Each time you draw an alien, you need to figure out which frame to use; this suggests you need a structure to store 3 frames and some internal counter that indicates what frame to use. When an alien is dead, it shouldn't be visible. This indicates you either need a special invisible frame or some other variable to track whether the alien is dead. The alien needs to move periodically and fire bullets periodically, but other parts of the program might manage that behavior. Let's focus just on the frame management.

Here's a shot at it with shorthand syntax:
Code:
Public Class Alien
    Private _frames() As Image
    Private _frameCounter As Integer
    Private _isDead As Boolean

    Public ReadOnly Property CurrentFrame As Image
        Get
            Return _frames(_frameCounter)
        End Get
    End Property

    Public ReadOnly Property IsDead As Boolean
        Get
            Return _isDead
        End Get
    End Property

    Public Sub Kill()
        _frameCounter = 2
        IsDead = True
    End Sub

    Public Sub Animate()
        If Not IsDead then
            _frameCounter += 1
            If _frameCounter = 1 Then
                _frameCounter = 0
            End If
        End If
    End Sub
End Class
There. Dead simple. When aliens move, call each alien's Animate() method. This toggles the frames between 0 and 1 if not dead. When it's time to draw the alien, if IsDead is true you shouldn't draw it. If it's false, get the frame from the CurrentFrame property. If a bullet hits an alien, call Kill(). This sets the frame to the "dying" frame and sets the IsDead flag. Note the subtle bug: since we don't draw when the aliens are dead, the "dying" frame is never displayed. Perhaps there should be an IsDying property? That's homework.

Your code to move aliens may look something like this:
Code:
For Each alien As Alien In _aliens
    ' ... update the location of the alien here
    alien.Animate()
Next
The painting code may look like this:
Code:
For Each alien As Alien in _aliens
    If Not alien.IsDead Then
        Dim frame As Image = alien.CurrentFrame
        e.Graphics.DrawImage(...)
    End If
Next
The point of having an alien class is to keep details about how to display an alien compartmentalized in one place. OnPaint() is where you want to draw your screen, not where you want to be calculating new frames. The movement code is where you want to relocate aliens, not where you want to calculate new frames. Sprites worry about their frames themselves; it's up to you to find a natural way to tell them how to update.

Alternatively: forget about sprites entirely. As long as *something* knows how to display the right frame it's OK.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #20  
Old 07-09-2011, 01:57 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Sorry I really need to be more careful when asking a question in a programming forum. I have a tendency to speak very conceptually rather than literally at times, which can lead to misunderstandings.

When I asked if the link above was what you were talking about, I meant in a very loose concept kind of way, not that specific Java example.

See I had been trying to tie two separate images to the same sprite entity (which wasn’t working - I couldn’t find any properties that allowed me to tie two separate images to the same sprite), and then trying to get a better understand of what you were talking about I did a search on the Sprite class (I didn't even know there was a Sprite class I thought it was something we were making up).

I found that website then realised that perhaps I completely misunderstood what you were trying to do with the frames. I didn’t even know you could pick a part of an image like that, so what I was looking for was a “yes that was what I was talking about” or “no you still have it wrong”.

Thank you for the new code, I will see if I can make more sense of it, now I have a different set of glasses on (metaphorically speaking not literally ;o).

Just to clarify... I still load my sprite images the same way as before, just now I use a multi image picture instead of loading a single image picture?

Hmmm i'm being too general again. I mean in the same style as before, as I obviously need a different frame sequence for each alien type.

Or should I be trying to put all three alien types in the same image? So that would mean I have 9 frames in my image, 2 walking, and 1 explode, for each of the 3 alien types.

Code:
For Columns As Integer = 0 To 10
            LocationX += 100                                            'Move each column of aliens 100 to the right 
            For Rows As Integer = 0 To 4
                LocationY += 100                                        'Move each row of aliens a further 100 down
                Sprit = New Sprite(SomeImages(0))                       'Set same generic image as place holder but another will loaded at paint time 
                Sprit.Location = New Point(LocationX, LocationY)        'Set all the various alien starting postions
                Sprit.Movement = New Point(10, 0)                       'Move all aliens horizontally by this much
                SomeSprites.Add(Sprit)
            Next
            LocationY = 0                                               'Reset from the top of frame down ready for next pass
        Next
Edit: Just having a rethink... Since each of the alien types will only need the 3 different frames within the image, it doesn't make sense to include all 9 frames in each of the sprite entities.

Row 1 uses the 3 frames from Alien Type 1
Rows 2 & 3 uses the 3 frames from Alien Type 2
Rows 4 & 5 uses the 3 frames from Alien Type 3

Last edited by CodeCruncher; 07-09-2011 at 02:12 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Alternating Images
Alternating Images
Alternating Images Alternating Images
Alternating Images
Alternating Images
Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images Alternating Images
Alternating Images
Alternating Images
 
Alternating Images
Alternating Images
 
-->