Assigning DynamicSprite Graphics to Room objects <solved>

Started by Quintaros, Tue 23/08/2016 19:50:56

Previous topic - Next topic

Quintaros

I'm trying to implement a puzzle that involves sorting some objects that are initially randomly oriented.  The code below was intended to initialize the objects orientation but instead of replacing the object's sprite with a rotated sprite, the objects get the famous blue cup sprite.  Any suggestions for what I'm doing wrong?  Thanks.


Code: ags

function room_Load(){
  DynamicSprite* sprite[19];
  for(int i=0;i<19;i++){    
    sprite[i] = DynamicSprite.CreateFromExistingSprite(object[i].Graphic);
    int starting_rotation=Random(3);
    if (starting_rotation!=0){
      sprite[i].Rotate(starting_rotation*90);
      object[i].Graphic=sprite[i].Graphic;      
      sprite[i].Delete();  
    }    
  }
}

morganw

Quote from: Quintaros on Tue 23/08/2016 19:50:56
Code: ags
sprite[i].Delete();


You need to keep the DynamicSprite pointer and delete it later, when you don't need it anymore. I think you just need to remove this line and then find another place to cleanup everything (probably when you leave the room and make sure any quit functionaility initiates a room change). Also, it's normally best to check it isn't equal to null before calling Delete().

Crimson Wizard

#2
What morganw said; to elaborate a bit:
You MUST keep dynamic sprites in global pointer variable if you are going to use this sprite outside of one function. If you keep them in local variable only, they will be disposed as soon as function ends. And if you assigned DynamicSprite.Graphic number to any object, your game will crash, or draw default "blue cup" sprite instead.

Quintaros

Thanks guys.  Declaring my DynamicSprites at the top of the room script outside of the room load function seems to have done the trick.

SMF spam blocked by CleanTalk