Using a variable for a sprite does not work

Just starting out? Need help? Post your questions and find answers here.
falsam
Posts: 288
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Using a variable for a sprite does not work

Post 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.

➽ Windows 11 - jdk-11.0.2 - SB 3.10 - Android 16
https://falsam.com

Sorry for my poor english
User avatar
MrTAToad
Posts: 291
Joined: Sun Apr 20, 2014 11:43 am
Location: Chichester, England
Contact:

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

Post 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...
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

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

Post 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")
falsam
Posts: 288
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: Using a variable for a sprite does not work

Post by falsam »

Fred wrote:SpiderBasic it is a topdown compiler
Thanks Fred. Noted.

➽ Windows 11 - jdk-11.0.2 - SB 3.10 - Android 16
https://falsam.com

Sorry for my poor english
Post Reply