Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Quintaros on Tue 23/08/2016 19:50:56

Title: Assigning DynamicSprite Graphics to Room objects <solved>
Post by: Quintaros on Tue 23/08/2016 19:50:56
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) Select

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(); 
    }   
  }
}
Title: Re: Assigning DynamicSprite Graphics to Room objects
Post by: morganw on Tue 23/08/2016 20:39:08
Quote from: Quintaros on Tue 23/08/2016 19:50:56
Code (ags) Select
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().
Title: Re: Assigning DynamicSprite Graphics to Room objects
Post by: Crimson Wizard on Tue 23/08/2016 21:08:58
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.
Title: Re: Assigning DynamicSprite Graphics to Room objects <solved>
Post by: Quintaros on Tue 23/08/2016 22:16:34
Thanks guys.  Declaring my DynamicSprites at the top of the room script outside of the room load function seems to have done the trick.