Rotating objects with Dynamicsprites

Started by ROOKMAGE, Fri 18/04/2014 05:42:23

Previous topic - Next topic

ROOKMAGE

I've placed this code under Repeatedly_execute_always:
Code: ags
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?

Slasher

#1
.


Snarky

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.

ROOKMAGE

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.

Khris

Try this:

Code: ags
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);
  }

ROOKMAGE

Still no luck. Do you think anything outside of the code could be doing anything?

Khris

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.

ROOKMAGE

So, it's definitely the code I'm using for rotation that's not working.
Code: ags
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.

Khris

That looks like it's not what I suggested to try though. Try this:

Code: ags
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);
  }
}

ROOKMAGE

Code: ags
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!

Snarky

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).

ROOKMAGE

Yeah :P, I've gotta go through my script and label everything as well as fix the way my code works.

SMF spam blocked by CleanTalk