Here's a better working example. :)
It uses defdata to store map data. It would be the same if you created the map with a map editor.
Think of the defdata as what is stored in the map when you output from TileStudio etc.
Run whats below in max and it should work out the box. :)
SuperStrict
Const TILESIZE:Int = 50
Const MAPWIDTH:Int = 16
Const MAPHEIGHT:Int = 12
Global MapArray:Int[MAPWIDTH, MAPHEIGHT]
Graphics 800, 600
Type TEnemy
Field x:Int
Field y:Int
Field EID:Int
Field Direction:Int
Global List:TList
Global TempID:Int = 0
Global EnemyCreated:Int = 0
Function Create()
If not TEnemy.List
TEnemy.List = CreateList()
End If
For Local xiter:Int = 0 Until MAPWIDTH
For Local yiter:Int = 0 Until MAPHEIGHT
If MapArray[xiter, yiter]= 3
Local e:TEnemy = New TEnemy
e.x = TEnemy.GetX(xiter)
e.y = TEnemy.GetY(yiter)
e.EID = TEnemy.TempID
e.Direction = Rand(1)
TEnemy.List.AddLast(e)
TEnemy.TempID:+1
TEnemy.EnemyCreated = 1
End If
Next
Next
End Function
Method DrawEnemy()
SetColor 255, 0, 0
DrawRect x, y, TILESIZE, TILESIZE
SetColor 255, 255, 255
End Method
Method MoveEnemy()
If Direction = 1
x:+2
ElseIf Direction = 0
x:-2
End If
If MapArray[x / TILESIZE, y / TILESIZE + 1]= 0 or MapArray[x / TILESIZE + 1, y / TILESIZE + 1]= 0
If Direction = 0
Direction = 1
ElseIf Direction = 1
Direction = 0
EndIf
End If
If MapArray[x / TILESIZE, y / TILESIZE]= 1 or MapArray[x / TILESIZE + 1, y / TILESIZE]= 1
If Direction = 0
Direction = 1
ElseIf Direction = 1
Direction = 0
EndIf
EndIf
End Method
Function GetX:Int(XPosition:Int)
Return XPosition * TILESIZE
End Function
Function GetY:Int(YPosition:Int)
Return YPosition * TILESIZE
End Function
End Type
For Local yiter:Int = 0 Until MAPHEIGHT
For Local xiter:Int = 0 Until MAPWIDTH
Local MapData:Int
ReadData MapData
MapArray[xiter, yiter]= MapData
Next
Next
TEnemy.Create
While not KeyHit(KEY_ESCAPE)
Cls
DrawMap()
If TEnemy.EnemyCreated = 1
For Local e:TEnemy = EachIn TEnemy.List
e.DrawEnemy
e.MoveEnemy
Next
End If
Flip
WEnd
Function DrawMap()
For Local xiter:Int = 0 Until MAPWIDTH
For Local yiter:Int = 0 Until MAPHEIGHT
If MapArray[xiter, yiter]= 1
DrawRect xiter * TILESIZE, yiter * TILESIZE, TILESIZE, TILESIZE
End If
Next
Next
End Function
' 1 = wall
' 3 = Enemy
DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0, 1
DefData 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1