Page 1 of 1

Using a variable for a sprite does not work

Posted: Fri Nov 25, 2016 4:07 pm
by falsam
■ This code works

Code: Select all

OpenScreen(800, 600, 32, "Test Sprites")

Procedure RenderFrame()  
  DisplayTransparentSprite(1, 400, 300)
  FlipBuffers()
EndProcedure

Procedure Loading(Type, Filename$)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 1
    ClearScreen(RGB(218, 165, 32))
    FlipBuffers() 
  EndIf
EndProcedure

Procedure LoadingError(Type, Filename$)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())

LoadSprite(1, "Data/spider.png")
■ But not this code

Code: Select all

OpenScreen(800, 600, 32, "Test Sprites")

Procedure RenderFrame()  
  DisplayTransparentSprite(spider, 400, 300)
  FlipBuffers()
EndProcedure

Procedure Loading(Type, Filename$)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 1
    ClearScreen(RGB(218, 165, 32))
    FlipBuffers() 
  EndIf
EndProcedure

Procedure LoadingError(Type, Filename$)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())

Global spider = LoadSprite(#PB_Any, "Data/spider.png")
There is no error in the console.

Re: [SB2.00] Using a variable for a sprite does not work

Posted: Sat Nov 26, 2016 12:11 am
by MrTAToad
Don't forget the display procedure cant be guaranteed to run after the sprite is loaded - and if it runs before it has, then the sprite variable will be invalid.

The sprite ends up being loaded into index 10000, whilst you end up displaying index 0...

Re: [SB2.00] Using a variable for a sprite does not work

Posted: Sat Nov 26, 2016 5:02 pm
by Fred
SpiderBasic it is a topdown compiler, so you need to declare your variable as global before using it in the procedure. You can use EnableExplicit to avoid these kind of issue

Code: Select all

OpenScreen(800, 600, 32, "Test Sprites")

Global spider

Procedure RenderFrame() 
  DisplayTransparentSprite(spider, 400, 300)
  FlipBuffers()
EndProcedure

Procedure Loading(Type, Filename$)
  Static NbLoadedElements
 
  NbLoadedElements+1
  If NbLoadedElements = 1
    ClearScreen(RGB(218, 165, 32))
    FlipBuffers()
  EndIf
EndProcedure

Procedure LoadingError(Type, Filename$)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())

spider = LoadSprite(#PB_Any, "Data/spider.png")

Re: Using a variable for a sprite does not work

Posted: Sat Nov 26, 2016 9:50 pm
by falsam
Fred wrote:SpiderBasic it is a topdown compiler
Thanks Fred. Noted.