Page 1 of 1

Sprite drawing problem

Posted: Wed Sep 22, 2010 3:09 pm
by Jago
I'm trying to draw a tile map using sprites. The map is stored in a two dimensional array cells(35,25). Each cell has a type (wall or floor) and sprite. Problem is that when i try to loop through the array and display every sprite, the program just shows a light grey screen. Sprites are displayed correctly if I display them "one at a time" by using multiple DisplaySprite()-commands, but not within a For-loop. Here's some code to explain my problem little more:

Works

Code: Select all

StartDrawing(ScreenOutput())

DisplaySprite(cells(0,0)\sprite,100,100)
DisplaySprite(cells(0,1)\sprite,200,200)

StopDrawing()
Doesn't work

Code: Select all

StartDrawing(ScreenOutput())

For x = 0 To #X_CELLS
	For y = 0 To #Y_CELLS
		If cells(x,y)\type = #WALL
			DisplaySprite(cells(x,y)\sprite,x * #CELL_SIZE,y * #CELL_SIZE)
			;Box(x * #CELL_SIZE,y * #CELL_SIZE,#CELL_SIZE,#CELL_SIZE,RGB(128,128,128))
		EndIf
	Next y
Next x

StopDrawing()

Re: Sprite drawing problem

Posted: Wed Sep 22, 2010 3:34 pm
by Trond
Some suggestions:

A. Remove the if statement that checks for a wall and try again.
B. Make sure the box command is commented out. If not, you will get a grey screen.
C. Run "Debug #CELL_SIZE" to make sure it is set to something sensible (should be a positive number)

Re: Sprite drawing problem

Posted: Wed Sep 22, 2010 3:38 pm
by Rook Zimbabwe
Also make sure you are only trying to draw the cells that are actualkly ON SCREEN at the moment... (for now)

You might want to make sure you filled in your array too. Simple Debug should tell you what cell number is in each when you create it... also did you modify this array inside a procedure and not GLOBAL it?

The other thing I did was to make my array 3D... LAYER(X,Y,Z) and then put all the walls on layer (say 1) and then DRAW that (or I drew ALL... simple switch commnd)

Re: Sprite drawing problem

Posted: Wed Sep 22, 2010 3:44 pm
by Comtois
here an example

Code: Select all

InitSprite()
InitKeyboard()
ExamineDesktops()
OpenScreen(DesktopWidth(0), DesktopHeight(0), DesktopDepth(0),"Game")

Structure Game
  Type.i
  other.i
EndStructure

#CELL_SIZE = 16
#X_CELLS = 35
#Y_CELLS = 25

Dim cells.Game(#X_CELLS, #Y_CELLS)  

For x = 0 To #X_CELLS
   For y = 0 To #Y_CELLS
      cells(x,y)\type = Random(1)
   Next y
Next x

Enumeration 
  #WALL
  #FLOOR
EndEnumeration

CreateSprite(#WALL, #CELL_SIZE, #CELL_SIZE)
If StartDrawing(SpriteOutput(#WALL))
  Box(0, 0, #CELL_SIZE, #CELL_SIZE, RGB(255, 0, 155))
  StopDrawing()
EndIf

CreateSprite(#FLOOR, #CELL_SIZE, #CELL_SIZE)
If StartDrawing(SpriteOutput(#FLOOR))
  Box(0, 0, #CELL_SIZE, #CELL_SIZE, RGB(255, 255, 55))
  StopDrawing()
EndIf


Repeat
ClearScreen(0)
ExamineKeyboard()
For x = 0 To #X_CELLS
   For y = 0 To #Y_CELLS
      DisplaySprite(cells(x,y)\Type,x * #CELL_SIZE,y * #CELL_SIZE)
   Next y
Next x
FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)

Re: Sprite drawing problem

Posted: Fri Sep 24, 2010 3:01 pm
by Jago
Thank you, for all. Problem solved.