I'm trying to figure out why my list of entities is only updating two object at a time and not all of them.
Below is the code for tEntity:
Here is the game loop:
When you run the code Sometimes your box will collide with the others and sometimes not. Why? If you only create two enities, everything is fine. This is driving me up a wall. Any help is greatly appreciated.
Below is the code for tEntity:
Strict Global EntityList:TList = CreateList() Type tEntity Field X:Float, Y:Float Field VX:Float, VY:Float Field Width:Int Field Height:Int Field Pushable:Int Method New() EntityList.AddLast(Self) X = Rnd(0,640) Y = Rnd(0,480) Width = 32 Height = 16 Pushable = True End Method Method Compare(_O:Object) Return Y - tEntity(_O).Y End Method Method Delete () EntityList.Remove(Self) End Method Method Render () SetColor 0,0,0 DrawRect X-1, Y-Height-1, Width+2, Height+Height+2 SetColor 200,200,200 DrawRect X, Y, Width, Height SetColor 125,125,125 DrawRect X, Y-Height, Width, Height End Method Method Update () MoveIfNotCollided(VX, VY) End Method Method MoveIfNotCollided(_VX:Float, _VY:Float) Local e:tEntity X :+ _VX e = CollideWithEntity() If (e <> Null) If e.Pushable X :- (_VX * .5) e.X :+ (_VX * .5) Else X :- _VX End If End If Y :+ _VY e = CollideWithEntity() If (e <> Null) If e.Pushable Y :- (_VY * .5) e.Y :+ (_VY * .5) Else Y :- _VY End If End If End Method Method CollideWithEntity:tEntity () Local e:tEntity For e = EachIn EntityList If e <> Self If (e.X > X + Width) Return Null If (e.X + e.Width < X) Return Null If (e.Y > Y + Height) Return Null If (e.Y + e.Height < Y) Return Null Return e End If Next End Method End Type
Here is the game loop:
Graphics 640,480 SetClsColor 0,150,0 Local p1:tEntity = New tEntity Local p2:tEntity = New tEntity Local p3:tEntity = New tEntity Local Controls:Int [] = [KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT] While Not KeyHit(KEY_ESCAPE) Cls p1.VX = 0; p1.VY = 0 If (KeyDown(Controls[0])) p1.VY :+ -2 'UP If (KeyDown(Controls[1])) p1.VY :+ 2 'DOWN If (KeyDown(Controls[2])) p1.VX :+ -2 'LEFT If (KeyDown(Controls[3])) p1.VX :+ 2 'RIGHT EntityList.Sort For Local e:tEntity = EachIn EntityList e.Update() e.Render() Next Flip FlushMem Wend
When you run the code Sometimes your box will collide with the others and sometimes not. Why? If you only create two enities, everything is fine. This is driving me up a wall. Any help is greatly appreciated.