Read an Xml file, create sprites from data

Discussion and feedback on Construct 2

Post » Fri Oct 16, 2015 10:33 pm

@jojoe Not sure I follow "Use a background". Can I place many instances of one small TiledBackground object (say it's 100px X 200px, whatever, small) on the layout, load it's image from a file using NWjs and then this will not change the images for the other existing instances?
B
13
S
4
Posts: 280
Reputation: 1,573

Post » Fri Oct 16, 2015 10:41 pm

System.create the background tiles one at a time in your loop, then load the URL right after you spawn it.

Or give them a instance variable like "Type", so you can choose which tile you load it into with a system.pick by comparison: background.Type="Thumbail" (or whatever)

sorry my english is not so great
B
29
S
8
G
4
Posts: 767
Reputation: 5,985

Post » Fri Oct 16, 2015 10:44 pm

jojoe wrote:or a single sprite and change the frames, the URL will load into the current frame


Ok I think I follow this part. I could create one "cardSprite" that has the size for 60 cards, but the sprite sheet can be completely blank. Then each time I create an instance of cardSprite, increment the frame, then load the image into that frame. I think this will work right?

Wait...

But ultimately the cardSprite will eventually contain every card image in every frame in every instance of each card on the screen. So I may as well just start with a cardSprte that has every card image and then just switch frames based on the Xml data. it's no difference. Am I thinking right?
B
13
S
4
Posts: 280
Reputation: 1,573

Post » Fri Oct 16, 2015 10:49 pm

jojoe wrote:System.create the background tiles one at a time in your loop, then load the URL right after you spawn it.

Or give them a instance variable like "Type", so you can choose which tile you load it into with a system.pick by comparison: background.Type="Thumbail" (or whatever)

sorry my english is not so great


Your English is pretty good :-)

This might be the way to go. I'll look into this.
B
13
S
4
Posts: 280
Reputation: 1,573

Post » Fri Oct 16, 2015 11:02 pm

"But ultimately the cardSprite will eventually contain every card image in every frame in every instance of each card on the screen. So I may as well just start with a cardSprte that has every card image and then just switch frames based on the Xml data. it's no difference. Am I thinking right? " -- @locohost

You got it :)

Yeah get 52 frames in an animation, then cycle the frames and load each frame url.

You can change animations if you want too as you load the images, so you can break it down into suits or whatever.Might be easier to deal with later if the card animation frame matches the number on the card.
B
29
S
8
G
4
Posts: 767
Reputation: 5,985

Post » Fri Oct 16, 2015 11:08 pm

@jojoe Maybe I can just create a TileMap that has one tile for every card face. Then just load the tiles on the screen using a card index number (rather than a file name) that is in the Xml. That would make the Xml a little less human readable but that could work.

Edit: Or maybe not. No way to dynamically create an instance of one tile from the map.
Last edited by locohost on Fri Oct 16, 2015 11:13 pm, edited 1 time in total.
B
13
S
4
Posts: 280
Reputation: 1,573

Post » Fri Oct 16, 2015 11:12 pm

Yeah! Tilemaps are much easier to deal with as far as loading lots of images. That is a great solution!
B
29
S
8
G
4
Posts: 767
Reputation: 5,985

Post » Fri Oct 16, 2015 11:14 pm

jojoe wrote:Yeah! Tilemaps are much easier to deal with as far as loading lots of images. That is a great solution!


No, see my post above. You can't create a tile instance during runtime. Not that I can see.
B
13
S
4
Posts: 280
Reputation: 1,573

Post » Fri Oct 16, 2015 11:27 pm

Oh yeah @locohost, they will have to load the entire sprite sheet. You cant replace just a single tile with a tilemap.

I just buckle down and make a sprite , and load it up with a loop. Sprites seem to be the most versatile as far as positioning and scaling. Every time i try using Backgrounds as my solution, I get boxed in with limitations. If you used backgrounds you would have to load every single time a card was dealt. With a sprite, it can all be pre-loaded into frames. Then the frame just switched to the right card face when it is needed.

There is a function called LoadIcon in the example I gave you. You can see there where i load the frame, and then switch to the next frame. (then it loops)
B
29
S
8
G
4
Posts: 767
Reputation: 5,985

Post » Fri Oct 16, 2015 11:44 pm

So if a card sprite sheet is 60 cards. Each card is say 150px X 300px. I make a blank sheet big enough to hold all the card image frames. I can load the card sprite sheet frames from the .png files using NWjs. We've discussed that. I can System.Create an instance of the card sprite sheet dynamically, then based on the card name from Xml data, choose the correct frame to display. I understand how all of this works so I'm good up to this point.

My question now is about how much RAM will this require? Internally, does a the full sprite sheet get loaded into memory every time a card instance is created?

EDIT...

Ok from the C2 Sprite documentation...

All instances of Sprite objects share their animations. In other words, there is a single set of images comprising the animations which belongs to the object type, and these images are referenced by instances.

This sounds like just one sprite sheet gets loaded and all instances use this one sheet for it's frames. This makes me smile :-)
B
13
S
4
Posts: 280
Reputation: 1,573

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 4 guests