This is just a simple demonstration of how to handle the creation and deletion of objects using dynamic arrays and how to give your objects (in this case sprites) a unique number.
No Media Required
Sync On : Hide Mouse
Ink RGB(0,255,0),1
Box 0,0,30,30
Ink RGB(255,255,255),1
Get Image 1,0,0,30,30
Sprite 1,0,0,1
Hide Sprite 1
Type Object
X as Float
Y as Float
SpriteNum as Integer
EndType
Dim SpriteNumber(0) as Integer
Dim Objects(0) as Object
Global BaseTime as Float : BaseTime=Timer()
Global Update as Integer : Update=0
For a=1 to 10
CreateObject()
Next a
Do
If UpKey() =1 then CreateObject()
If DownKey() =1 and Array Count(Objects(0))>0 then DestroyObject()
If Spacekey()=1 then CheckSprites()
For aObject=1 to Array Count(Objects(0))
Paste Sprite Objects(aObject).SpriteNum,Objects(aObject).X,Objects(aObject).y
Next aObject
Set cursor 0,0
Print "Objects:",Array Count(Objects(0))
Print "FPS:" ,Screen fps()
Print "Press Upkey to Create Objects"
Print "Press DownKey to Destroy Objects"
Print "Press SpaceKey to Check the Objects for Duplicate sprite numbers"
sync
Loop
Function CreateObject()
Array Insert at Bottom Objects(0)
Local NewObject as Integer : NewObject=Array Count (Objects(0))
Objects(NewObject).X=Rnd(Screen Width())
Objects(NewObject).Y=Rnd(Screen Height())
Objects(NewObject).SpriteNum=CreateSpriteNumber()
Sprite Objects(NewObject).SpriteNum,0,0,1
Hide Sprite Objects(NewObject).SpriteNum
EndFunction
Function DestroyObject()
Local DeletedObject : DeletedObject = Rnd(Array Count(Objects(0))-1)+1
FreeSpriteNumber(Objects(DeletedObject).SpriteNum)
Array Delete Element Objects(0),DeletedObject
EndFunction
Function CheckSprites()
For aSprite=1 to Array Count(SpriteNumber(0))
For bSprite=1 to Array Count(SpriteNumber(0))
If aSprite<>bSprite
If SpriteNumber(aSprite)=SpriteNumber(bSprite)
Exit Prompt "There are sprites with the same Spritenumber", "Error CheckSprites()" : End
EndIf
EndIf
Next bSprite
Next aSprite
EndFunction
Function CreateSpriteNumber()
If Array Count(SpriteNumber(0))>10000
Exit Prompt "You have reached The maximum amount of sprites allowed", "CreateSpriteNumber Error" : End
EndIf
`Pick a Sprite Number
Local aSpriteNum : aSpriteNum=(Rnd(10000)+1)
Local Unique as Integer : Unique=0
`Check if the Sprite number is being used if so pick another new number
Repeat
If Unique=0
Unique=1
For aSprite=1 to Array Count(SpriteNumber(0))
If SpriteNumber(aSprite) = aSpriteNum then aSpriteNum=(Rnd(10000)+1) : Unique=0
Next aSprite
EndIf
Until Unique=1
`If the Sprite Number is free to use then assign it to the SpriteNumber Array
Array Insert at Bottom SpriteNumber(0)
SpriteNumber(Array Count(SpriteNumber(0))) = aSpriteNum
EndFunction aSpriteNum
Function FreeSpriteNumber(aSpriteNum as Integer)
For aSprite=1 to Array Count(SpriteNumber(0))
If aSpriteNum=SpriteNumber(aSprite) then Array Delete Element SpriteNumber(0),aSprite : ExitFunction
Next aSprite
Exit Prompt "Sprite Number " + str$(aSpriteNum)+" that you are trying To delete Does not Exist", "FreeSpriteNumber Error"
End
EndFunction
Better to be dead, than to live your life afraid.