Swap image on Tap event

@RedEldorado: there are a few things you can do to solve this problem - as @Appletreeman indicated, you could use the setFillColor method (https://docs.coronalabs.com/api/type/ShapeObject/setFillColor.html) to simply change the coloring of the square. That’s a nice elegant solution, as it keeps the number of display objects you create as low as possible. It’s worth noting that you can use setFillColor on display.newImage objects, too - so if you wanted to use a PNG instead of a RoundedRect for whatever reason, you could still employ that same solution by using a single PNG that is all white for all your squares, and apply the blue/yellow tint as appropriate using setFillColor.

However, I think it’s important that you at least understand the other possible solution, as it will benefit you down the line as you create more complicated touch/tap handler functions. The “event” table that gets passed into a touch/tap handler function contains a bunch of key/value pairs - as you’ve discovered, event.x and event.y contain the X and Y coordinates of the touch event itself. But event.target is a reference to the display object that was touched and/or tapped - so event.target.x and event.target.y will give you the X and Y coordinates of the touched square. I also see that you used a table listener for your tap handler (meaning you get not only the event table passed into your function, but also a “self” variable that references the tapped object), so you could simply use self.x and self.y to get the X and Y coordinates of the tapped square. How you handle this problem is not so important (all these solutions will work equally well), but understanding the event tables getting passed to your handler functions will pay dividends over time.

One other unrelated thing I’ve noticed: you use the variable “square” a whole lot, and don’t always localize it. This could potentially lead to issues down the line where you’re unable to manipulate one of your display objects because you’ve overwritten the variable used to point to it. You’re putting your squares into a table, which is great - and why this isn’t likely to be an actual problem for you in this particular project (at least not yet), but it’s good to try and establish good habits surrounding scoping your variables. Using the same variable name over and over without localizing it every time can lead to unexpected (and potentially difficult to debug) issues later on. Just my two cents of uninvited advice. :wink:

@schroederapps,

Thanks for your detailed response and feedback regarding my original code. Unfortunately, the code is a bit of a hodgepodge and needs to be rebuilt from the ground up.

* Applying a colored tint to a png sounds interesting. I’ll have to check that out.

* Using “self.x and self.y to get the X and Y coordinates of the tapped square” is another great idea.

As per using “square” a lot, and not always localizing them, yeah, I’m still working on getting a grasp on the whole localization and scope conceptualization. Square started out as just a convenient reference and cascaded from there.

I appreciate your feedback!  Thanks

@yosu, thanks for the file (over SQL) recommendation, for speed. Figuring out how to save all the data is going to be another big learning curve. Thanks.

@schroederapps,

Using “self.x and self.y to get the X and Y coordinates of the tapped square” works great, answered my question and solved my problem!

Thanks!

Thank you all for your responses!  I appreciate it.

Tom