Tap to Select Rect

Hello all, 

I’m trying a make a little tap game on the simulator, what I’m trying to make happen is when one of the rects is tapped say the purple rect the message in the simulator would read Purple Rect Tapped.  In my mind when the purple rect is tapped that rect is now selected to be used in the game. 

What happens when I tap any of the rects all 3 colors are written out; I thought that I could figure out how to change some of the code on the multiple taps info in corona labs, but it’s not working out the way I hoped it would.

Appreciate any help, 

Thank you

Hello,

try this :

local function tapListener(event) local o=event.target print(o.color.." Rect Tapped: "..tostring(o)) return true end local yellowRect=display.newRect(...) yellowRect:setFillColor(...) yellowRect.color="yellow" \<\<-- important yellowRect:addEventListener("tap",tapListener) same with purple and green etc...

yvan

Thanks very much yvan, I appreciate you helping me.

I actually have a couple more questions.  Would you be able to explain what the “o” stands for in the code?  

Also how would I go about changing the background color in my game?  I tried using the code you provided, but it didn’t work. 

Thanks again

The “o” is a local variable to have a shortcut to the element “event.target”. It’s just a name, you can use other term like “myObject” for example.

For the background, you can use

display.setDefault( "background", 0, 0, 1 ) \<--for blue

So in the end you have :

local function tapListener(event) local myChoice=event.target print(myChoice.color.name.." Rect Tapped: "..tostring(myChoice) display.setDefault( "background", unpack(myChoice.color.rgb) ) return true end local greenRect=display.newRect(...) greenRect:setFillColor(0,1,0) greenRect.color={name="green",rgb={0,1,0}}

Perfect, thanks so much for all the help.

Hello,

try this :

local function tapListener(event) local o=event.target print(o.color.." Rect Tapped: "..tostring(o)) return true end local yellowRect=display.newRect(...) yellowRect:setFillColor(...) yellowRect.color="yellow" \<\<-- important yellowRect:addEventListener("tap",tapListener) same with purple and green etc...

yvan

Thanks very much yvan, I appreciate you helping me.

I actually have a couple more questions.  Would you be able to explain what the “o” stands for in the code?  

Also how would I go about changing the background color in my game?  I tried using the code you provided, but it didn’t work. 

Thanks again

The “o” is a local variable to have a shortcut to the element “event.target”. It’s just a name, you can use other term like “myObject” for example.

For the background, you can use

display.setDefault( "background", 0, 0, 1 ) \<--for blue

So in the end you have :

local function tapListener(event) local myChoice=event.target print(myChoice.color.name.." Rect Tapped: "..tostring(myChoice) display.setDefault( "background", unpack(myChoice.color.rgb) ) return true end local greenRect=display.newRect(...) greenRect:setFillColor(0,1,0) greenRect.color={name="green",rgb={0,1,0}}

Perfect, thanks so much for all the help.