Lua - nil value - lua

I get the following error message from Corona SDK : attempt to call field "ImageSheet" (a nil value) stack traceback. Can somebody point out the mistake?
local ISPar = {
width = 2541,
height = 264,
numFrames = 7
}
local ImageSheet = graphics.ImageSheet("Apus.png, ISPar")
local ApusSequenceData = {
{name = "fly", frames {1,2,3,4,5,6,7}, time = 800, loopCount = 0}
}
local Apus = display.newSrpite(ImageSheet, ApusSequenceData)
Apus.x = display.contentWidth/2
Apus.y = display.contentHeight/2
Apus:play()

You got the function name wrong, it was supposed to be graphics.newImageSheet. Also you misplaced the quotation marks when calling it. And afterwards you misspelt newSprite
This code should work:
local ISPar = {
width = 2541,
height = 264,
numFrames = 7
}
local ImageSheet = graphics.newImageSheet("Apus.png", ISPar)
local ApusSequenceData = {{name = "fly", frames = {1,2,3,4,5,6,7}, time = 800, loopCount = 0}}
local Apus = display.newSprite(ImageSheet, ApusSequenceData)
Apus.x = display.contentWidth/2
Apus.y = display.contentHeight/2
Apus:play()

Related

Variables inside Functions (Corona Lua)

I've searched everywhere for an answer on how to change a variable inside a function. I have tried many different solutions but none have worked so far.
My plan is to change a variable named 'stateRed' inside a function which is run when a button is pressed. When the function is run, the variables will change to 'READY'. I will then use this variable to run a separate chunk of code (the actual game). There are two of these buttons.
My code looks something like this:
function ReadyRed()
stateRed = 'READY'
stateBtnRed = widget.newButton
{
defaultFile = "ready_red.png",
overFile = "unready_red.png",
label = "Ready",
emboss = true,
onPress = unreadyStateRed,
}
stateBtnRed.rotation = 90; stateBtnRed.width = 90; stateBtnRed.height = 90; stateBtnRed.x = display.contentWidth / 2 - 170; stateBtnRed.y = display.contentHeight - 270
return stateRed
end
function UnreadyRed()
local stateRed = 'UNREADY'
stateBtnRed = widget.newButton
{
defaultFile = "unready_red.png",
overFile = "ready_red.png",
label = "Unready",
emboss = true,
onPress = readyStateRed,
}
stateBtnRed.rotation = 90; stateBtnRed.width = 90; stateBtnRed.height = 90; stateBtnRed.x = display.contentWidth / 2 - 170; stateBtnRed.y = display.contentHeight - 270
return stateRed
end
The code that starts the game looks like this:
if stateRed == 'READY' and stateBlue == 'Ready' then
If variable stateRed is global then remove local keyword stand before variable name i.e.
function UnreadyRed()
stateRed = 'UNREADY'
...
end
Note:
You probably don't need return stateRed in ReadyRed and UnreadyRed functions since stateRed is global variable.
Use one notation style. I recommended you Camel Case notation

main.lua:25:attempt to call method'setanchorPoint'(a nil value)stack traceback:main.lua25:in maiin chunk

I am geeting an error: main.lua:25:attempt to call method'setanchorPoint'(a nil value)stack traceback:main.lua25:in maiin chunk. Please tell me how can I use anchorPoint. And how to resolve this issue
--constants
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
o = 0;
time_remain = 10;
time_up = false;
total_orbs = 20;
ready = false;
--Prepare sounds to be played or accessed
local soundtrack = audio.loadStream("media/soundtrack.caf");
local pop_sound = audio.loadSound("media/pop.caf");
local win_sound = audio.loadSound("media/win.caf");
local fail_sound = audio.loadSound("media/fail.caf");
local display_txt = display.newText("Wait", 0, 0, native.systemFont, 16*2);
display_txt.xScale = .5; display_txt.yScale = .5;
display_txt:setanchorPoint(display.BottomLeftanchorPoint);
display_txt.x = 20; display_txt.y = _H-20;
If you want to know how to use anchor points in Corona, the usual approach is to refer to the Corona manual....
Corona Anchor Guide
Anchors
The anchor of an object controls how geometry is positioned
relative to the object's origin. This is specified via the
object.anchorX and object.anchorY properties.
They even give examples....
You cannot just invent function names and wonder why you get error messages for callinig them.

for loop help and sprite addition

I have two questions. what I am
trying to do is every time I shoot a enemy/vine it should be
removed and a explosion should occur. the removal works perfect
but the sprite is not called to explode
What's this in the for loop #sections[sectInt]["vines"] ?
Are they parent/ child references? Can someone break this
down to the letter even telling me what the # is?
How can I use my explosion sprite after each vine is destroyed?
I am having a difficult time figuring out how to call every x and y
vine in the for loop to explode when removed.
Code:
local sections = require("sectionData")
local lastSection = 0
function createSection()
--Create a random number. If its eqaul to the last one, random it again.
local sectInt = mR(1,#sections)
if sectInt == lastSection then sectInt = mR(1,#sections) end
lastSection = sectInt
--Get a random section from the sectionData file and then
--Loop through creating everything with the right properties.
local i
-- the random creation of vines throughout the screen
for i=1, #sections[sectInt]["vines"] do
local object = sections[sectInt]["vines"][i]
local vine = display.newImageRect(objectGroup, "images/vine"..object["type"]..".png", object["widthHeight"][1], object["widthHeight"][2])
vine.x = object["position"][1]+(480*object["screen"]); vine.y = object["position"][2]; vine.name = "vine"
local rad = (vine.width*0.5)-8; local height = (vine.height*0.5)-8
local physicsShape = { -rad, -height, rad, -height, rad, height, -rad, height }
physics.addBody( vine, "static", { isSensor = true, shape = physicsShape } )
end
end
-- explosion sprite
options1 =
{
width = 96, height = 96,
numFrames = 16,
sheetContentWidth = 480,
sheetContentHeight = 384
}
playerSheet1 = graphics.newImageSheet( "images/explosion.png", options1)
playerSprite1 = {
{name="explosion", start=1, count=16, time = 400, loopCount = 1 },
}
explode = display.newSprite(playerSheet1, playerSprite1)
explode.anchorX = 0.5
explode.anchorY = 1
--player:setReferencePoint(display.BottomCenterReferencePoint)
-- i want to reference the for loop position if each vine so it plays sprite when they are removed
explode.x = "vine.x" ; explode.y = "vine .y"
explode.name = "explode"
explode.position=1
extraGroup:insert(explode)
1) What's this in the for loop #sections[sectInt]["vines"] ? Are they parent/ child references? Can someone break this down to the letter even telling me what the # is?
As I said in my comment the # is table length.
The loop is looping over each bit of "vine" data in the selected segment data (whatever that means exactly in terms of the game) and then creates the objects for those vines.
When it's time to make your vine explode, you play the sprite. If you want to explode every vine, you would have something like:
sheetOptions =
{
width = 96, height = 96,
numFrames = 16,
sheetContentWidth = 480,
sheetContentHeight = 384
}
playerSheet = graphics.newImageSheet( "images/explosion.png", sheetOptions)
spriteSequence = {
{name="explosion", start=1, count=16, time = 400, loopCount = 1 },
}
for i, vine in ipairs(objectGroup) do
local explode = display.newSprite(playerSheet, spriteSequence)
explode.anchorX = 0.5
explode.anchorY = 1
explode.x = vine.x
explode.y = vine.y
explode.name = "explode"
explode.position=1
explode:play()
extraGroup:insert(explode)
end
Note: not tested, let me know of if any issues you can't resolve.
ok so, this is what i did to get my object to have a explosion, for anybody that is having the same issue.
function isShot( event )
print('shot!!!')
if (event.other.class == "enemy") then
if (event.other.name == "limitLine") then
event.target:doRemove() --remove bullet if hits the limitLine
elseif (event.other.name == "vine") then
event.other:doRemove()
spriteExplode(event.other.x, event.other.y) -- this function calls itself & runs the sprite
if event.other.name == "vine" then
if event.other.name == "explode" then
event.other:doRemove() --this removes the explosion sprite
end
end
end
-- remove the bullet & explosion sprite
timer.performWithDelay( 5000, function(event) explode:doRemove(); end, 1 )
event.target:doRemove()
end
return true
end
function spriteExplode( x, y)
explode.isVisible = true
explode:play()
print("play explode")
if (x and y) then -- this is the code that keeps my sprite updating upon removal of vine
explode.x, explode.y = x, y
end
function explode:doRemove(event)
timer.performWithDelay(
1,
function(event)
display.remove(self)
self= nil
end,
1 )
end
end
i added the isShot function eventListener inside of the Bullet function
bullet:addEventListener("collision", isShot) & a bullet:doRemove function is inside the bullet function as well.
hope this helps.

How can I access a lua file with a tab bar?

I am having an error when trying to access a file with a tab bar constructed within it. Does anyone know what I am doing wrong. Corona SDK tells me my error is in the section of code where I actually create the tab bar, not the buttons. Thanks
Below is my code for the MainPage.lua scene in which the tab bar is created.
local tabBar = nil
function scene: create (event)
local group = scene.view
local tabButtons =
{
{
width = 20,
height = 32,
defaultFile = "assets/home.png",
overFile = "assets/PressHome.png",
label = "Home",
font = tabLabelFont,
size = tabLabelFontSize,
onPress = function() composer.gotoScene( "Home" ); end,
},
{
width = 20,
height = 32,
defaultFile = "assets/Explore.png",
overFile = "assets/Explore.png",
label = "Explore",
font = tabLabelFont,
size = tabLabelFontSize,
onPress = function() composer.gotoScene( "Explore" ); end,
},
{
width = 20,
height = 32,
defaultFile = "assets/Post.png",
overFile = "assets/Post.png",
label = "Post",
font = tabLabelFont,
size = tabLabelFontSize,
onPress = function() composer.gotoScene( "Post" ); end,
},
{
width = 20,
height = 32,
defaultFile = "assets/Notification.png",
overFile = "assets/Notification.png",
label = "Notification",
font = tabLabelFont,
size = tabLabelFontSize,
onPress = function() composer.gotoScene( "Notification" ); end,
},
{
width = 20,
height = 32,
defaultFile = "assets/Profile.png",
overFile = "assets/Profile.png",
label = "Profile",
font = tabLabelFont,
size = tabLabelFontSize,
onPress = function() composer.gotoScene( "Profile" ); end,
}
}
-- Create a tab-bar and place it at the bottom of the screen
tabBar = widget.newTabBar
{
top = display.contentHeight - 50,
width = display.contentWidth,
backgroundFile = "assets/tabbar.png",
tabSelectedLeftFile = "assets/tabBar_tabSelectedLeft.png",
tabSelectedMiddleFile = "assets/tabBar_tabSelectedMiddle.png",
tabSelectedRightFile = "assets/tabBar_tabSelectedRight.png",
tabSelectedFrameWidth = 20,
tabSelectedFrameHeight = 52,
buttons = tabButtons
}
group: insert(tabBar)
composer.gotoScene("Home")
end
scene:addEventListener( "create", scene )
return scene
here is the method from the log in page that is supposed to access the tab bar page.
local function EnterPage( event )
composer.gotoScene("MainPage")
end
this is what the error is saying
stack traceback:
?: in function '?'
?: in function <?:703>
(tail call): ?
?: in function <?:122>
(tail call): ?
MainPage.lua:127: in main chunk
[C]: in function 'require'
?: in function <?:797>
(tail call): ?
LogIn.lua:78: in function <LogIn.lua:70>
?: in function <?:218>
line 127 of Main Page is the line that states "tabBar = widget.newTabBar"
The errors in the log in page just go to the gotoScene(MainPage) method
"File: ? Attempt to index a nil value" is very helpful: it says that you are doing an operation of the form a.b like a.b = something or a.b() where a is nil. Given the line it happens on, this is surely because you forgot
widget = require 'widget'
near the top of your file.
If that line is already there (in MainPage.lua), then check that widget hasn't been overwritten:
print('widget is', widget)
tabBar = widget.newTabBar
...
If it is not nil then you have made an error in the information you posted (wrong line number, etc) or maybe you tried many things and info got mixed up. You could try, just to be sure,
widget = require 'widget'
tabBar = widget.newTabBar
If that works, then you know you are overwriting widget somewhere in your MainPage.lua, in code not shown. You should also try
test = widget.newButton
{
left = 100, top = 200,
label = "Default",
onEvent = function() print('hi') end
}
If that works, but the newTabBar still doesn't work, try replacing your options with those of the example on corona newTabBar site.

Lua - Not displaying text in Corona SDK

In this app I'm creating with Corona SDK, when you win the text "You win" sould appear, but doesn't. I could post all the code, but I don't think the rest would be hepful, so here is only the essencial:
_H = display.contentHeight;
_W = display.contentWidth;
mRand = math.random;
o = 0;
time_remain = 20;
time_up = false;
total_orbs = 45;
total_secs = 20;
ready = false;
local backGround = display.newImage("media/bg.png");
backGround.xScale = 2;
backGround.yScale = 2;
loseMSG = display.newText("You Lose!", _W/2, _H/2, nil, 50)
loseMSG.isVisible = false
winMSG = display.newText("You Win!", _W/2, _H/2, nil, 50)
winMSG.isVisible = false
local countdowntxt = display.newText(time_remain, 0, 0, native.systemFont, 60);
countdowntxt.xScale = .5; countdowntxt.yScale = .5;
countdowntxt:setReferencePoint(display.BottomRightReferencePoint);
countdowntxt.x = _W-20; display.y = _H-20;
countdowntxt:setTextColor(0, 0, 0)
function winLose(condition)
if (condition == "Win") then
bgAlpha = display.newImage("media/bgAlpha.png");
bgAlpha.xScale = 2;
bgAlpha.yScale = 2;
winMSG.isVisible = true -- Here the win text should become visible, but doesn't
elseif (condition == "Fail") then
bgAlpha = display.newImage("media/bgAlpha.png");
bgAlpha.xScale = 2;
bgAlpha.yScale = 2;
loseMSG.isVisible = true
end
end
Any Ideas why?
You need to take a divide and conquer approach.
Does this work?
winMSG = display.newText("You Win!", _W/2, _H/2, nil, 50)
winMSG.isVisible = true <-- note this is set to true
It does?
Does winLose every get called? Put a print statement in it or a break point or whatever you use on your platform to debug.
It does?
What does the variable condition contain? Inspect it/print it out and verify that it is indeed "Win", same case, no spaces, etc. or you can put a print statement in the appropriate branch of that function to make sure it's being hit.
Is that OK?
Does it show up if you remove the bgAlpha code?
So on and so forth.
I don't know Corona, but Googling the docs for newText it's possible you have the parameters wrong (you're not passing a font).
I could post all the code
The less you post the better, because that means you've already gone through the steps shown above to try to isolate the problem. Nine times out of ten, doing that will reveal the problem all by itself.
Why have you given nil for font value?
Atleast pass the default system font
loseMSG = display.newText("You Lose!", _W/2, _H/2, native.systemFont, 50)
winMSG = display.newText("You Win!", _W/2, _H/2, native.systemFont, 50)

Resources