class question

Xtreme
02-05-2002, 12:18 PM
I am programming a mario type game and am at the point of putting enemies on scree. Well i want to do it so that first the map is read into an array. When the map is being read if it comes accross the symbol for an enemy it creates a new class object with the starting coordinates and the starting direction of the enemy. Now this object will be called within my main game loop to tell all the enemies that have been created to move and so on.

Now here's my goal: I needs each new enemy to be a seperate object so that each one has their own x, y, direction, speed and so on variables that will be affected differently for each one. Here is my code ---symplified

''This is in a module
Public BadGuy(100) As Enemy
Public bcounter as integer

'''Now when i read the file and come to the symbol representing an enemy then ''''''''''''''''''''''

Set BadGuy(bcounter) = New Enemy
BadGuy(bcounter).Initialize_Values (i * 32), (P * 32), 2
bcounter = bcounter + 1

'''Now in my main sub after i draw all of the tiles on screen i

Dim i As Integer

For i = 0 To bcounter
BadGuy(i).go
Next i

''''alright I hope everyone still understands what is going on. Now for the actual class!

Private x As Single
Private y As Single
Private direction As Byte
Private jumping As Boolean
Private speed As Integer

Sub Initialize_Values(user_x As Single, user_y As Single, user_speed As Integer)
speed = user_speed
x = user_x
y = user_y
direction = 0
jumping = False
End Sub

Private Sub Move(tile_map As tile)
Debug.Print y
If tile_map.map(Int(y / 32) + 1, Int(x / 32)).solid = False And tile_map.map(Int(y / 32) + 1, Int(x / 32)).platform = False And jumping = False Then
y = y + speed
End If

If tile_map.map(Int((y + 2) / 32), Int(x / 32)).solid = True Then
direction = 1
End If

If tile_map.map(Int((y + 2) / 32), Int((x / 32) + 1)).solid = True Then
direction = 0
End If

If direction = 1 Then
x = x + speed
End If

If direction = 0 Then
x = x - speed
End If

draw_enemy

End Sub

Public Sub go()

Move game

End Sub
Public Sub draw_enemy()
extraRECT.Top = 32
extraRECT.Bottom = 64
extraRECT.left = 0
extraRECT.right = 32

''clipper is a function that blts the character to the screen
Clipper extra, BackBuffer, extraRECT, (x - 320) - lowerxoff, (y) - loweryoff, True, True


End Sub

''''This is the end of the modual

Ok my problem is that if I put more than one enemy in the map that is read then only the last one that is read is created?
What am i doing wrong?


Thanks

Teric
02-05-2002, 01:32 PM
Have you tried using a Collection to hold all of your enemy objects (instead of using an array)?

It's difficult to tell the problem from just the code that you have pasted here. Could you attach your code modules to a post so we can see the whole program?

Thanks!

Xtreme
02-06-2002, 08:04 AM
:) Well the code is really to large to post i'm afraid not everyone would understand it. However to put it simply, my question is this:

I want 100 seperate objects named enemy.

(i figue to do this i must have do something like
Public enemy(100) as new name_of_my_class)

(unless theirs another way also is their a way to declare the 100 objects dynamically so that i only create the neccessary number of enemy objects as i need? )

Thats my question.



Thanks

Teric
02-06-2002, 10:35 AM
Yes--you can use a Collection to dynamically allocate and de-allocate memory for your enemy objects. (That way, you don't have to have a huge array sitting there eating up memory.)

Once you understand how to use a collection, it makes things a lot easier when you want to use a number of instances of the same object.

I haven't checked, but there may be a tutorial on how to use collections over in the Tutorials forum.

If anything, go take a look at my 'Working on a Basic RPG--TAKE A LOOK' thread. My RPG uses objects and collections to represent enemies, items, etc. You could take a look at my code to see how it is done.

Squirm
02-06-2002, 10:50 AM
The tutorial you speak of is here (http://www.visualbasicforum.com/showthread.php?s=&threadid=9065) :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum