how to use table elements - lua

I'm displaying titles of movies as letter images e.g. A separate image for each letter. Each letter can then be dragged in a space/container. this is my code for displaying the container
posX = {}
posY = 124
px = 10
containers = {}
for i = 1, #letters do
if(letters[i]==" ") then
px = px + 10
-- print(posX[i])
-- table.remove(posX, posX[i])
else
posX[i] = px
containers[i] = display.newImage( "Round1_blue_tileEnlarged 40x40.png", posX[i],posY )
px = px + 40
end
end
As you can see I am checking for a space e.g if batman begins was the title, I have no problems if the title is a single word, but adding the space is adding another element to my array that is causing an error when im placing an objecet in my containers. You can see in the 'if' im just adding a space but I dont want this to be an element of my table posX

I am not sure I understand your question well but if I do here is your problem: you are using i as the index in posX but i is incremented by the for loop even for spaces. That results in holes in the posX and containers tables.
You can fix that in several ways, here is a trivial one:
posX = {}
posY = 124
px = 10
containers = {}
local j = 1
for i = 1,#letters do
if(letters[i]==" ") then
px = px + 10
else
posX[j] = px
containers[j] = display.newImage( "Round1_blue_tileEnlarged 40x40.png", posX[j],posY )
px = px + 40
j = j + 1
end
end
You could also use #posX instead of j.

Related

How do I make it look like the picture in Lua

This is my code
local level = 5
for i = 1, level do
local text = ""
for j = 1, i do
text = text..""
end
for j = 1, level-i, 1 do
text = text.." "
end
for j = 1+level, level+(level-i) do
text = text.." "
end
for j = 1, level + i-level do
text = text..""
end
print(text)
end
I want the result to be similar to the one in the picture.
Here is what your code looks like with proper formatting.
local level = 5
for i = 1, level do
local text = ""
for j = 1, i do
text = text..""
end
for j = 1, level-i, 1 do
text = text.." "
end
for j = 1+level, level+(level-i) do
text = text.." "
end
for j = 1, level + i-level do
text = text..""
end
print(text)
end
Your current code prints... well... an empty string. You haven't yet added the characters it's to display to be on par with the image.
The amount of characters per row is 9. So you ideally need 9 characters per row. You will also be incrementing the number once per row. The amount of characters per row also increases by 2; one on each side.
We can use the string.rep(string, number) function to duplicate a 'string' 'number' times. You can feed in your current level into that so it generates 1 2 or 3 depending on the line the number of times. Then you have whitespace to worry about. You can use string.rep again along with a bit of distance math to calculate the amount of whitespace you need from what you take up. Then finally throw everything together concatenated trailing with the first string and print.
local levels = 5
local columns = 9
for i=1, levels do
local str = string.rep(i, i)
local padding = columns - (#str * 2) + 1
print(str .. string.rep(" ", padding) .. str)
end

How to arrange picture in lua like a grid?

I'm learning lua and I want to arrange my bubble picture with some specific x and y coordinates, here's my code so far, the value of my j and i is only incrementing by 1 instead of the +29, I know I'm lacking some knowledge so any help will be appreciated
local background = display.newImageRect("blueBackground.png",642, 1040)
background.x = display.contentCenterX
background.y = display.contentCenterY
local x = 15
local y=15
for i=15,25 do
for j=15, 25 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = i
bubble.y = j
j = j + 29
print("j",j)
end
i = i + 29
print("i",i)
end
This should helps you.
From Lua documentation
The for statement has two variants: the numeric for and the
generic for.
A numeric for has the following syntax:
for var=exp1,exp2,exp3 do
something
end
That loop will execute something for each value of var from exp1
to exp2, using exp3 as the step to increment var. This third
expression is optional; when absent, Lua assumes one as the step
value. As typical examples of such loops, we have
for i=1,f(x) do print(i) end
for i=10,1,-1 do print(i) end
Use
for i=15, 29*10+15, 29 do
for j=15, 29*10+15, 29 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = i
bubble.y = j
print("j",j)
end
print("i",i)
end
or
for i=0, 10 do
for j=0, 10 do
local bubble = display.newImageRect("bubble.png", 23,23)
bubble.x = 15 + i * 29
bubble.y = 15 + j * 29
...

Object is positioning through joints

I didn't know how to best describe this problem in the title but Ill show you pictures to illustrate what I mean.
(1) Image 1 shows the problem I am having
(2) Image 2 shows what I am trying to achieve.
-- The problem --
As you can see i am trying to make the 3 blocks align with the exception of the spike, which extrudes out. When I set this up in corona it basically makes the images align by height.
Here is my spawn function:
function createBlock(event)
b = display.newImageRect("images/Spike.png", 37,80)
b.x = display.contentWidth + 100
b.y = math.random(2) == 1 and display.contentCenterY -75 or display.contentCenterY +40
b.rotation = math.random(2) == 1 and 0 or 180
b.name = 'block'
physics.addBody( b, "static", physicsData:get("Spike"))
blocks:insert(b)
end
EDIT:
function check( event )
if b.rotation == 180 then
b.y = math.random(2) == 1 and display.contentCenterY - 80 or display.contentCenterY + 30
end
end
If the height of the object is H and the height of the spike is S (so the height of the 3 blocks is H - S), and you position them currently at Y, with Y increasing towards bottom of screen, and the object's origin is at the bottom of bottom-most block (opposite spike), and their default orientation is spike up, then:
position the blocks that have spike up at Y + H; the tip of their spike will be at Y, and the bottom of the spike at Y + S.
position the blocks that have spike down at Y + S, then rotate 180 deg. The top-most edge of the blocks will be at Y+S, which is what you want.
If any of the conditions in the first paragraph are different, you will have to make corresponding adjustments, but hopefully this shows how to figure it out.
Update:
Probably a better way is to shift the anchor point so it is in the center of the middle square. By default anchor is at 0.5, thus placing it between the 2nd and 3rd squares (assuming the square furthest from spike is "the first square"). Since your Block consists of 4 cells (3 squares and one spike), you could do this:
function createBlock(event)
local H = 80
local b = display.newImageRect("images/Spike.png", 37, H)
b.x = display.contentWidth + 100 -- NOTE: weird, doesn't this put the obj off screen?
local cellH = H/4 -- assume all four cells in a Block have this height
b.anchorY = 0.5 - 0.25/2 -- each cell is 0.25 of height, need half that, away from spike
b.rotation = math.random(2) == 1 and 0 or 180 -- will rotate around the new anchorY
local posY = display.contentCenterY - 75
b.y = math.random(2) == 1 and posY or posY + 115
b.name = 'block'
physics.addBody( b, "static", physicsData:get("Spike"))
blocks:insert(b)
end
Note that b.anchorY might have to be b.anchorY = 0.5 + 0.25/2 depending on how you have created your image, but it should be either + or - 1/8.

Lua Separation Steering algorithm groups overlapping rooms into one corner

I'm trying to implement a dungeon generation algorithm (presented here and demo-ed here ) that involves generating a random number of cells that overlap each other. The cells then are pushed apart/separated and then connected. Now, the original poster/author described that he is using a Separation Steering Algorithm in order to uniformly distribute the cells over an area. I haven't had much experience with flocking algorithm and/or separation steering behavior, thus I turned to google for an explanation (and found this ). My implementation (based on the article last mentioned) is as follows:
function pdg:_computeSeparation(_agent)
local neighbours = 0
local rtWidth = #self._rooms
local v =
{
x = self._rooms[_agent].startX,
y = self._rooms[_agent].startY,
--velocity = 1,
}
for i = 1, rtWidth do
if _agent ~= i then
local distance = math.dist(self._rooms[_agent].startX,
self._rooms[_agent].startY,
self._rooms[i].startX,
self._rooms[i].startY)
if distance < 12 then
--print("Separating agent: ".._agent.." from agent: "..i.."")
v.x = (v.x + self._rooms[_agent].startX - self._rooms[i].startX) * distance
v.y = (v.y + self._rooms[_agent].startY - self._rooms[i].startY) * distance
neighbours = neighbours + 1
end
end
end
if neighbours == 0 then
return v
else
v.x = v.x / neighbours
v.y = v.y / neighbours
v.x = v.x * -1
v.y = v.y * -1
pdg:_normalize(v, 1)
return v
end
end
self._rooms is a table that contains the original X and Y position of the Room in the grid, along with it's width and height (endX, endY).
The problem is that, instead of tiddly arranging the cells on the grid, it takes the overlapping cells and moves them into an area that goes from 1,1 to distance+2, distance+2 (as seen in my video [youtube])
I'm trying to understand why this is happening.
In case it's needed, here I parse the grid table, separate and fill the cells after the separation:
function pdg:separate( )
if #self._rooms > 0 then
--print("NR ROOMS: "..#self._rooms.."")
-- reset the map to empty
for x = 1, self._pdgMapWidth do
for y = 1, self._pdgMapHeight do
self._pdgMap[x][y] = 4
end
end
-- now, we separate the rooms
local numRooms = #self._rooms
for i = 1, numRooms do
local v = pdg:_computeSeparation(i)
--we adjust the x and y positions of the items in the room table
self._rooms[i].startX = v.x
self._rooms[i].startY = v.y
--self._rooms[i].endX = v.x + self._rooms[i].endX
--self._rooms[i].endY = v.y + self._rooms[i].endY
end
-- we render them again
for i = 1, numRooms do
local px = math.abs( math.floor(self._rooms[i].startX) )
local py = math.abs( math.floor(self._rooms[i].startY) )
for k = self.rectMinWidth, self._rooms[i].endX do
for v = self.rectMinHeight, self._rooms[i].endY do
print("PX IS AT: "..px.." and k is: "..k.." and their sum is: "..px+k.."")
print("PY IS AT: "..py.." and v is: "..v.." and their sum is: "..py+v.."")
if k == self.rectMinWidth or
v == self.rectMinHeight or
k == self._rooms[i].endX or
v == self._rooms[i].endY then
self._pdgMap[px+k][py+v] = 1
else
self._pdgMap[px+k][py+v] = 2
end
end
end
end
end
I have implemented this generation algorithm as well, and I came across more or less the same issue. All of my rectangles ended up in the topleft corner.
My problem was that I was normalizing velocity vectors with zero length. If you normalize those, you divide by zero, resulting in NaN.
You can fix this by simply performing a check whether your velocity's length is zero before using it in any further calculations.
I hope this helps!
Uhm I know it's an old question, but I noticed something and maybe it can be useful to somebody, so...
I think there's a problem here:
v.x = (v.x + self._rooms[_agent].startX - self._rooms[i].startX) * distance
v.y = (v.y + self._rooms[_agent].startY - self._rooms[i].startY) * distance
Why do you multiply these equations by the distance?
"(self._rooms[_agent].startX - self._rooms[i].startX)" already contains the (squared) distance!
Plus, multiplying everything by "distance" you modify your previous results stored in v!
If at least you put the "v.x" outside the bracket, the result would just be higher, the normalize function will fix it. Although that's some useless calculation...
By the way I'm pretty sure the code should be like:
v.x = v.x + (self._rooms[_agent].startX - self._rooms[i].startX)
v.y = v.y + (self._rooms[_agent].startY - self._rooms[i].startY)
I'll make an example. Imagine you have your main agent in (0,0) and three neighbours in (0,-2), (-2,0) and (0,2). A separation steering behaviour would move the main agent toward the X axis, at a normalized direction of (1,0).
Let's focus only on the Y component of the result vector.
The math should be something like this:
--Iteration 1
v.y = 0 + ( 0 + 2 )
--Iteration 2
v.y = 2 + ( 0 - 0 )
--Iteration 3
v.y = 2 + ( 0 - 2 )
--Result
v.y = 0
Which is consistent with our theory.
This is what your code do:
(note that the distance is always 2)
--Iteration 1
v.y = ( 0 + 0 + 2 ) * 2
--Iteration 2
v.y = ( 4 + 0 - 0 ) * 2
--Iteration 3
v.y = ( 8 + 0 - 2 ) * 2
--Result
v.y = 12
And if I got the separation steering behaviour right this can't be correct.

Cannot change value of object in lua

i have a table(array) where i keep the references of some images. Here is the code:
local rowcount = 8
local colcount = 4
local blockWidth = display.contentWidth / (colcount*4)
local blockHeight = display.contentWidth / (rowcount*2)
local row
local col
local pan = 3
local i=0
for row = 1, rowcount do
for col = 1, colcount do
local x = (col - 1) * blockWidth + pan + 30
local y = (row + 1) * blockHeight + pan
local random= math.random(1,6)
random = revisarTres(i, random)
local image = display.newImage(images[random], 0, 0)
image.x = x
image.y = y
print (x)
print (y)
image.value = random
image:addEventListener("touch", blockTouch)
block[i] = image
i=i+1
end
end
I keep the references in block of my 31 images.
Then i make a transition for one image to another, conversely. And i want to change the value, the coordenates and the reference of this two. I'm making this:
block[old].value, block[new].value = block[new].value, block[old].value
block[old].x, block[new].x = block[new].x, block[old].x
block[old].y, block[new].y = block[new].y, block[old].y
block[old], block[new] = block[new], block[old]
Old is the position of one of the images i want to change and new of the other one.
The reference is changing but the value doesn't.
Please can someone help me,
Thanks!
One small note (addition?):
if You ever find a table, where You can't directly change values - check if it's not readonly one. More about this kind of technique using Lua metatables: lua-users Wiki - Read Only Tables

Resources