SOLVED: Dynamic Sprites for Dummies

Started by Baron, Thu 01/01/2015 21:17:35

Previous topic - Next topic

Baron

I've got a sprite that I want to build in layers like a sandwich.  So I figured I'd try something called "Dynamic Sprites" that I read about in the manual.  As far as I could figure, if you want to draw on a sprite (which is necessary, because I want the sandwich to move), you have to first create a dynamic sprite, then create a drawing surface for it, then draw something on the surface, and then release.  So I pieced me together a bit of code like so:

Code: ags

DynamicSprite* BurgerOne = DynamicSprite.Create(120, 100, true); 
DrawingSurface *BurgOneSurface = BurgerOne.GetDrawingSurface();
BurgOneSurface.DrawImage (100, 100, 292);  //sprite slot 292 is a slice of tomato
BurgOneSurface.Release ();


...and I got nothing.  Anyone see where I went wrong?  What I'm interested in doing is repeating line 3 over and over again to build up the sandwich, and then call release.  Is this even possible?

Crimson Wizard

#1
Well, first of all, did you assign your dynamic sprite to anything? Like Gui button or Room object, or Overlay? Sprites do not get drawn on screen on their own.

Secondly, if your DynamicSprite reference is kept only as a local variable, then it gets disposed as soon as function ends. To keep them in memory, make a global variable instead.

Quote from: Baron on Thu 01/01/2015 21:17:35What I'm interested in doing is repeating line 3 over and over again to build up the sandwich, and then call release.  Is this even possible?
Sure, you can draw anything on it, its just a normal rectangle of pixels as a regular static sprite.

johanvepa

#2
If what you're doing is putting an optional number of stuff on top of each other ("on top of" as viewed from the side when you look at the screen), you might want to do a simpler solution using similarly-sized objects placed "on top of" each other to form a "stack", then assign the correct graphics to an object and turn its transparency on/off as you choose what filling the sandwich should have.

You might also use a function to edit all of the objects' xy-coordinates simultaneously to emulate object movement.

If you want to do the same stuff with dynamic sprites, you might want to check out how I learned to use dynamic sprites, which with a lot of help from experienced board members became a sort of dynamicsprites for dummies.

Baron

Thanks guys.

@Nanuaraq: I initially thought I could pull this off with stacking objects, but the idea doesn't scale very well. (roll)

@Crimson Wizard: I'm missing the bit in the manual where it says you need to assign the dynamic sprite to an object, etc.  But declaring the dynamic sprite outside of the function is a good idea! ;-D  How do I assign a dynamic sprite to an object?

I've read through Nanuaraq's link, but I'm still not seeing where Khris assigns the dynamic sprite to anything (in his case it seems to be the main character, in mine it will be an object).  Is that what all those ViewFrame pointers are for?  The manual seems to indicate that those are just pointing at sprites assigned to various frames in view loops. Could someone please direct me to the manual section where it explains assigning dynamic sprites to objects?  Thanks.

Crimson Wizard

The DynamicSprite has Graphic property, which is sprite index. You use it anywhere where you could use a sprite index.

E.g.:
Code: ags

Object.Graphic = BurgerOne.Graphic;
Button.NormalGraphic = BurgerOne.Graphic;
ViewFrame.Graphic = BurgerOne.Graphic;


The dynamic sprite is just another sprite, the difference from static sprites is that you can create and modify them at runtime.

Baron

Aha!  The missing piece of the puzzle! ;-D

Except now my object just disappears, so I'm somehow literally drawing a blank now. (roll)

Code: ags

// room script file

DynamicSprite* BurgerOne;

function room_Load(){
  BurgerOne = DynamicSprite.Create(120, 100, true);
  DrawingSurface *BurgOneSurface = BurgerOne.GetDrawingSurface();
  BurgOneSurface.DrawImage (oBurger1.X, oBurger1.Y, 292);
  BurgOneSurface.Release ();
  oBurger1.Graphic = BurgerOne.Graphic;

}


I get the same results if I pass absolute coordinates on line 8, and if I declare the drawing surface at the top of the script instead of inside the function.  When I comment out line 10 the burger object still appears where it was placed in the room, so somehow I'm creating a blank dynamic sprite (but sprite slot 292 for sure has a graphic in it).  So.... somehow it is deleting itself without that being called (but I would have thought that declaring both the pointers outside the function would have solved that), or somehow transparency has been activated, or somehow the drawing is happening off the screen.  I'm guessing it's something to do with the first scenario, but I can't think why it would be deleting itself. 

Cassiebsg

And me thinking that making burgers was a simple task... (laugh)
There are those who believe that life here began out there...

Crimson Wizard

Baron, when you draw on surface, all coords are related to surface 0;0, not the room 0;0.

Forget about your object placement, think of drawing a picture in graphics editor. You don't know where this picture will get applied later.

Do this instead:
Code: ags

BurgOneSurface.DrawImage (0, 0, 292);

Baron

I think the sound of my face-palm just set off the tsunami sirens in Micronesia. (roll)  It now works perfectly: one thousand thanks Mr. Wizard! ;-D

Monsieur OUXX

CW explained everything perfectly.

About the Drawing Surface: You have to imagine that your sprite is like a painting locked behind a protective piece of glass, to avoid anyone painting onto it by mistake.
- When you "get the drawing surface", it's as if you were asking the curator of the museum (the game engine) : "hey could you open the glass case? I need to paint something onto this painting". If it's a dynamic sprite, he will bring the painting to you and allow you to paint onto the surface. He "gives you the surface".
- Then when you're done you tell him: "I'm fine you can close the glass case again, thanks". That's when he "releases the drawing surface". He's taking away the surface from you and everyone so that nobody can draw onto it.
 

SMF spam blocked by CleanTalk