I'm try to create a new game on corona SDK i'm new in lua language, my goal is had a set of enemies in a kind of action game.
For this i think the best way is have a array to store all my enemeis in this case i use three.
So my code is :
local enemies = {}
enemy1 = display.newImageRect( "assets/images/sheep_mini.png", 60, 60 )
enemy1.anchorX = 0
enemy1.anchorY = 0
enemy1.name = 'enemy'
enemy1.id = 1
enemy1.x, enemy1.y = 28, display.contentHeight - 260
enemy1.angularVelocity = 0
enemies[1] =enemy1
enemy2 = display.newImageRect( "assets/images/sheep_mini.png", 60, 60 )
enemy2.anchorX = 0
enemy2.anchorY = 0
enemy1.id = 2
enemy2.name = "enemy"
enemy2.x, enemy2.y = screenW - 120, display.contentHeight - 420
enemy2.angularVelocity = 0
enemies[2] =enemy2
So after that i've a while to iterate to this enemies enemies, but when i try to get the enemies from the array i only getting this :
Mar 31 02:23:36.576: table: 0x600000a66640
Mar 31 02:23:36.577: table: 0x600000a78e00
i'm using this code for doing while :
local len = #enemies
local i= 1
while i <= len do
enemy1 = enemies[i]
print(enemy1)
end
Can you help here? i'm now on corona and also on lua
thanks in advance
What you are trying to achieve can be done through
table.print(enemy1)
For more information I suggest you read this: Table Serialization which explains how:
functions to serialize/unserialize a table or object (usually, not
always, represented as a table), which is to convert it to and from a
string representation. This is typically used for display (e.g.
debugging) or storing data in a file (e.g. persistence).
Related
I have a table full of enemies and simple want them to move across the screen via gameLoop, however for some reason nothing seems to work. Its probably an easy fix but I have tried to fix it but am getting nowhere. Anyone know whats up?
gameLoop Function
local i
for i = 1, #enemies do--.numChildren,1, -1 do
local blocks = enemies[i]
if blocks ~= nil and blocks.x ~= nil then
enemyRate = 2.0 + (0.1 * wave)
transition.to( blocks, { time=1500, x=300} )
end
end
The Spawn Function
function spawnEnemy()
local spawnData = { -- Easily store spawns in a table to make it easier to add new enemies later
{name = "Blue", seq = "blueRect", frame = 3, imgSheet = imageSheetRectangle, seqData = sequenceDataRectangle},
{name = "Red", seq = "blueCross", frame = 1, imgSheet = imageSheetCross, seqData = sequenceDataCross},
{name = "Green", seq = "blueCirc", frame = 2, imgSheet = imageSheetCircle, seqData = sequenceDataCircle}
}
local xPos = display.contentWidth - 150
local r = math.random(1, #spawnData)
local sd = spawnData[r] -- get the spawn data for this enemy
local s = display.newSprite(sd.imgSheet, sd.seqData)
s.name = sd.name
physics.addBody(s, { isSensor = true })
s:setSequence(sd.seq)
s:setFrame(sd.frame)
s.y = display.contentHeight - 400
s.x = xPos
enemies[#enemies+1] = s
enemyGroup:insert(s)
In this related answer: transition.to( ) doesn't work within a function and with a Runtime:addEventListener( "enterFrame", method) listener in Corona / Lua you can see a similar issue as I stated above. You are creating an animation right as one is starting - making it seem as though it is not moving. As I suggested above, if it suits your game, begin the transition when you spawn the object; not every gameloop.
I'm trying to generate 40 circles with text and I want to be able to use them and handle them later.
According to several post I found here and on corona forums, I'm doing it well but, in all the examples, is only with one "display" object... not with several so I'm not sure if it's possible or maybe it needs something different.
After some introduction... here the code:
function _.spawn(params)
local orb = display.newCircle( display.contentWidth/2, display.contentHeight/2, 40 ) -- creates the orb shape
orb.orbTable = params.orbTable --assign the internal table
orb.index = #orb.orbTable + 1 -- takes control of the index
orb.orbTable[orb.index] = orb -- assign a orb to the internal table
orb:setFillColor( unpack(params.color) ) -- change the color according to the rules above
orb.x = params.x -- control the X position of the orb
orb.y = params.y --control the Y position of the orb
orb.alpha = 1 -- borns with alpha = 0
orb.value = params.value -- assign the internal value of the orb (1-10)
--local orbText = display.newText( orb.value, orb.x+2, orb.y-5, "Aniron", 40 ) -- generates the value on the ball
--orbText.orbTable = params.orbTable
--orbText.index = #orbText.orbTable + 1
--orbText.orbTable[orbText.index] = orbText
--orbText:setFillColor( 0,0,0 )
--orbText.alpha = 1
--The objects group
orb.group = params.group or nil
--If the function call has a parameter named group then insert it into the specified group
orb.group:insert(orb)
--orb.group:insert(orbText)
--Insert the object into the table at the specified index
orb.orbTable[orb.index] = orb
return orb -- returns the orb to have control of it
end
The function to generate all the objects I need is:
function _.generateDeck()
for i = 1, 40 do -- loop to generate the 40 orbs
internalValue = internalValue + 1 -- counter to assigns the right values
if (internalValue > 10) then -- if the internal value is more than 10 starts again (only 1-10)
internalValue = 1
end
if (i >= 1 and i <= 10) then -- assign RED color to the first 10 orbs
completeDeck[i] = _.spawn({color = {196/255,138/255,105/255}, group = orbsGroup, x = 100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
elseif (i >= 11 and i <= 20) then -- assigns YELLOW color to the next 10 orbs
completeDeck[i] = _.spawn({color = {229/255,214/255,142/255}, group = orbsGroup, x = 100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
elseif (i >= 11 and i <= 30) then -- assigns BLUE color to the next 10 orbs
completeDeck[i] = _.spawn({color = {157/255,195/255,212/255}, group = orbsGroup, x = 100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
elseif (i >= 11 and i <= 40) then -- assigns GREEN color to the next 10 balls
completeDeck[i] = _.spawn({color = {175/255,181/255,68/255}, group = orbsGroup, x = 100, y = display.contentHeight / 2, value = internalValue, orbTable = completeDeck})
end
end
end
After calling the function, it generates the circles correctly and I can read the value:
for i = 1, #completeDeck do
print("Complete Deck Value ("..i.."): "..completeDeck[i].value)
end
But if I uncomment the lines regarding the newText (orbText) then I'm not able to read the values... (value is a nil value)
I guess I need to create a displayGroup with the objects I want inside (circle and text), then "spawn" it, modifying the values? In that case... how can I refer to the object inside the displayGroup?
I think I'm mixing different concepts.
In Lua, objects are tables and to create an object composed of existing objects, we create a table with and add the objects to it as values. The code below creates an objects with orb and orbText which can be accessed by object.orb or object.orbText
function _.spawn(params)
local orb = display.newCircle(display.contentWidth/2, display.contentHeight/2, 40) -- creates the orb shape
orb:setFillColor(unpack(params.color))
orb.x = params.x
orb.y = params.y
orb.alpha = 1
local orbText = display.newText(param.value, param.x + 2, param.y - 5, "Aniron", 40)
orbText:setFillColor(0, 0, 0)
orbText.alpha = 1
-- create an object with orb and orbText as values
local object = {orb = orb, orbText = orbText}
-- add more values to the object
object.value = params.value
object.index = #params.orbTable + 1
params.orbTable[object.index] = object
return object
end
Now to use the object:
for i, object in ipairs(completeDeck) do
print(object.value)
end
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
I'm trying to make a game similar to candy crush in Lua. Here is the code:
local images = {
"images/beer.png",
"images/beef.png",
"images/canned_food.png",
"images/cup_ice_cream.png",
"images/french_fries.png",
"images/pepper.png"
}
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
for row = 1, rowcount do
for col = 1, colcount do
local x = (col - 1) * blockWidth + pan
local y = (row + 1) * blockHeight + pan
local block = display.newImage(images[math.random(1, 6)], x, y)
block:addEventListener("touch", blockTouch)
end
end
I need to know which image is moving, to know if with the new position they made 3 in a line.
So my question is, how can i have an id or a identifier to know which image the user is moving in the table?
Thanks for your help
you must put ID in each object you create for example:
local function getID(event)
t = event.target
print(t.id)
end
local beef = display.newImage("images/beef.png",)
beef.id = "beef"
local canned_food= display.newImage("images/canned_foods.png",)
canned_food.id = "cannedfoods"
local fries = display.newImage("images/fench_fries.png",)
fries.id = "fries"
beef:addEventListener("tap", getID())
canned_food:addEventListener("tap", getID())
fries:addEventLister("tap", getID())
hopes this helps :)
I would put your blocks into a table to keep track of each of them. But to answer your specific question, Lua allows you to add any method or attribute to an object, so you can do:
block.name = "Beer"
block.color = "Green"
block.gobbldygook = 400
Then in your tap/touch handler, your "event.target" is the object, so you can say:
print(event.target.gobbldygook)
Hi and good day stackoverflow community
how can i set an id for this kind of code using corona sdk/lua
the number 22 represents the current x position, for me to properly delete/destroy
some of the ground i need to pin point the coordinates of x,y,z
it should be like (1,1,1) or (2,1,69). Can please someone help me.
Thank you so much
ground = {}
for i = 1, 68 do
for j = 1, 100 do
ground[i] = display.newImageRect( "assets/minebackground.png", 256 , 128)
ground[i].x = -1500 + (i*50)
ground[i].y = 5254 - (j*50)
physics.addBody( ground[i], "static" , { density=0.1, friction= 0 } )
ground[i].id = i
ground[i]:addEventListener("tap", oncollision)
screenGroup:insert (ground[i])
game : insert (ground[i])
end
end
for pint pointing
function oncollision(event)
ground = event.target
return ground.id
end
in your onCollision function, event.target.x and event.target.y are the X and Y for the touched item.