Corona SDK - Managing Game State/Objects/Inventory System/Sprite Animation - coronasdk

I am attempting to determine the best way and most efficient way to handle the following tasks in a game written with the Corona SDK. It seems like there are so many ways to do things that it becomes quite confusing, so I am hoping someone here can help!
I am creating an adventure type game that will have an inventory system/puzzles etc. The thought process I've developed so far involves using separate "classes" to handle each specific aspect of the game. Such as InventoryManagement.lua, ObjectManagement.lua, PuzzleManagement.lua etc.
Just a side note - this game really doesn't involve Physics, but I would like to have static images with animations that occurs (Think opening a door or picking up an object):
Here is an example of what I am trying to accomplish:
Say you start a new game and it loads the first scene. I need to setup the player's inventory, the objects in the room, their state, images for these things etc. which I assume could be defaulted on first game load, and loaded subsequently ...
Then the player clicks on a key to pick it up - at this point the key needs to appear in their inventory so now it will be removed from the scene, added to their inventory (Via InventoryManagement?), and the scene will be updated (Via SceneManagement?)...
From now on the key should no longer show in the scene.
Now say they click the key and use it on a door, the door should animate open and remain open from now on.
If the player leaves the room and comes back, the key should not appear.
Now to me it makes sense to load/unload the scene every time you enter/leave the scene, but ... won't this become memory intensive etc. if you do it this way? ... Is there a better way to handle a scene if it has say, 30 objects on screen?
Hopefully that is clear - It is hard to find specific information related to each one of these elements. Everything seems to be related to physics games and I can't seem to find something on how to "Add a key to the scene if, but not if, and if it's been used then animate that door" :(
Thanks!

Had the same problem with unexpected results when jumping between storyboards. One thing I found out was
Always make sure to Runtime:remove and stop the timers you use. One alternative when you want to reset everything is to make a reset.lua file and on enterScene you use storyboard.removeScene or storyboard.purgeScene (If you just want to get rid of all display objects you have rigged up in memory in scene:createScene).
Also check out storyboard.RemoveAll() or purgeAll()

You need to make some stuff in Global space (using _G) to use them between scenes.
To change between scenes, "Storyboard" is very nice.
But mind you, you will need to use some manual memory hacking if you make things too much memory intensive, mainly, you might need to before loading a scene, do several calls of "collectgarbage" after cleaning the last scene, otherwise you will hit a curious problem:
You loaded scene "A".
You switch to scene "B".
The game 'unloads' "A", but the garbage collector don't, then loads "B", the end result is memory use of "A" + "B".
On this adventure game I was making in Corona, it resulted in several crazy crashed until I figured the "collectgarbage" trick.

Related

Creating SKScene, init(fileNamed:) vs init(size:)

If you make a new game template project in Xcode, the default GameViewController will use the following initializer to instantiate the game scene:
let scene = SKScene(fileNamed: "GameScene")
The initializer's use in the template file suggests that this is Apple's recommended way for creating an SKScene. However, I have seen many examples on Stack Overflow that use the init(size:) initializer or simply SKScene().
I am wondering which is the best way to create an instance of SKScene and what are the advantages/disadvantages of each way, as well as various pitfalls to look out for when using one approach versus another.
My reason for asking is that I used init(fileNamed:) to create my game's scene and build level 1. When I later tried to create another instance of the scene for level 2 using init(size:), I ran into problems. Namely, some positional calculations using UIScreen and the scene's frame seem to produce different results if I create the scene using init(size:) instead of init(fileNamed:).
In a more general sense, I would like to hear the opinion of someone who is very familiar with using iOS and SpriteKit about their preferred way of initializing a new SKScene and the pros & cons of using different approaches.
I very strongly recommend not using init(size:) when it comes to creating your scenes that you use as a part of your game. You really should try to keep design separate from structure. This allows for you to change how the layout of your game is, without changing any code, thus reducing the potential for bugs.
With that being said, there is a very common practice tutorials use that looks like this init(size:view.size). This one causes all kinds of trouble with game designers, and destroys the simple handling of multiple devices that SpriteKit offers. People tend to end up in a 0..1 coordinate system, placing divides everywhere when positioning their nodes, and this too leads to bugs because people may end up forgetting a divide somewhere, or using width when they meant height.
SKScenes have 4 scale modes that handle how a scene should look on every device, and it should always be taken into consideration when developing a game with this tool. It really should be the first thing that gets discussed so that people can understand the power behind it.
The only time I would recommend using init(size:) is when you need to create a dynamic scene that cannot be achieved via the sprite kit builder, and when you already have a static size of the window established.

Is there a way to layout my entire level in an .sks file, but only load whats currently in view?

I've seen this concept mentioned around the internet but can't find any specifics on how to do it.
I want to lay out my entire level in an .sks file but load only whats in the view of the player/frame at any given moment. In my game I have platforms that go up and down forever, simple roving enemies, and collectable coins that float in the air while bobbing up and down. Since these actions are using
SKAction.repeatForever
They are continuously going even when not in view of player. The only other option I see is giving every action a "key" and then placing invisible sprites through out the level that when contacted start and stop certain actions. While doable it seems it could get very convoluted very fast.
Is there a more straightforward way to lay out my entire scene, but only load what's currently in view? I'm not sure what to call this concept which is probably the reason I can't find much on it.
Any insight is appreciated!
There is no easy way to do it. My personal method is to load your SKS into an SKScene that is not attached to the root node at all (the SKView) and use SKCameraNode's containedNodeSet to move all nodes from the loaded scene over to the scene that is viewable to the user. You would then need to implement methods to continuously swap between the two scenes.
If your concern is only actions, then you can avoid the swapping and just pause all nodes not in the containedNodeSet and unease the ones that are inside of it.

How to make an object static in lua

I'm making an android game in LUA with the corona SDK and I want to stack boxes onto each other.
I can add the boxes to physics but when the boxes land I want them to become static so they don't move anymore after they have landed and I have no idea how to do that. Can someone please help me? I already tried it with physics.removeBody() after I added them but that didn't work.
I'm sure the solution will be pretty easy but I'm relatively new to the corona SDK.
One way of accomplishing this would be to - after
physics.removeBody()
add it again, this time as static, eg.:
physics.addBody(bodyObj, "static", ...)
Remember this about physics.removeBody() though:
This call cannot be used in a collision event. However, your collision
handler may set a flag or include a time delay via
timer.performWithDelay() so that the action can occur in the next
application cycle or later

iOS app as a staged play

This may not be a unique question, but I don't know how to phrase it because my Google skills are insufficient.
I am writing an app framework and I am spending a lot of time writing a system to store the positions and other properties of UIView elements on the screen.
What I want to know is whether something like this already exists, since even though my system works well, I am concerned about memory usage with a large number of elements.
Basically, I subclassed UIControl and added a "state" property that stores position, alpha, colorscheme data and all forms of transforms that apply to a certain state in the interface. This is akin to actors positioned on a stage. When a scene change happens (a button is pressed, or something) the actors (UIViews) know exactly where to be and how to look from stored data.
This means that with a single button press and NSNotification, I can broadcast a simple integer that identifies the state to be in, and all necessary properties will be animated accordingly.
Am I wasting my time? Does something like this already exist. It does not appear to be included in the tools that Apple provides.

What is the right way to design a point and click game with Lua?(Corona)

I have some ideas,I have already drawn the scenes roughly and put them into corona.
This is how I would think it would work.
1.I have all the major scenes
2.In each major scene ,there are mini scenes,which lead to smaller scenes with puzzles.
3.I create a game.lua module so it would handle the game logics,and keep track of them.
Example:
A.Door is opened,puzzle number one is solved.
B.Send a costume event to the game module.
4.I need a separate module for inventory box so it acts as an overlay over other scenes.
It also interacts with game module.
.
I know you might say,wait till you get started,but Am I on the right Track?
Do you have better suggestions or know any code examples(Corona Sdk,Lua,Moai,GiderosMobile)?
.
Thanks in advance
I am making a game more or less on this style using Corona, there are a beta on google play.
Corona SDK offers a API named Storyboard, that API allow you to code each scene as a lua module, and allows also you to load a scene as a "overlay".
I guess that you would make each of your scenes a full Storyboard scene, and the inventory a "overlay" scene.
The difference between overlay to a normal scene, is that when you load a overlay it triggers a event on the scene that was open instead of unloading that scene (And when the overlay is unloaded, it triggers another event)
So yes, at least if you do it in Corona, you are in the right track!

Resources