Posted: Thu Jun 08, 2017 4:55 pm Post subject: max id error with sprites... CONTINUED
So i'm making a sword fighting game in turing and after repeated use of the attack button an error occurs and the game crashes. It's a max ID error i think becuase each time the player attacks the sprite image is replaced. If you have a suggestion can you please write the code? I'm kind of new to sprites and stuff. Thank You Smile
The following is the code snippet:
% Sword Attack
elsif arrowInput (' ') and playerYValue <= ground then
player := Pic.FileNew ("playerBlazeSword.bmp")
player := Pic.Scale (player, 152, 165)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
View.Update
player := Pic.FileNew ("playerBlaze.bmp")
player := Pic.Scale (player, 152, 165)
Sprite.ChangePic (playerSprite, player)
Sprite.SetPosition (playerSprite, playerXValue, playerYValue, true)
delay (20)
plsyerBlazeSword.bmp is the character picture with a sword
playerBlaze is a regular picture of the player
Sponsor Sponsor
Insectoid
Posted: Thu Jun 08, 2017 5:57 pm Post subject: RE:max id error with sprites... CONTINUED
Are you sure it's an illegal sprite ID and not an illegal picture ID? You need to use Pic.Free to delete unnecessary images from memory before you can re-assign the variable.
If you aren't using more than a thousand pictures, you might as well load all of them and give them all unique variables at the beginning of the game. Then you only need to use Sprite.ChangePic to swap, no need for Pic.FileNew.
Also, look into Sprite.Animate. it combines all your standard sprite functions into a single line.
programmer_needs_help
Posted: Thu Jun 08, 2017 6:06 pm Post subject: RE:max id error with sprites... CONTINUED
Process "playerMovement": Cannot allocate item. Out of id numbers (max 1000)
is the error message
programmer_needs_help
Posted: Thu Jun 08, 2017 6:08 pm Post subject: RE:max id error with sprites... CONTINUED
i don't understand what you mean by "You need to use Pic.Free to delete unnecessary images from memory before you can re-assign the variable." Can you show me where to put that? I tried it before.
programmer_needs_help
Posted: Thu Jun 08, 2017 6:28 pm Post subject: RE:max id error with sprites... CONTINUED
omg I fixed it thanks so much!!!!! Your advice worked
Insectoid
Posted: Thu Jun 08, 2017 6:30 pm Post subject: RE:max id error with sprites... CONTINUED
You are only allowed to create 1000 images in Turing for technical reasons. Any time you call Pic.FileNew or Pic.Scale, a new image is created. If you're doing that in a loop, it doesn't take long to reach 1000. To get around this, you need to use Pic.Free to delete the pictures you're done using. Deleted pictures no longer count towards the 1000 picture limit.
In your example, you'll need an extra variable. We'll call it 'rawImage', because it will hold the unscaled image.
RawImage := Pic.FileNew ("blah.bmp")
Player := Pic.Scale (RawImage)
Pic.Free (RawImage)
Now, before you chance player to a new image later, you need to free it too:
Pic.free (player)
programmer_needs_help
Posted: Thu Jun 08, 2017 6:30 pm Post subject: RE:max id error with sprites... CONTINUED
now it says:
Process "playerMovement": Pic was freed
programmer_needs_help
Posted: Thu Jun 08, 2017 6:35 pm Post subject: RE:max id error with sprites... CONTINUED
If you're wondering, all this code is in the playerMovement process
Insectoid
Posted: Thu Jun 08, 2017 6:38 pm Post subject: RE:max id error with sprites... CONTINUED
You have to free the image after you are done with it. Freeing the image deletes it from memory, which means you cannot use it anymore. You can't draw it and you can't assign it to a sprite.
So tell me why you are freeing an image immediately before assigning it to a sprite?
programmer_needs_help
Posted: Thu Jun 08, 2017 6:41 pm Post subject: RE:max id error with sprites... CONTINUED