GetNextSprite
SpriteIndex = GetNextSprite(SpriteIndex)
 
Parameters:

    SpriteIndex = The sprite index you wish to query
Returns:

    SpriteIndex = The Next Sprite Index after the sprite yo
 

      The GetNextSprite function returns the Index of the Next Sprite after the Sprite you've queried.


      Active sprites, like all resources in PlayBasic are internally managed through a linked list. Each time a new sprite is created (regardless of it's sprite index), it's placed at the head of the list. So sprites are linked together from the most recently created sprite (the newest), to the oldest sprite.


      To traverse the sprite list, first we need to the grab the head sprite from PlayBasics internal linked list. We do this using GetFirstSprite. This command grabs the most recently created sprite from the head of sprite list for us.


      Once we have the first sprite in the list, we can then step through all the created sprites using GetNextSprite.

I.e This sample code steps through the entire sprite list.

  
; Grab the first Sprite Index from the head
; of the sprite link list and place it in the
; variable ThisSprite
  ThisSprite=GetFirstSprite()
  
; Start of While/EndWhile loop. The loop will continue
; until "ThisSprite" variable is equal to or lower than zero
; (end of list)
  While ThisSprite>0
   ; << Do something with This Sprite here >>
     
     
   ; Get the next sprite  in the List
     ThisSprite=GetNextSprite(ThisSprite)
     
   ; Continue the While loop
  EndWhile
  


PlayBasic uses the linked list in everything from rendering to collsion (ie. DrawAllSprites, DrawOrderedSprites, SpriteHit)

      While we could also step through the sprite list and render them using DrawSprite, We'll most commonly be using GetFirstSprite, GetNextSprite commands in conjunction with the sprite collision. (See: SpriteHit).





FACTS:


      * GetNextSprite() will return a ZERO if there is no next sprite.

      * The sprite list will be in order the sprites were created, and not the sprites index numbers.

      * See GetFirstSprite



Mini Tutorial:


      This first part of example creates 10 sprites. The sprite indexes will be between 100 and 110. Then in the second part, we use the GetFirstSprite and functions to step through our list of created sprites.


  
; =============================
; Part #1
; =============================
  
; Create 10 sprites randomly
  For lp =1 To 10
     
   ; Create the sprite
     CreateSprite 100+lp
     
  Next
  
; =============================
; Part #2
; =============================
  
; Display a Heading Message
  Print "============================"
  Print "Linked_List of Created Sprites:"
  Print "============================"
  
; Get the First Created Sprite.
  ThisSprite=GetFirstSprite()
  
; While ThisSprite index is NOT 0, then run the loop
  While ThisSprite<>0
     
   ; Display this sprite index
     Print "Sprite :"+Str$(ThisSprite)
     
   ;Read what sprite is after this one
     ThisSprite=GetNextSprite(ThisSprite)
  EndWhile
  
; Display the screen and wait for key press
  Sync
  WaitKey
  
  



This example would output.

  
  ============================
Linked_List of Created Sprites:
  ============================
  Sprite :110
  Sprite :109
  Sprite :108
  Sprite :107
  Sprite :106
  Sprite :105
  Sprite :104
  Sprite :103
  Sprite :102
  Sprite :101
  





 
Related Info: CreateSprite | GetFirstSprite | GetFreeSprite | NewSprite | SpriteHit :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com