evaluating table in corona sdk

hey guys i am stuck a little.  want to  to access the image or id in a table .  when the food collide with the hero the hero would give  a specific point to the food. i found this  stack over flow but it address touch not collision  http://stackoverflow.com/questions/16966784/corona-sdk-lua-how-to-evaluate-which-array-object-was-tapped this exactly what i want but in collsion form. here  

foodTable = {“healthy food/strew”,“healthy food/milk”,“healthy food/keewee”,“healthy food/green”,“healthy food/greaps”,“healthy food/fish”,“healthy food/egg”,“healthy food/carrot”,“healthy food/bluebarrys”,“healthy food/banna”,“healthy food/apple”, “healthy food/Banana01”} 

 foods = {} 

    

function spawnFoods()

 ranNum = math.random(#foodTable)

foods[#foods + 1] = display.newImage( foodTable[ranNum]…".png" )

foods[#foods].x = math.random(-10, 300);

foods[#foods].y = -40;

foods[#foods].id = foodsNum

physics.addBody( foods[#foods], “dynamic”, { friction =1, bounce =0, density=0 } )

transition.to( foods[#foods], {time = math.random(2000, 8000), x = math.random(90, 100) , y = 480, onComplete = nil});

foods[#foods].name = “happy”

foods[#foods].isFixedRotation = true

total_food = 0

  foodtimer= timer.performWithDelay( 1000, spawnFoods, total_food )  

– hero movement on user’s drag

function movePlayer(event)

    if event.phase == “began” then

        moveX = event.x - hero.x;

    elseif event.phase == “moved” then

        hero.x = event.x - moveX;

    end

    if((hero.x - hero.width * 0.5) < 0) then

        hero.x = hero.width * 0.5;

    elseif((hero.x + hero.width * 0.5) > display.contentWidth) then

        hero.x = display.contentWidth - hero.width * 0.5;

    end

end

hero:addEventListener(“touch”, movePlayer);

local function onLocalCollision( self, event )

  if event.other.name == “badfood” then 

print ‘floor’

 display.remove(event.other)

elseif event.other.name == “enemies01” then 

 display.remove(event.other)

elseif event.other.name == “happy” then

display.remove(event.other)

  end

    end

  

  floor01.collision = onLocalCollision

  floor01:addEventListener( “collision”, soldier )

— hero collision  point / calories giver 

local function onLocalCollision( self, event )

  if event.other.name == “badfood” then 

print ‘bad’

 display.remove(event.other)

elseif event.other.name == “happy” then 

display.remove(event.other) 

end

end

  hero.collision = onLocalCollision

  hero:addEventListener( “collision”, soldier )is my code 

hope this is good  enough to answer my question. if any question please reply.  

thanks 

Sean 

PS  the stack overflow  would explain what i want to say better. :slight_smile:   

I think you’re setting up your collision handlers wrong.   Try this:

  floor01.collision = onLocalCollision

  floor01:addEventListener( “collision”, floor01 )

  hero.collision = onLocalCollision

  hero:addEventListener( “collision”, hero )

 

Rob

thanks Rob i will fix that but how do i accomplish making a id for the  images inside the table and when they collide with the hero  the hero would give points depending on what object collided with it  

thanks 

Sean 

ps 

 :)

I would do something like foods[#foods].points = howmanyeverthatfoodisworth

Then in your collision handler you could just grab event.other.points value and use it however you want.  You can add any attributes you want to your object and they are available to you when you need them.  If you want more you could do:   foods[#foods].type = “Bread” then in the collision handler you could do if event.other.type == “Bread” and do things based on that.

Rob

thanks Rob  but i keep  getting error message  is is what i want   i take this from the stack overflow

 "I’m fairly new to the Corona SDK and LUA.I hope someone can help me. I’m stuck now for quite a while on my array and evaluating, based on the tapped object, in a function what object has been passed to the function.

What I try to accomplish? Based on an Array I spawn a car (image), when the user taps this car the car is removed from the screen and the score is updated.

What I would like to accomplish is to set a different score value (100) for the blue car (bluecar.png) and when all other cars are tapped a score of 5." (stack overflow)  function createCar()

carArray = { “carblue.png”, “cargreen.png”, “carred.png”, “caryellow.png” }
car = display.newImage( carArray [math.random (#carArray )] )
car.x = -200
car.y = math.random (0, display.contentHeight)
car.xScale = 0.2
car.yScale = 0.2
car:rotate (90)

car.moves = transition.to (car, {time = 3000, x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } )
car:addEventListener ( “tap”, touchCar )

return car

end

function touchCar(event)
local cartouched = event.target
transition.cancel ( event.target.moves )
audio.play(brake)
cartouched:rotate (370)

if cartouched == carArray[1] then
score = score + 100
else score = score +5
end
scorelabel.text = "Score: "… score
local function removeCar()
display.remove(cartouched)
end
timer.performWithDelay(600, removeCar)

createCar()
end

–(stack overflow) instead of touch i would like collision determine what object get points 

thanks Rob 

from sean  ps 

you  are a real life saver  :slight_smile:

Unless that Stack Overflow post is yours, I wouldn’t even look at it.  Lets focus on your issue.   What is your error message?  Is this from a popup or are you taking the error from your console log (in which case posting the whole stack trace is helpful)?

It the stack overflow post is yours, while it may be related, it seems to be a different problems (cars instead of food), so I’d rather look at them as separate issues.

okay  basically when i insert the code it don’t work / nothing happen. This is what i did in the table i put foods[#foods].type = “Bread”  in the collision i put  if event.type == “Bread” then give what ever point/calories that food has. Let me explain exactly what i am going for i want all the images inside the table to have its own ID. so for example bread right it has different amount of calories  than an egg . So if the hero collide  with egg the player would gain 78 calories and if the player collide with bread the player would gain 58 calories . Out of curiosity would my table look something like this

images ={

  { getFile = “images/bread.png”, types = “bread”   },

  { getFile = “images/bread01.png”, types = “bread01”},

  { getFile = “images/milk.png”, types = “milk”}

}

each image would have its own type that i could access in the  collision  method.

sorry Rob about  posting the stack next time i won’t 

thanks again Rob Big fan

Sean  

:slight_smile:

You can do your table like that, but you have to change your spawn function accordingly:

images ={ &nbsp; { getFile = "images/bread.png", types = "bread" &nbsp; }, &nbsp; { getFile = "images/bread01.png", types = "bread01"}, &nbsp; { getFile = "images/milk.png", types = "milk"} } &nbsp; function spawnFoods() &nbsp;&nbsp;&nbsp;&nbsp; ranNum = math.random(#images) &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods + 1] = display.newImage( images[ranNum].getFile ) &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].x = math.random(-10, 300); &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].y = -40; &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].id = ranNum&nbsp;&nbsp; -- foodNum wasn't identified, I think you want ranNum here. &nbsp;&nbsp;&nbsp;&nbsp; physics.addBody( foods[#foods], "dynamic", { friction =1, bounce =0, density=0 } ) &nbsp;&nbsp;&nbsp;&nbsp; transition.to( foods[#foods], {time = math.random(2000, 8000), x = math.random(90, 100) , y = 480, onComplete = nil}); &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].name = "happy" &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].isFixedRotation = true &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].type = images[ranNum].types end

Hmmm, it ate some of my post…   Anyway, once tell each object the other attributes, (types points etc.)  You access them in your collision function like this:

event.other.type   or  event.other.points

I think the reason you’re having a problem is you said event.type not event.other.type

thanks rob it works but how would i go about setting up the collision

 thanks from  Sean   :slight_smile:

You already had that setup in your top post, with the exception of the couple of things I mentioned.  I would suggest you read the collision detection guide:  http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

thanks rob i look into that your a life saver bro thanks for what you are doing helping out others with problems.  thanks again sean :)    

I think you’re setting up your collision handlers wrong.   Try this:

  floor01.collision = onLocalCollision

  floor01:addEventListener( “collision”, floor01 )

  hero.collision = onLocalCollision

  hero:addEventListener( “collision”, hero )

 

Rob

thanks Rob i will fix that but how do i accomplish making a id for the  images inside the table and when they collide with the hero  the hero would give points depending on what object collided with it  

thanks 

Sean 

ps 

 :)

I would do something like foods[#foods].points = howmanyeverthatfoodisworth

Then in your collision handler you could just grab event.other.points value and use it however you want.  You can add any attributes you want to your object and they are available to you when you need them.  If you want more you could do:   foods[#foods].type = “Bread” then in the collision handler you could do if event.other.type == “Bread” and do things based on that.

Rob

thanks Rob  but i keep  getting error message  is is what i want   i take this from the stack overflow

 "I’m fairly new to the Corona SDK and LUA.I hope someone can help me. I’m stuck now for quite a while on my array and evaluating, based on the tapped object, in a function what object has been passed to the function.

What I try to accomplish? Based on an Array I spawn a car (image), when the user taps this car the car is removed from the screen and the score is updated.

What I would like to accomplish is to set a different score value (100) for the blue car (bluecar.png) and when all other cars are tapped a score of 5." (stack overflow)  function createCar()

carArray = { “carblue.png”, “cargreen.png”, “carred.png”, “caryellow.png” }
car = display.newImage( carArray [math.random (#carArray )] )
car.x = -200
car.y = math.random (0, display.contentHeight)
car.xScale = 0.2
car.yScale = 0.2
car:rotate (90)

car.moves = transition.to (car, {time = 3000, x=display.contentWidth-30 , y=math.random(0,display.contentHeight), onComplete = hitWall } )
car:addEventListener ( “tap”, touchCar )

return car

end

function touchCar(event)
local cartouched = event.target
transition.cancel ( event.target.moves )
audio.play(brake)
cartouched:rotate (370)

if cartouched == carArray[1] then
score = score + 100
else score = score +5
end
scorelabel.text = "Score: "… score
local function removeCar()
display.remove(cartouched)
end
timer.performWithDelay(600, removeCar)

createCar()
end

–(stack overflow) instead of touch i would like collision determine what object get points 

thanks Rob 

from sean  ps 

you  are a real life saver  :slight_smile:

Unless that Stack Overflow post is yours, I wouldn’t even look at it.  Lets focus on your issue.   What is your error message?  Is this from a popup or are you taking the error from your console log (in which case posting the whole stack trace is helpful)?

It the stack overflow post is yours, while it may be related, it seems to be a different problems (cars instead of food), so I’d rather look at them as separate issues.

okay  basically when i insert the code it don’t work / nothing happen. This is what i did in the table i put foods[#foods].type = “Bread”  in the collision i put  if event.type == “Bread” then give what ever point/calories that food has. Let me explain exactly what i am going for i want all the images inside the table to have its own ID. so for example bread right it has different amount of calories  than an egg . So if the hero collide  with egg the player would gain 78 calories and if the player collide with bread the player would gain 58 calories . Out of curiosity would my table look something like this

images ={

  { getFile = “images/bread.png”, types = “bread”   },

  { getFile = “images/bread01.png”, types = “bread01”},

  { getFile = “images/milk.png”, types = “milk”}

}

each image would have its own type that i could access in the  collision  method.

sorry Rob about  posting the stack next time i won’t 

thanks again Rob Big fan

Sean  

:slight_smile:

You can do your table like that, but you have to change your spawn function accordingly:

images ={ &nbsp; { getFile = "images/bread.png", types = "bread" &nbsp; }, &nbsp; { getFile = "images/bread01.png", types = "bread01"}, &nbsp; { getFile = "images/milk.png", types = "milk"} } &nbsp; function spawnFoods() &nbsp;&nbsp;&nbsp;&nbsp; ranNum = math.random(#images) &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods + 1] = display.newImage( images[ranNum].getFile ) &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].x = math.random(-10, 300); &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].y = -40; &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].id = ranNum&nbsp;&nbsp; -- foodNum wasn't identified, I think you want ranNum here. &nbsp;&nbsp;&nbsp;&nbsp; physics.addBody( foods[#foods], "dynamic", { friction =1, bounce =0, density=0 } ) &nbsp;&nbsp;&nbsp;&nbsp; transition.to( foods[#foods], {time = math.random(2000, 8000), x = math.random(90, 100) , y = 480, onComplete = nil}); &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].name = "happy" &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].isFixedRotation = true &nbsp;&nbsp;&nbsp;&nbsp; foods[#foods].type = images[ranNum].types end

Hmmm, it ate some of my post…   Anyway, once tell each object the other attributes, (types points etc.)  You access them in your collision function like this:

event.other.type   or  event.other.points

I think the reason you’re having a problem is you said event.type not event.other.type