I am trying to make a bird class in Bird.lua and require it in main.lua
However, Bird.lua makes use of a global variables VIRTUAL_WIDTH and VIRTUAL_HEIGHT defined in main.lua
I have tried requiring main.lua in Bird.lua as well, but I received loop or previous error loading module error.
How should I go about doing this?
main.lua
push = require 'push'
Bird = require 'Bird'
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288
local background = love.graphics.newImage('background.png')
local backgroundScroll = 0
local BACKGROUND_SPEED = 30
local BACKGROUND_LOOP_POINT = 413
local ground = love.graphics.newImage('ground.png')
local groundScroll = 0
local GROUND_SPEED = 70
...
Bird.lua
require 'main' --error
Bird = {}
function Bird:new(name)
o = o or {}
setmetatable(o,self)
self.__index = self
o.name = name
self.image = love.graphics.newImage('bird.png')
--returns width of image
self.width = self.image:getWidth()
--returns height of image
self.height = self.image:getHeight()
--centering the bird by default
self.x = VIRTUAL_WIDTH / 2 - (self.width / 2)
self.y = VIRTUAL_HEIGHT / 2 - (self.height / 2)
return o
end
For the sake of completion, I'm providing an answer based on Nifim's comment.
You should move the global variables in a separate file, e.g. constants.lua:
WINDOW_WIDTH = 1280
WINDOW_HEIGHT = 720
VIRTUAL_WIDTH = 512
VIRTUAL_HEIGHT = 288
Then, remove the require 'main' from Bird.lua and require 'constants' in your main.lua. Note that this will probably make the variables from constants.lua globally available which can be troubling (see also Nifim's comment).
I have a list of color codes and want to make a text fade the colors smoothly like in this picture:
Is this possible with Corona? I couldn't find a way to do this yet.
Try this function I made, might not be perfect but it works for me:
-- Transitions a display object from one color to another (based on 0 to 1 values)
function applyColorTransition(oDisplayObject, nDelay, nStartR, nEndR, nStartG, nEndG, nStartB, nEndB)
if oDisplayObject.setFillColor then
local nIterations = 10
local nStepsR = (nEndR - nStartR) / nIterations
local nStepsG = (nEndG - nStartG) / nIterations
local nStepsB = (nEndB - nStartB) / nIterations
local nI = 1
timer.performWithDelay( nDelay, function()
oDisplayObject:setFillColor(nStartR + nStepsR * nI, nStartG + nStepsG * nI, nStartB + nStepsB * nI)
nI = nI + 1
end, nIterations)
end
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 have a image declared at some specific location.
local deselectButton = display.newImage ( "images/nutritional info/deselectButton.png" )
deselectButton.x = display.contentWidth / 2 - 15
deselectButton.y = display.contentHeight / 2 - 172
deselectButton.id = "0"
nutriinfo:insert(nutriNavBar)
When I tap on that image, I want another image to be displayed. That is, this second image should fade in and out every time I click on the above image.
local dropDown1 = display.newImage ( "images/nutritional info/dropDown.png" )
dropDown1.x = display.contentWidth / 2 - 75
dropDown1.y = display.contentHeight / 2 - 65
dropDown1:setReferencePoint(display.TopCenterReferencePoint)
After your code, just do as follows... This may help you:
local function addListener()
deselectButton:addEventListener("tap",clickFunction)
end
local clickCount = 0
function clickFunction()
deselectButton:removeEventListener("tap",clickFunction)
clickCount = clickCount + 1
if(clickCount%2==1)then
-- show the image
transition.to(dropDown1,{time=200,x=dropDown1.x,y=dropDown1.y+100,alpha=1,onComplete=addListener}) -- or parameters as you like
else
-- hide the image
transition.to(dropDown1,{time=200,x=dropDown1.x,y=dropDown1.y-100,alpha=0,onComplete=addListener})
end
end
deselectButton:addEventListener("tap",clickFunction)
Note: The above code provides you both drop down as well as fade-in/out effect. But, if you need fade in and fade out effect only, you can eliminate y parameter from transition, and if you want the drop down type effect, you can eliminate the alpha parameter.
Keep coding............... :)
I have written some lua code like this:
local a1 = 1
local a2 = 2
local a3 = 3
local a4 = 3
local a5 = 3
local a6 = 3
local a7 = 3
local a8 = 3
local a9 = 3
local a10 = 3
local a11 = 3
local a12 = 3
local a13 = 3
local a14 = 3
local a15 = 3
local a16 = 3
local a17 = 3
local a18 = 3
local a19 = 3
local a20 = 3
local a21 = 3
local a22 = 3
local a23 = 3
local a24 = 3
local a25 = 3
local a26 = 3
local a27 = 3
local a28 = 3
local a29 = 3
local a30 = 3
local a31 = 1
local a32 = 2
local a33 = 3
local a34 = 3
local a35 = 3
local a36 = 3
local a37 = 3
local a38 = 3
local a39 = 3
local a40 = 3
local a41 = 3
local a42 = 3
local a43 = 3
local a44 = 3
local a45 = 3
local a46 = 3
local a47 = 3
local a48 = 3
local a49 = 3
local a50 = 3
local a61 = 3
local a62 = 3
local a63 = 3
local a64 = 3
local a65 = 3
local a66 = 3
local a67 = 3
local a68 = 3
local a69 = 3
local a70 = 3
local function fun1(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10)
print('.......')
end
local function fun2()
fun1(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)
fun1(a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
fun1(a21, a22, a23, a24, a25, a26, a27, a28, a29, a30)
fun1(a31, a32, a33, a34, a35, a36, a37, a38, a39, a40)
fun1(a41, a42, a43, a44, a45, a46, a47, a48, a49, a50)
fun1(a51, a52, a53, a54, a55, a56, a57, a58, a59, a60)
fun1(a61, a62, a63, a64, a65, a66, a67, a68, a69, a70)
end
and i got an error like this when ran this code:
78: function at line 71 has more than 60 upvalues
i know this kind of code is ugly and i can use some way else to do it (like table), but this kind of code may be written by my users.
can someone explain this for me, and tell me how to avoid this? thanks very much.
tell me how to avoid this?
There's the old joke. A guy walks into a doctor's office and says, "It hurts when I raise my arm like this." The doctor says, "So don't raise your arm like that."
If your users write Lua code that does not compile, that's not something you can fix. This is no more illegitimate a compile error than something like:
if condition --forgot the then
return something
end
So if your code is going to accept arbitrary Lua scripts to compile and execute, it will need to be able to deal with Lua scripts that don't compile. For whatever reason.
If compilation fails, report the error to the user and recover the best you can.
Maximum 60 upvalues, that is values from outer scopes that your closure is closed over, is one of internal Lua limits. Of course you can change it by recompiling Lua itself, but I'd advise against it. Pack your values in some table instead, with its layout dictated by logic of code. In your particular example you really should be using:
local a = {}
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 3
a[5] = 3
-- etc...