Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: ROOKMAGE on Fri 18/04/2014 05:42:23

Title: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Fri 18/04/2014 05:42:23
I've placed this code under Repeatedly_execute_always:
Code (ags) Select
if (IsTimerExpired(5))
   {
   int randomframe = Random(360);
   DynamicSprite* turnsprite = DynamicSprite.CreateFromExistingSprite(blockArray[selectedobject.graphic].sprite1);
   turnsprite.Rotate(randomframe);
   selectedObject.Graphic = turnsprite.Graphic;
   turnsprite.Delete();
      SetTimer(5, 10);
   }


The code is meant to randomly rotate an object (blockarray's object) every 10 game loops. However, when I run it, the object's sprite disappears.
Sorry for posting if this has already been asked, but I couldn't find anything. Is this even possible?
Title: Re: Rotating objects with Dynamicsprites
Post by: Slasher on Fri 18/04/2014 06:22:20
.
Title: Re: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Fri 18/04/2014 15:32:29
Quote from: slasher on Fri 18/04/2014 06:22:20
.
?
I'm a bit confused ???
Title: Re: Rotating objects with Dynamicsprites
Post by: Snarky on Fri 18/04/2014 16:16:11
I would guess the problem is that you call turnsprite.Delete(), and at that point the sprite ceases to exist and the selectedObject.Graphic becomes invalid. Try waiting until you no longer want to display the object before calling Delete() on the sprite.
Title: Re: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Fri 18/04/2014 16:29:44
Just tried that out, I'm still getting the same error. Some tests have led me to believe I'm just not using the Turnsprite.Rotate() properly because I can't seem to get it to work, even when not called repeatedly or with a random variable.
Title: Re: Rotating objects with Dynamicsprites
Post by: Khris on Fri 18/04/2014 21:11:56
Try this:

Code (ags) Select
DynamicSprite* turnsprite;  // above rep_ex_always

  // inside rep_ex_always
  if (IsTimerExpired(5)) {
    int randomframe = Random(360);
    turnsprite = DynamicSprite.CreateFromExistingSprite(blockArray[selectedobject.graphic].sprite1);
    turnsprite.Rotate(randomframe);
    selectedObject.Graphic = turnsprite.Graphic;
    SetTimer(5, 10);
  }
Title: Re: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Sat 19/04/2014 01:33:20
Still no luck. Do you think anything outside of the code could be doing anything?
Title: Re: Rotating objects with Dynamicsprites
Post by: Khris on Sat 19/04/2014 12:26:16
I guess the most likely explanation is that this:
blockArray[selectedobject.graphic].sprite1
is equal to 0.

Try plugging in a fixed sprite slot to see if the code works in principle.
Title: Re: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Sat 19/04/2014 17:43:22
So, it's definitely the code I'm using for rotation that's not working.
Code (ags) Select
DynamicSprite* turnsprite = DynamicSprite.CreateFromExistingSprite(41);

   if (IsTimerExpired(5))
   {
   int randomframe = Random(360);
   
   turnsprite.Rotate(randomframe);
   selectedObject.Graphic = turnsprite.Graphic;
      SetTimer(5, 10);
      turnsprite.Delete();
      }
     


does the same exact thing, where the sprite becomes completely invisible until I reassign it.
Title: Re: Rotating objects with Dynamicsprites
Post by: Khris on Sat 19/04/2014 18:10:14
That looks like it's not what I suggested to try though. Try this:

Code (ags) Select
DynamicSprite* turnsprite;
     
function repeatedly_execute_always {
  if (IsTimerExpired(5)) {
    int randomframe = Random(360);
    turnsprite = DynamicSprite.CreateFromExistingSprite(41);
    turnsprite.Rotate(randomframe);
    selectedObject.Graphic = turnsprite.Graphic;
    SetTimer(5, 10);
  }
}
Title: Re: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Sat 19/04/2014 21:22:49
Code (ags) Select
if (IsTimerExpired(5))
   {
   turnsprite = DynamicSprite.CreateFromExistingSprite(blockArray[selectedArray].sprite1);
   int randomframe = Random(360);
   turnsprite.Rotate(Random(359) + 1, 204, 152);
   selectedObject.Graphic = turnsprite.Graphic;
      SetTimer(5, 5);

This works. Thanks!
Title: Re: Rotating objects with Dynamicsprites
Post by: Snarky on Sat 19/04/2014 21:54:43
Well, that's good. On a somewhat unrelated topic, fix your indentation, man! Tabs are not just some stylistic choice to be sprinkled through your code based on whatever you feel like. You indent everything within the same set of braces by the same amount (and if you use another pair of braces inside, you indent that chunk by another level, or if you have single-line conditional commands where the braces have been omitted).
Title: Re: Rotating objects with Dynamicsprites
Post by: ROOKMAGE on Sat 19/04/2014 22:38:12
Yeah :P, I've gotta go through my script and label everything as well as fix the way my code works.