Sprite drawing problem

Just starting out? Need help? Post your questions and find answers here.
Jago
User
User
Posts: 11
Joined: Tue Sep 14, 2010 4:01 pm

Sprite drawing problem

Post 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()
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Sprite drawing problem

Post 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)
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Sprite drawing problem

Post 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)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Sprite drawing problem

Post 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)
Please correct my english
http://purebasic.developpez.com/
Jago
User
User
Posts: 11
Joined: Tue Sep 14, 2010 4:01 pm

Re: Sprite drawing problem

Post by Jago »

Thank you, for all. Problem solved.
Post Reply