Choosing random objects from a group and Displaying

I have a noobish question as I’m just getting started with Corona and Lua. I’m looking to have a set of images (lets say a pool of 10 shapes) that are presented to the user in a “level/round” each “level/round” with consecutively more and more being displayed yet not repeating/duplicating. So round one the user would see 3 random shapes(but not repeated), round two: 4 random shapes(but not repeated), and so on.

Could someone point me in a direction? Is the best way to handle that many independent objects, in a group? With object references? [import]uid: 10361 topic_id: 4048 reply_id: 304048[/import]

have a master array of all shapes… (allShapes) A,B,C,D,E,F,G,H,I,J etc

then per level extract a secondary array (levelShapes) eg A,B,C,D,E

then use that as a pool for your shapes for that round. pick a random number from the length of levelShapes eg 3 = C, then remove the third entry from levelShapes so levelShapes is then A,B,D,E

next time you repeat that process your length is now 4, but you only have A,B,D,E to choose from so you won’t get C again

etc

[import]uid: 6645 topic_id: 4048 reply_id: 12387[/import]

I understand the logic of what I need to do, but I don’t know the lua language very well yet. I’ve been going through the resources and docs and videos but none of them are really helpful for a “Jumping off” point that I need for my current project. [import]uid: 10361 topic_id: 4048 reply_id: 12388[/import]

what language are you used to? [import]uid: 6645 topic_id: 4048 reply_id: 12389[/import]

this’ll help:
http://lua-users.org/wiki/TablesTutorial

and to numerically loop through a table used as an array

[lua]for i=1, #myTable, 1 do
print(i … ": " … myTable[i])
end[/lua]

(# is the “length” operator, note Lua is 1-indexed not 0-indexed ie myTable[0] doesn’t exist, it is myTable[1] for the first element) [import]uid: 6645 topic_id: 4048 reply_id: 12390[/import]

Thanks for the tips JMP. I have some rudimentary javascript knowledge.

Unfortunately my “first projects” tend to be a bit complex as far as the complexity of the code required (by noob standards) :slight_smile:

I have an app already created/selling based out of GameSalad and my goal is to learn Corona/Lua by porting it over and giving myself a springboard to make different iterations of the game since Corona offers much much more. [import]uid: 10361 topic_id: 4048 reply_id: 12393[/import]

some starter points
http://phrogz.net/lua/LearningLua_FromJS.html

other than that, read the API docs, read the wiki’s etc
http://lua-users.org/wiki/

I managed to port my match3 physics prototype from Flash in a few days after picking up corona and starting to learn Lua. maybe it’s even worth prototyping your required functions in JavaScript and then working out how to port them.

this is handy if you need to test some basic functions and aren’t near your corona development machine: http://www.lua.org/demo.html
[import]uid: 6645 topic_id: 4048 reply_id: 12397[/import]

Thanks a bunch JMP!

Out of curiosity… Is it possible to pass a local variable as a physics.addBody parameter?
[import]uid: 10361 topic_id: 4048 reply_id: 12398[/import]

umm yes except i’m not sure what you mean… like this?

[lua]local bodydef = {
type = “dynamic”,
def = {density=1.5, friction=2.5, bounce=3.0}
}

physics.addBody(ball, bodydef.type, bodydef.def)[/lua]
[import]uid: 6645 topic_id: 4048 reply_id: 12402[/import]

Hmm. that’s not what I was expecting, but I guess that would probably do the trick. I was just thinking that instead of going through all the “shapes” and changing their physics structures you could do it on a more global scope. Is that how you would do that?

I’m curious what the type=“dynamic” does. [import]uid: 10361 topic_id: 4048 reply_id: 12413[/import]

i may have misunderstood what you’re trying to do, i was just passing the defs in as a variable.

could you give an example of what you mean?

the 3 body types are “dynamic”, “kinematic” and “static”…dynamic is the default if you don’t supply it i believe.

the API docs aren’t *that* big…you should read them :wink:
http://developer.anscamobile.com/content/game-edition-physics-bodies
[import]uid: 6645 topic_id: 4048 reply_id: 12441[/import]

Thanks, I have been going through the API docs. I must have missed the “Type” part. But my question earlier was specifically referring to the parameters of defining characteristics(I may have phrased it wrong when I was describing what I needed):

{density=1.5, friction=2.5, bounce=3.0}  

which you definitely accomplished what I was looking for. I just wasn’t expecting it to look like that. Like I said, I don’t have a very big coding background. But this stuff is pretty easy to pick up.

Thanks for helping.
[import]uid: 10361 topic_id: 4048 reply_id: 12452[/import]

it’s the same in Actionscript

[as]
var myObject:Object = {Param1:“Hi!”, Param2:76}; //This creates an object with two variables.
[/as] [import]uid: 6645 topic_id: 4048 reply_id: 12474[/import]

Well, I’m finally to the point where I really need to nail this code down. Back to the starting question.

I’m creating a scenario where I’ll have about 40 different colored shapes(10shapes at 4 colors each) available.

when the round starts I need to select ten from that table. I want it to be unique group of 10 to each round. Then from the 10 I need to pull one or two to be the “matching shape” or “correct answer” that is to say, the shapes that the user will have to find in the crowd of 10.

Anyways, I have some tables set up then I thought I might be doing it wrong because Carlos sent me this idea for a jumpstart. However I can’t seem to get it even close to functioning, let alone in the direction of the complex scenario that I need…

any pointers in addition to what was stated earlier in the thread? I only ask because Carlos thought there was an easier way.

Thanks for any help!

This was his idea on how to handle which shapes were used and whatnot: Thoughts?

local foo = new group --<is this supposed to be display.newgroup i assuming so><br>local shape = {};<br><br>shape.type = display.newImage("bluediamond.png");<br>shape.name = "bulediamond"<br>shape.used = 0;<br><br>foo.insert(shape); --this doesn't work and I'm guessing its the shape.name that isn't working<br><br>shape = {};<br>shape.type = display.newImage("redDaimond");<br>shape.name = "reddiamond"<br>shape.used = 0;<br><br>foo.insert(shape);<br><br>- use a random number between 1 and the number of elments<br>- shape = foo[random.number];<br>foo[randommumber].used = 1;<br><br> [import]uid: 10361 topic_id: 4048 reply_id: 28346[/import]

Trying to do this my own way. Floundering… Any tips would be greatly appreciated. My approach might be laughable since I don’t really grasp tables well… YET :wink:

btw, I’m seeding the random number at the start of my code. and I’m simplifying this a little for the sake of learning, that’s why I only have it handling 10 total.

[code]
local currentShapes = {}
local allShapes = {}

allShapes[1]= “circle”
allShapes[2]= “diamond”
allShapes[3]= “heart”
allShapes[4]= “hex”
allShapes[5]= “oct”
allShapes[6]= “oval”
allShapes[7]= “rectangle”
allShapes[8]= “square”
allShapes[9]= “star”
allShapes[10]= “triangle”
local shapesChosen ={}

local function shapeChooser()
local function getShapeNumbers()

for i=1,numThisRound do
shapesChosen[i]=math.random(10) – random number between 1 and 10
print("i = "…shapesChosen[i])

end
end
getShapeNumbers()
for i,v in ipairs(shapesChosen) do print(i,v) end
end

local function spawnRoundShapes()

for i,v in ipairs(shapesChosen) do
currentShapes[i]= allShapes[v]

display.newImageRect(“b_”…currentShapes[i]…".png", 50, 50)

physics.addBody(currentShapes[i], “dynamic”, {density = 0, friction = 0, bounce = 0, radius = 20})
wrapShape(currentShapes[i])
shapeMovement(currentShapes[i])
for i,v in ipairs(currentShapes) do print(i,v) end

end

end
[/code] [import]uid: 10361 topic_id: 4048 reply_id: 28860[/import]