Imagescollide
BlitzMax Forums/BlitzMax Beginners Area/Imagescollide
| ||
| So I know why this fail, I just have no idea how to fix it. when it get's to images collide, technically the bullet doesn't exist yet as I haven't pressed space bar, so it tells me that it doesn't recognize b. How would I make this code work? Repeat Cls For Local l:Objects = EachIn Objectlist l.Updatestate() l.Drawstate() Next For Local life:Int = 1 To Lives DrawRect 30*life,580,25,20 Next If KeyHit(key_space) Then Local b:Bullet = New Bullet.Create("Images/Bullet.png",hero.x,hero.y-20) If ImagesCollide(b.Image,b.x,b.y,0,e.Image,e.x,e.y,0) ListRemove Objectlist,e ListRemove Objectlist,b Exit EndIf If Lives = 0 Then End Flip Until AppTerminate() Or KeyHit(key_escape) I've tried adding "If Bullet.Objectlist" to the imagescollide code, it then decides to tell me that "Objectlist" does not exist. in spite of it working everywhere else in the code. |
| ||
not the best way of doing it but it will work:
'note that I loaded the image as a pixmap and stored it globally
Global pixmap:TPixmap = LoadPixmap("Images/bullet.png")
Repeat
Cls
' ************* Logic *******************
For Local l:Objects = EachIn Objectlist
l.Updatestate()
Next
For Local b:Bullet = EachIn objectList
For Local e:Enemy = EachIn objectList
If ImagesCollide(b.Image,b.x,b.y,0,e.Image,e.x,e.y,0)
ListRemove objectList,e
ListRemove objectList,b
EndIf
Next
Next
If KeyHit(key_space) Then
'this creates a bullets and stores it for future use.
'passes the pixmap to the collision instead going trough file access every time which is really slow.
Local b:Bullet = New Bullet.Create(pixmap,hero.x,hero.y-20) ' uses LoadImage(pixmap)
objectList.AddLast(b)
EndIf
If Lives = 0 Then End
' graphics done separate from logic.
For Local l:Objects = EachIn Objectlist
l.Drawstate()
Next
For Local life:Int = 1 To Lives
DrawRect 30*life,580,25,20
Next
Flip
Until AppTerminate() Or KeyHit(key_escape)
I would keep the bullets in a separate list so there can be different type of enemies in the enemy list while allowing collision of bullet with enemies. |