Adventure game help

Hi to community. I’m working on adventure game and just can’t get the code right. I need that when i touch an object on screen it will dissappear and placed in inventory. And when it’s in there, i need to touch on it and be able to use it on another object. Corona is pretty interesting, but so hard for noobs like me :frowning: [import]uid: 16142 topic_id: 5119 reply_id: 305119[/import]

Hi darkconsoles

the code below should remove the object as for adding the inventory that all depends on how you have coded it.

[lua]function remove(event)
local t = event.target
if event.phase == “ended” then
t:removeSelf()
end
end

myObject:addEventListener( “touch”, remove )[/lua] [import]uid: 12378 topic_id: 5119 reply_id: 16937[/import]

If you want people to be able to stop the game and resume where they left off you’re going to need a way to save info like the inventory (as well as other things). While you could read/write plain text files I’m a big fan of using databases for data storage.

See this page:
http://developer.anscamobile.com/content/data-storage

Try the sample app there – actually build it to your device and try it. It’s a pretty simple example.

Once you have that working you can start playing around with some functions such as:

addToInventory(obj)
Pass in the object you’re wanting to add to the inventory table.

getInventory()
Retrieve a list (table) of objects that are currently in the inventory table. You can use that list to display the inventory items.

removeFromInventory(obj)
Get the specific object out of the inventory table. Delete it from the inventory table and pass back the object.

That would be the core of your inventory system.

When you create a new object on the screen you can specify whether it can be added to the inventory with a flag such as canPickUp:

trolldoll.canPickUp = true

Then in your touch event for that object you’ll check to see whether it can be added to the inventory:

function tappedItem(event) if event.phase == "began" then local t = event.target if t.canPickUp then addToInventory(t) t.isVisible = false -- or maybe you removeSelf() here end end end

That’s untested code and I probably have an error in every line, so treat it as pseudo-code that will just point you in the right direction.

Hopefully this is helpful to you.

Jay

[import]uid: 9440 topic_id: 5119 reply_id: 16985[/import]

Thanks, gotta try it. But how can i use an item with another item in inventory and get another item from this (example: using hammer on vase gets me something else, and destroys vase and hammer) [import]uid: 16142 topic_id: 5119 reply_id: 17345[/import]

It kind of depends on how you code it, there is no one way to do it.

For me, I’d probably give my objects some properties, such as…

hammer.worksWith = vase hammer.worksWithEvent = hitVase hammer.worksWithText = "You swing the hammer and smash the vase to pieces." hammer.doesntWorkWithText = "You can't just willy-nilly smack things with a hammer. Stop it."

If you try to use the hammer with another object your engine will check and see if the hammer .worksWith that object. If not, it shows the .doesntWorkWithText to the user. If it does work with that object, it shows the .worksWithText to the user and then fires off the .worksWithEvent called hitVase.

What is an event called hitVase?

Events are things that can happen during a game – read more about them in this article I wrote:
http://gamedevnation.com/game-development/agm-events/

In a nutshell, when you hit the vase with the hammer your event will “turn off” the hammer and vase and turn on a different item. What’s interesting is that in the article I give an example of breaking a vase and getting something else, so that must be classic adventure game fodder. :wink:

Hope that helps.

Jay

PS - In the code above where it says hammer.worksWith = vase I wouldn’t actually link it to the vase object like that, I’d use the database ID of the vase instead, so it might look like, hammer.worksWith = 37 – then when you use the hammer on something you just see whether that object has the same ID as in the .worksWith property of the hammer.

[import]uid: 9440 topic_id: 5119 reply_id: 17363[/import]