Feedback wanted

Hello,

I’m currently working on a iOS game, explaining the development of Wim Crouwel’s New Alphabet through the learn as you play principle. But because it is my first time developing with the Corona sdk and with the programming language lua i’m having some difficulties in the development stage, especially with a certain corner object i’m trying to create. 

So my question is, is there someone out there who is willing to check out my code and give me some feedback and advice on the project. I made a repo on github here

The problem i’m having with the corners is that i don’t know how to “show” and hide them when a certain object is “active”. There is a video showing all the game mechanics here.

The app will be free and i’m not aiming at getting revenue out of this project. So i’m not able to pay for the help unfortunately. But maybe i can help you out in return, i’m a dutch graphic design student, so if you need some graphics for a project… we’ll could do some sort of trade.

Thanks in advance!

Bram

Hi Bram,

Welcome to Corona and to the forums.

I watched the video, and everything looked OK to me.  Can you explain in more detail what’s the issue you’re facing, i.e., what you mean by “the problem I’m having with the corners is that I don’t know how to ‘show’ and hide them when a certain object is ‘active’”?  In the video, the strokes did activate when the adjacent strokes were turned on.  Is that because the video is just a concept demo, but your code doesn’t actually do that yet?

  • Andrew

Hi Andrew,

Thanks for your reply! 

The video i posted is “unfortunately” a concept demo, what i have now is a corner object witch has four triangles in it, when all active they form a square, currently that is al i’m seeing now.

What needs to happen is when a “rectangle” is active (or visible) the corner knows if it needs to be filled or show as a triangle by checking if a neighboring rectangle is active.

What i don’t know is how i can let these objects know that they are neighbors of each other.

Thanks,

Bram

Hi Bram,

Thanks for the clarification, I understand now.

I see in your level2.lua file how you’ve represented the structure of your grid, and so your question is how can you tell whether a corner is next to a block.  There are two ways to do that (1) use the way you’ve represented the structure of your grid, in which case we’ll have to figure out the math that tells us when a corner is a neighbor of a block, or (2) represent the structure of your grid in a different way that makes it easier to figure out when a corner is a neighbor of a block.

For (1), let’s say you have a corner and you want to figure out if it’s adjacent to a block that’s on.  It seems to me like you’ll have to loop through all of your blocks and, for each one, compare whether (a) the corner’s coordinate is 1 different in x or y from the block’s starting coordinate, or (b) the corner’s coordinate is 1 different in x or y from the block’s ending coordinate (which is its starting coordinate plus width-1 and height-1).

For (2), here’s an alternative way to think about representing the grid.  Instead of having “blocks” and “corners” as separate things, you would have a single 2D array of the “cells” in your grid.  Each cell could have some custom properties such as whether it’s touchable, whether it’s currently on, etc.  In addition, you’d have a table that stores, for each row and column, how many cells high the row and column is.  Thus, you can tell when two cells are neighbors simply by comparing their 2D index (if either the x or y is different by 1, they’re neighbors) and you can render the grid by using the cell heights in your row and column tables.

  • Andrew

Hi Andrew,

Thanks for the help! Do you maybe know were i can find some sort of a tutorial on this topic? i think i need to see it and reed it four times to understand how it would work  :stuck_out_tongue:

Bram

Hi Bram,

Unfortunately no, I don’t think there would be a tutorial on something quite as specific as this.  But I’d be happy to try to help clarify – what part are you finding confusing?

  • Andrew

Hi Andrew,

thanks for the support!

i’m trying to implement it in my code (pushed to github), and i have something working:

blockX = event.target.x - (event.target.width/2) + event.target.width cornerX = cX\*gridWidth blockY = event.target.y - (event.target.height/2) cornerY = cY\*gridHeight if ( math.round(blockY) == math.round(cornerY) and math.round(blockX) == math.round(cornerX) ) then fill.alpha = 1 print("susses!") end

This is taking place in a loop that goes trough all the corners when a block is clicked.

problem is that “fill.alpha = 1” fills all the corners and not the specific one corresponding with the x and y pos.

Also the math i’m doing now only triggers the corner right to the clicked block. So what i’m thinking is to add a:

blockLeftX = event.target.x - (event.target.width/2) - gridWidth

that would correspond to a block on the left side. <-- EDIT: added and works, triggers two succeses for the left and right.

what i have now is working for the horizontal blocks but not yet for the vertical blocks. <-- EDIT: i can now detect left, right, top and bottom corners

problem is the filling and only showing the correct triangle

Bram

Hi Bram,

OK great, it sounds like you’re making progress.  So if I understand correctly, you can now get the corner to highlight when adjacent blocks are selected, just not the appropriate ‘triangle’ within the corner?

  • Andrew

Hi Andrew,

Well i can trigger the locations as in printing a response, but i do not have a proper way to highlight the correct triangels or square. What happens now is that i highlight the group in which the triangels are stored (fill.alpha = 1) and thus highlighting all the squares/triangels.

Bram

Hi Bram,

It sounds to me like, rather than setting the alpha of the entire group containing the triangles, you need to do it for just one triangle.  Which triangle to highlight depends on the direction of the adjacency to the neighboring blocks.

  • Andrew

Hi Andrew,

yes indeed, but i do not yet have a way of targeting the block or triangle directly.

For the block i use the “event.target” but i don’t know how to call the block or triangle.

thanks,

Bram

Hi Bram,

I’m not sure exactly what you mean, though I think I have an idea.  Do you mean that you’re able to target (refer to) the display group that is holding the corners (triangle images), but you’re not able to target (refer to) the corners (triangle images) themselves?  In this case, you can just make the triangle images a custom property of the group so that you can refer to them.

For example:

[lua]

– Create the display group

local cornerGroup = display.newGroup

– Create the display objects

local topLeftCorner = display.newImage(…)

local topRightCorner = display.newImage(…)

local bottomLeftCorner = display.newImage(…)

local bottomRightCorner = display.newImage(…)

– Insert the display objects into the display group

cornerGroup:insert(topLeftCorner)

cornerGroup:insert(topRightCorner)

cornerGroup:insert(bottomLeftCorner)

cornerGroup:insert(bottomRightCorner)

– Create references from the display group to the display objects for easier access

cornerGroup.topLeftCorner = topLeftCorner

cornerGroup.topRightCorner = topRightCorner

cornerGroup.bottomLeftCorner = bottomLeftCorner

cornerGroup.bottomRightCorner = bottomRightCorner

[/lua]

  • Andrew

Hi Andrew,

Thanks! But if i would target the group.corner wouldn’t all the corresponding corners react. For example if i would do bottomLeftCorner.Alpha = 1 then all bottom left corners would be visible?

Bram

Hi Bram,

My understanding is that you have a display group for each square on your grid that holds corners, and that within that display group, you have four triangles.  If that’s the case, then no, targeting a particular group’s bottomLeftCorner wouldn’t affect any of the other ones.

  • Andrew

Hi Andrew,

Well now i have a loop looping trough all the x and y positions of the cornerObjects placing them and adding them to a group. But if i would create a group for every cornerObject then i would not be abel to place them with a loop right?

Bram

Hi Bram,

OK, I think I see what you mean.  You mean that, currently, all of the corner objects are part of the same display group.

I don’t see why you couldn’t continue to use a loop though.  Any time you’re doing something repetitive in code, if you’re not using a function or a loop (or both), you’re probably coding it inefficiently.  If you think you can’t use a loop, then you might be misunderstanding how display groups work.

Display groups in Corona are very flexible, and so are Lua tables (display groups are just a special kind of Lua table).  You can insert display objects into a group, or you can insert other display groups and make a display group hierarchy.  In your case, if you wanted, you could have a display group for the entire area, containing two groups – one for blocks and one for corners.  The corners group could in turn contain X display groups, one for each of the X corners you have.  Each of those groups could in turn contain four triangle display objects like in my previous post.

That’s just one way of doing it (and not necessarily the best way either).  The whole purpose of a group is to be able to manipulate all of its contents at the same time (positioning, scale, etc.).  Since it’s hard to imagine a case where you would want to change all of the corners at the same time, I don’t see why you’d need a display group to hold all the corners (or all the blocks, for that matter).  On the other hand, having a group for the entire grid probably still makes sense, in case you want to adjust certain properties quickly (make it fade in/out, scale it, etc.).

  • Andrew

Hi Andrew

Thanks! That would add some logic to the accessing, if i want to implement this in a loop would i then need to make something like:
if i = 1 (or whatever number) then
Add to this display group
if i = 2 then
Add to that display group

Or is there a better way to add each loop to a different group?

Bram

Hi Bram,

Typically, you never want to code something that’s not “scalable”.  Your approach of if i==1 do something, i==2 do something else, etc. would not be scalable if the possible values for i were large – you’d be writing a lot of code that’s effectively doing the same thing.  Instead, you’d want to set up an array of display groups (using a table) and insert into the i’th element of the array.

  • Andrew

Hi Andrew,

Thanks for all the help!

I sort of have it working, i can refer to the corner by using “cornerGroup[4].alpha = 1”

The corner group contains all the corners and corners have 4 “directions” like:

local topLeft = polygonFill( table.listToNamed(tlcPoints,{'x','y'}), isclosed, isperpixel, widthheight, widthheight, {0,0,0} ) corner:insert( topLeft ) local topRight = polygonFill( table.listToNamed(trcPoints,{'x','y'}), isclosed, isperpixel, widthheight, widthheight, {0,0,0} ) corner:insert( topRight ) local bottomLeft = polygonFill( table.listToNamed(blcPoints,{'x','y'}), isclosed, isperpixel, widthheight, widthheight, {0,0,0} ) corner:insert( bottomLeft ) local bottomRight = polygonFill( table.listToNamed(brcPoints,{'x','y'}), isclosed, isperpixel, widthheight, widthheight, {0,0,0} ) corner:insert( bottomRight ) cornerGroup:insert( corner )

What i think is happening when i’m targeting cornerGroup[4] is that i refer to the 4th corner in the cornerGroup. But what i don’t understand is why i can’t refer to the direction e.g bottomRight by doing “cornerGroup[4].bottomLeft” ?

When i try i get the message “attempt to index field ‘bottomLeft’ (a nil value)”

Hi Bram,

The reason cornerGroup[4].bottomLeft doesn’t work (right now) is because you haven’t set up the reference that way (yet).  But we can fix that.

Let’s take this portion of your code as an example:

[lua]

local bottomLeft = polygonFill( table.listToNamed(blcPoints,{‘x’,‘y’}), isclosed, isperpixel, widthheight, widthheight, {0,0,0} )

corner:insert( bottomLeft )

[/lua]

This adds a filled polygon to the corner.  However, ‘bottomLeft’ is just a local variable used to refer to the polygon object.  The corner doesn’t ‘use’ that name other than to get a reference to the polygon object.  That’s why writing corner.bottomLeft (or cornerGroup[4].bottomLeft) won’t work.

To make it work, try this:

[lua]

local bottomLeft = polygonFill( table.listToNamed(blcPoints,{‘x’,‘y’}), isclosed, isperpixel, widthheight, widthheight, {0,0,0} )

corner:insert( bottomLeft )

corner.bottomLeft = bottomLeft

[/lua]

Now, not only are we adding bottomLeft to the group, as before, we’re also adding a custom property of the group to refer directly back to bottomLeft.  Now you should be able to write corner.bottomLeft (or cornerGroup[4].bottomLeft) to target that polygon.

Hope this helps.

  • Andrew