death_entry 04-01-2005, 10:03 PM Here is my structure at the moment
'Defines the Green enemy
Public Structure Green
Public greenStep As Timer
Public greenLoc As Rectangle
Public sprite As Image
Public Sub New(ByVal Xpoint As Integer, ByVal Ypoint As Integer)
greenLoc = New Rectangle(Xpoint, Ypoint, 21, 16)
sprite = New Bitmap(Application.StartupPath & "/Images/grunt_green_formation.jpg")
greenStep = New Timer
greenStep.Interval = 100
greenStep.Enabled = True
End Sub
End Structure
Now is it possible that after every 100 milliseconds the structures draws the rectangle asscoiated to it, using the image associating to it; meaning every object is responsible for drawing itself on screen.
However I don't think this is possible so I'm guessing I need to use a global image drawing thing, which scans an array of enemies retrives their image and rectangle values and then draws them?
My structure is meant to define a Green enemy, have a sprite associated with it, a rectanlge with a position passed to it when an instance of the structure is created, if anyone spots any mistakes in it let me know
Many thanks. :D
edit: well I got the array way working, now all I got to think of is of a way of getting an instance to automaticly take itself out of the array when it gets blown up :(
elnerdo 04-02-2005, 06:59 AM I'm not really sure, but will this work?
Public Structure Green
Public greenStep As Timer
Public greenLoc As Rectangle
Public sprite As Image
public greent as system.threading.thread
Public Sub New(ByVal Xpoint As Integer, ByVal Ypoint As Integer)
greenLoc = New Rectangle(Xpoint, Ypoint, 21, 16)
sprite = New Bitmap(Application.StartupPath & "/Images/grunt_green_formation.jpg")
greent.addressof(Greendrawloop)
greent.start
End Sub
public sub greendrawloop()
do
'draw here
greent.sleep(100)
loop
end sub
End Structure
death_entry 04-04-2005, 07:04 AM I got the error of
'addressof' is not a member of 'System.Threading.Thread'.
for line "greent.addressof(greendrawloop)"
sorry for taking so long to write back i've been out clubbing :P
Anyway I got my way working
'Defines the Green enemy
Public Structure Green
Public greenStep As Timer
Public greenLoc As Rectangle
Public sprite As Bitmap
Dim aTimer As Timer
Public Sub Green_Load(ByVal Xpoint As Integer, ByVal Ypoint As Integer)
greenLoc = New Rectangle(200, 100, 21, 16)
sprite = New Bitmap(Application.StartupPath & "/Images/grunt_green_formation.jpg")
greenLoc.Location = New Point(Xpoint, Ypoint)
aTimer = New Timer
AddHandler aTimer.Tick, AddressOf OnTimer
aTimer.Interval = 100
aTimer.Enabled = True
greenStep = New Timer
greenStep.Interval = 100
greenStep.Enabled = True
greenStep.Start()
End Sub
Public Shared Sub OnTimer(ByVal source As Object, ByVal e As EventArgs)
'greenLoc.Offset(2, 0)
GFX.DrawString("crap", New Font("Courier New", 10), Brushes.White, 50, 50)
FormGFX.DrawImage(Backbuffer, 0, 0)
'Console.WriteLine("Hello World!")
End Sub
End Structure
Iceplug 04-04-2005, 03:18 PM I'd definitely go with an arraylist containing all of the monster structures that you need.
You could then go through and draw them all with a For Each loop
For Each thing In Things
thing.Draw()
Next
and then you can remove it like this:
Things.Remove(thing)
To make the arraylist:
Dim Things As Arraylist
Things = New Arraylist
:)
death_entry 04-04-2005, 05:24 PM lol i tried that but then they got stuck in an infinite loop, probably why you use next and not while :P ok i'll try that
Iceplug 04-04-2005, 07:02 PM It shouldn't get in an infinite loop unless you tried to modify the loop variable.
death_entry 04-05-2005, 08:26 AM this is my code so far
Dim tempSprite As Bitmap
Dim tempLoc As Rectangle
Dim a As [Green]
For Each a In Enemies
tempSprite = a.sprite
tempLoc = a.greenLoc
GFX.DrawImage(a.sprite, a.greenLoc)
FormGFX.DrawImage(Backbuffer, 0, 0)
Next a
it stops on line
GFX.DrawImage(a.sprite, a.greenLoc)
and says "An unhandled exception of type 'System.NullReferenceException' occurred in system.drawing.dll
Additional information: Object reference not set to an instance of an object."
From what I can tell it's not loading the bitmap or the rectangle when it goes to draw it
Iceplug 04-05-2005, 08:32 AM If it's a yellow error, then you need to confirm that GFX is set to an instance of a graphics object.
GFX = Graphics.FromImage(Backbuffer)
What subroutine is this in and is it called after the subroutine that sets GFX to an instance.
And take out the
FormGFX.DrawImage(Backbuffer, 0, 0)
line from inside that loop.
That should only be executed once.
:)
death_entry 04-05-2005, 08:50 AM this is the entire draw routine, however this version is slightly different to the one I post earlier, it now compiles, but doesn't draw the aliens. For some reason it wasn't copying the bitmap information only the rectangle, so since I know its a green alien, I can provide the bitmap information in the draw function.
Private Sub Draw()
GFX.FillRectangle(Brushes.Black, Rectangle.FromLTRB(0, 0, 640, 480)) 'Fills a background
GFX.DrawImage(PlayerBmp, PlayerLoc) 'draw player one
GFX.DrawRectangle(Pens.Lime, Player2Loc) 'Draw Player Two
If PlayerLoc.IntersectsWith(Player2Loc) Then 'Check to Player One hit Player Two
GFX.DrawString("COLLISION!", New Font("Courier New", 10), Brushes.White, 50, 50) 'If they did write out that they hit
End If
Dim tempSprite As Bitmap
Dim tempLoc As Rectangle
Dim a As [Green]
tempSprite = New Bitmap(Application.StartupPath & "/Images/grunt_green_formation.jpg")
For Each a In Enemies
tempLoc = a.greenLoc
GFX.DrawImage(tempSprite, a.greenLoc)
FormGFX.DrawImage(Backbuffer, 0, 0)
Next a
GFX.DrawString("Hello World " + CStr(one.greenLoc.Location.X), New Font("Courier New", 10), Brushes.White, 10, 10)
'one.formation = 1
If two.formation = 1 Then GFX.DrawString("Help", New Font("Courier New", 10), Brushes.White, 50, 150)
FormGFX.DrawImage(Backbuffer, 0, 0) 'Make everything visable
End Sub
and for referance this is the "green" structure"
'Defines the Green enemy
Public Structure Green
Dim greenStep As Timer
Dim greenLoc As Rectangle
Dim sprite As Bitmap
Dim aTimer As Timer
Dim formation As Integer
Public Sub Green_Load(ByVal Xpoint As Integer, ByVal Ypoint As Integer)
formation = 1
greenLoc = New Rectangle(200, 100, 21, 16)
sprite = New Bitmap(Application.StartupPath & "/Images/grunt_green_formation.jpg")
greenLoc.Location = New Point(Xpoint, Ypoint)
aTimer = New Timer
AddHandler aTimer.Tick, AddressOf OnTimer
aTimer.Interval = 1
aTimer.Enabled = True
aTimer.Start()
End Sub
Public Sub OnTimer(ByVal source As Object, ByVal e As EventArgs)
' If formation = 0 Then greenLoc.Y += 1
If direction = 0 And formation = 1 Then greenLoc.X += 1
If direction = 1 And formation = 1 Then greenLoc.X -= 1
If greenLoc.X = 600 Then direction = 1
If greenLoc.X = 20 Then direction = 0
End Sub
End Structure
For some reason when I have tired to edit the formation integer of a specific instance eg "one" it seems to change it but it doesnt then make the green alien move. I have commented out the bit that is set to formation = 0 as I couldn't get it work based on having the formation variable changed.
Iceplug 04-05-2005, 09:00 AM What's the value of the rectangle(s) that .greenloc returns?
Are they all the same rectangle?
And I thought I told you to take
FormGFX.DrawImage(Backbuffer, 0, 0)
out of the loop :).
death_entry 04-05-2005, 09:13 AM Well i thought it was being returned each time it went round the loop as a?
When I was using a while loop I told it to return the object in the position of the array that the current count signified, I used to copy the returned green into another green variable, but i took that out. I think I might have to do that here, but I don't understand how to get the object in the array returned, thereby allowing me to take the rectangle information from it.
The rectangle informaiton is the same for each instance except the fact the X and Y values for its locaiton will be different, as the array holds 6 instances of the green structure, and it should draw them all.
ok i took the formgfx thing out :p
anyway i got the array thing to work but for some reason the aliens dont move as if the array only gets drawn once.
I've gone back to letting them being drawn from the structure but I still can't influence a specific instance, it's like once they have been created you can't change anything
anyway i've posted my project might make it easier to look at
|