Create Several Gif And Then Move All

NoIdeas

Active member
Joined
Aug 13, 2011
Messages
25
Location
Sweden
Programming Experience
1-3
Hi Everybody!

I'm making a small game, where you move your character and collect coins. Now, i've reached to the very last part where i add my coins... I have a timer to define the positions and create a coin-image on that position... And when the timer ticks again, the last image disappears, so i can't have several images at once... Please help! :(

And when i have that fixed, I have to make all coins move like 5 pixels downwards at the same time... And i have NoIdeas how to make that happen :S

All replies appriciated!

// NoIdeas
 
have you tried doing an array with the coins?, so each image generated would be....

Coin(0)
Coin(1)
Coin(2)
Coin(3)
Coin(5)

In this Array their would be 6 Coins as Arrays start from "0".
Hope this helps.
 
Well, 've actually thought about it, but I did'nt know how many coins there could be... But now when i think about it, if i make sure that it's always one coin per "row" then i would know... So, i have a solution :)

I've tried the solution now, but i got this error, and i don't know what the problem is...

Here's the code:

Public Class Form1
Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
Dim TransparentColor As Color = Color.FromArgb(255, 0, 255, 0)
Dim pos As Point = New Point(0, 460)
Dim Loc As String = "C:\Users\UserName\My Documents\Visual Studio 2008\Projects\Milky\Milky\bin\Milky\Front.gif"
Dim DefLoc As String = "C:\Users\UserName\My Documents\Visual Studio 2008\Projects\Milky\Milky\bin\Milky\Front.gif"
Dim Dir As String
Dim Frame As Integer = 1
Dim JumpFrame As Integer = 1
Dim Jumpheight As Integer = 0
Dim JumpPhase As Integer = 1


Dim NrOfPoints As Integer = Me.Width / 10 - 1
Dim CurrentPoint As Integer = 0
Dim CoinLoc As String = "C:\Users\UserName\My Documents\Visual Studio 2008\Projects\Milky\Milky\bin\Coin.gif"
Dim Coin As New Bitmap(CoinLoc)
Public PNCP As New Point
Dim CoinX(NrOfPoints) As Point
Dim CoinCout As Integer = 0
Dim CoinsPos() As Point

Public Resources As String = "C:\Users\UserName\My Documents\Visual Studio 2008\Projects\Milky\Milky\bin\"

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Me.Close()
End If

If e.KeyCode = Keys.Up Then
JumpTick.Start()
End If

Select Case e.KeyCode
Case Keys.Right
Call Walk("Right")
Case Keys.Left
Call Walk("Left")
Case Keys.Down
If Not Loc = Resources & "Milky\Dodge.gif" Then
pos.Y += 11
End If

Loc = Resources & "Milky\Dodge.gif"
End Select

Me.Invalidate()
End Sub

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If Loc = Resources & "Milky\Dodge.gif" Then
pos.Y -= 11
End If
Loc = DefLoc
Me.Invalidate()
End Sub


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
CurrentPoint = 0

Coin.MakeTransparent(TransparentColor)

Do Until CurrentPoint = NrOfPoints
CoinX(CurrentPoint) = New Point(CurrentPoint * 10, 0)
CurrentPoint += 1
Loop

End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim bmp As New Bitmap(Loc)

bmp.MakeTransparent(TransparentColor)
e.Graphics.DrawImage(New Bitmap(bmp), pos)

End Sub

Public Sub Walk(ByVal Direction As String)
Dim Framestr As String = CStr(Frame)

If Not Loc = Resources & "Milky\Jump" & Framestr & ".gif" Then
Loc = Resources & "Milky\" & "Walk" & Framestr & ".gif"
End If
If Direction = "Right" Then
pos.X += 3
ElseIf Direction = "Left" Then
pos.X -= 3
End If
Me.Invalidate()
Frame += 1
If Frame = 5 Then Frame = 1
End Sub

Public Sub Jump() Handles JumpTick.Tick

If Jumpheight > 79 Then
JumpPhase = 2
End If
If JumpPhase = 1 Then
pos.Y -= 2
Jumpheight += 2
Me.Invalidate()
ElseIf JumpPhase = 2 Then
pos.Y += 2
Jumpheight -= 2
Me.Invalidate()
End If

If Jumpheight <= 0 Then
JumpTick.Stop()
JumpPhase = 1
End If

If GetAsyncKeyState(Keys.Right) Then
pos.X += 1
ElseIf GetAsyncKeyState(Keys.Left) Then
pos.X -= 1
End If

End Sub

Private Sub EventTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EventTimer.Tick
Dim R As New Random
Dim NewCoinPos As Point = New Point(CoinX(R.Next(1, NrOfPoints)))
PNCP = NewCoinPos
CoinsPos(CoinCout) = New Point(PNCP) <----------------------The Exception Helper points to this line
CoinCout += 1


End Sub

Public Sub NewCoin(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Try
Dim MiniCC As Integer = 0
For Each Point In CoinsPos
e.Graphics.DrawImage(New Bitmap(Coin), CoinsPos(MiniCC))
MiniCC += 1
Next
Catch Ex As Exception

End Try
End Sub
End Class


Error is: "Object reference not set to an instance of an object." And it is pointing at line 130... I've marked it with an arrow... Does anybody know what the problem is?




Thanks for replie! :D
 
Last edited:
Back
Top