I’ve been working with Corona for only a few days, so I’m still really new to this. One thing I’ve been struggling with is the apparent limitations on custom images. I can’t find a way, for example, to create a new blank image or sprite and “stamp” smaller images onto it. I’ve done quite a bit of searching around and found tips on creating tiled backgrounds, but that seemed to entail creating a separate sprite for every image I want to draw, and that seems highly inefficient. I just want one image that I can scroll around in the background. Any tips? [import]uid: 77723 topic_id: 13182 reply_id: 313182[/import]
Have you tried some of the 3rd party tools that integrate with Corona?
Zwoptex is a popular tool for creating sprite sheets. There are couple of others related:
http://www.anscamobile.com/corona/tools/
[import]uid: 26 topic_id: 13182 reply_id: 48349[/import]
I’ve worked with sprite sheets, and they are great for creating a single animated sprite. But creating 500+ sprites, some potentially animated, to paint my background seems really inefficient. I’ve played with Lime a bit, but it seems rather limited and inefficient when working with massive maps. It wants to load an entire 1000x1000 map at once when all I can show on the screen at one time is a section 35 tiles wide by 16 high.
I was hoping that all this frustration was just due to my lack of experience, but I suspect I’ve already bumped into a couple of limitations.
I’ve looked into Cocos2d, but I hate objective c.
[import]uid: 77723 topic_id: 13182 reply_id: 48356[/import]
Oh, sorry I misunderstood. If you are talking about tile mapping, it’s certainly something you can do. You’d just have to code the logic yourself to handle what image to render and what to remove
[import]uid: 26 topic_id: 13182 reply_id: 48359[/import]
@walter Sounds like he’s asking about blitting a sprite to a display buffer. Ideally that’s already happening under the hood. But it’d be neat to duplicate a display object hierarchy and then you can write custom code for scene management. [import]uid: 27183 topic_id: 13182 reply_id: 48362[/import]
If you create a new display group and insert your sprites into it, you can scroll the entire group as a single entity. You can do parallax scrolling by putting sprites in several different groups and scrolling the groups at different rates.
[import]uid: 9422 topic_id: 13182 reply_id: 48365[/import]
Yes, but I still need to create 500+ sprites in order to add them to the display group, correct? What I was hoping for is a way to create only a handful of larger sprites or images, paint my tiles onto them, and them scroll them around. But maybe I’m over-thinking. Should I not be concerned about a large number of sprites? I have no idea how they’re managed under the hood. [import]uid: 77723 topic_id: 13182 reply_id: 48368[/import]
Hi ellsworthcody,
Can you give a more detailed explanation of what your intention is? For example, do you want to “paint” tiles onto a “game board” or something like that?
As noted above, you can create a display group, insert any number of objects into it (within device memory limitations of course), and manipulate the entire group as one… move it, scale it, rotate it, etc. etc. This is a great feature of Corona!
If you intend to have 500+ “tiles”, you should probably arrange these into a giant spritesheet. Although many users think sprites are only useful for animations (and obviously they are), you can also use spritesheets to group many smaller images (“tiles”) into a large spritesheet (“tilemap”). Then you can pull these images out by positional reference and use them as needed… no actual animation required. If you’ve used tilemaps in other development platforms, it’s the same basic idea.
[import]uid: 9747 topic_id: 13182 reply_id: 48380[/import]
Specifically, I need to use a large map, limited only by storage space. Let’s say it’s 200 tiles wide by 200 tiles high. Each tile is 32x32 pixels in size. The tile data is stored as a series of numeric values, indicating a particular tile on a sprite sheet. What would be the best approach to scrolling this map around without having to create a bazillion sprite objects? If I create just enough sprites to fill the screen, I end up with nearly 500 of them. That seems like it would be slow and require a lot of memory, but maybe I’m wrong?
You mentioned using sprite sheets. I do have a sprite sheet that contains all the possible tiles that I use on my map, but I believe you still have to create individual sprites from that sprite sheet, one for each tile on the map, which leaves me back at square 1, having to create a great many sprite objects.
You also mentioned creating a display group and adding my sprites to the group. Since I’m still creating a lot of sprite objects, I don’t see how that’s going to be any more efficient, memory or performance wise. But like I mentioned before, I don’t know how Corona is handling these objects under the hood, so maybe it is more efficient. I’d like to know what the answer is, though. 
So, to recap, I have a 200x200 tiled map that I need to scroll around in the most efficient way possible. Are there any tutorials or examples out there? [import]uid: 77723 topic_id: 13182 reply_id: 48385[/import]
If you are reusing the same sprite texture repeatedly for many sprites I believe Corona is smart enough to use only 1 sprite’s worth of texture memory.
However, there’s still overhead involved in rendering objects to screen. In my experience with Corona there IS a performance hit for large numbers of sprites simply placed into a scene. And by large number I mean like in the 200-300 range. The performance issues only really show up on actual devices, not in the simulator.
The solution that worked for me was to create a very simple culling routine that, on every 10th frame, iterates through all the sprites in each display group and changes the alpha property for each sprite to 0 or 1 depending on if it is within range of the actual display window (plus a small buffer zone around the window). Using that routine boosted my fps on devices about 30% vs. not doing it, which surprised me since I figured the overhead for doing it would be too high. I guess lua is fast enough!
And I don’t know what Corona is doing (or not doing) under the hood that would cause such a performance hit. It seems like Corona is spending precious time painting sprites into the display no matter how far from the actual viewport they are. It’s something Corona’s renderer could(should?) be handling already, since visibility culling is very simple to achieve with clear performance benefits on devices. Looking for some low hanging fruit, Ansca?
For your purposes, though, I’m not sure that kind of culling would do much good. You’re talking about 500 sprites on screen at once with 40,000 sprites in the world. Might work fine on a desktop pc, but I’d guess you’re going to see framerates of 1-2 fps on an actual device. I’m sure there are more sophisticated culling routines to handle maps of that size, but you’d probably have to roll your own and experiment to find something that works. I’m curious to know what Lime does about this issue since it seems to be designed to support large tile maps.
[import]uid: 9422 topic_id: 13182 reply_id: 48394[/import]
@XenonBL,
Yes, there’s definitely a performance hit for offscreen objects, but I don’t know if Corona is trying to display them visually or not. I would hope that the core Corona engine knows better than to “re-paint” offscreen objects every game cycle, in regards to the OpenGL renderer.
A better guess might be that the engine is still “tracking” them. If they’re part of a display group, and you move the group, Corona has to look at every object in that group and recalculate the X and Y positions of each. I’m not sure how much Ansca could do about this; if you imagine a “database” of display objects, it has to look at each entry when the group is moved, because if some offscreen object is moved as part of a group, and later the program calls for the X/Y coordinates of that object, it must know that it was moved…
This problem/issue probably isn’t unique to Corona. Every engine must, in some way, determine how to manage display objects that aren’t visible. You have achieved this with your method, and I have achieved it with a different method in my game. I’m actually pleased that Corona does track offscreen objects, because with my method I use an object (a “world tracker”) and its X/Y coordinates to determine where new objects get placed into the world, in relation to the tracker. If Corona stopped tracking the coordinates of that tracker object when it drifted offscreen, my solution would be unworkable.
I don’t know how Lime is approaching the culling dilemma. If I ever decide to build a game with a huge tilemap, my current theory is to create a “database” of tiles with row/column coordinates. When the map moves a certain distance horizontally or vertically (perhaps the pixel width/height of a tile) it calls a routine to see if an entire row/column is now offscreen. If so, it deletes all those tiles. And since a new row/column will be visible on the other side of the screen, it loops through that set and places those tiles into the world. This is completely a theory at this point and I haven’t tried to implement it. 
You should probably check out “Lime” if you’re looking for a more comprehensive tilemap solution. It’s still in the beta stage, and there are performance issues for giant tilemaps (the author and Ansca are looking for a solution to this). But in terms of creating and managing tilemaps, this is probably the best current solution for Corona SDK.
http://developer.anscamobile.com/forums/lime
[import]uid: 9747 topic_id: 13182 reply_id: 48404[/import]
@IgnisDesign
Yeah, at first I assumed Corona was smart enough not to paint objects that are offscreen. Unfortunately I think that is exactly what it does by default. I’m pretty sure all my culling routine does, by setting alpha = 0 for off screen sprites, is skip over the OpenGL painting step for those display objects. Given the huge performance benefits I see I’m surprised that Corona doesn’t already do this.
But I still have the overhead of managing the positions, physics, etc. of those off screen objects, so yes, Corona is still “tracking” them. That’s why I don’t think what I’m doing would help elsworthcody’s situation since he’s trying to manage 40,000 objects. Trying to sort 40,000 of anything, let alone do something meaningful to them, 30-60 times a second is going to kill a mobile device. (Maybe not the iPad2, which seems to rival my desktop Mac in performance.).
There are way more sophisticated solutions to optimize sorting and rendering objects, , such as BSP trees, and I’m sure there are methods that could handle 40,000 objects on a phone, but that seems more like a job for a seasoned graphics programmer at Ansca, not a programming newbie like me futzing around with lua 
[import]uid: 9422 topic_id: 13182 reply_id: 48422[/import]
Thanks for all the input, guys! It’s much appreciated. What I think I will try is adding just enough sprites to fill the screen, and then updating their currentFrame as the map scrolls. Hopfully a real phone can handle that. If not, I guess I’ll be trying to tackle Cocos2d and objective C (bleh).
Thanks, again! [import]uid: 77723 topic_id: 13182 reply_id: 48446[/import]
Glad we could help!
A real phone… even an old iPod Touch 2nd-gen… can *definitely* handle 1 screen full of sprites in a Corona project, assuming you program it with sensible, clean Lua/Corona principles in mind (just like you’d have to do with any language, even Obj-C). Maybe Cocos2D has some method already built in for culling and un-culling a large tilemap. If so, Ansca would do well to follow their lead. 
[import]uid: 9747 topic_id: 13182 reply_id: 48450[/import]