Hello,
My name is Preston. I have just recently learned corona and am working on a project to build my very first app that is publishable. I am having some problems and wondering how I can do somethings that I didn’t learn in the book.
My App
I am trying to make an app that is for Ranchers that have cattle. I want to be able to add a birth and have a textField for the “Cows ID”, “Age”, “Calf ID”, “Sex”, “Date”, “Weight”, “Sire”, and then any “Notes”. I want a “save” and “cancel” button. The save button will “save” to a database and the “cancel” button will cancel and return to the menu. That is one thing I’m having a problem with. I can’t implement a close button and a save button. I guess I don’t know the best way to implement both.
After the save button is tapped it will save the data into a table and will be displayed in another button at the menu called “Herd Data”. I don’t know how to set up a table correctly. I wanted the table to have the Cows ID" and the "Calf ID" displayed and then when tapped it displays the rest of the information added, but I didn’t know how to do that and couldn’t find an example on the blog here at Corona. I would just like a little guidance, maybe some examples or just some good conversation and input of what I could do to make it better and easier.
Thank you so much!
I will copy and paste what code I have now. What I have right now throws an error for my “path” created for the file. I don’t understand why, but after that it crashes and I have to restart it. Thank you again… just need a little help I guess.
Thanks again
–
– main.lua
–
– Your code here
local storyboard = require(“storyboard”)
storyboard.gotoScene(“menu”)
–
– menu.lua
–
local widget = require (“widget”)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local localGroup = display.newGroup()
function scene:createScene(event)
local background = display.newImage(“barn.jpg”, display.contentCenterX, display.contentCenterY ) – WORK ON!! **
local display_stage = display.getCurrentStage()
display_stage:insert(background)
display_stage:insert(storyboard.stage)
background:toBack()
local herdData_function = function (event)
storyboard.gotoScene(“herdData”, “fade”, 400)
end
local herdData_button = widget.newButton
{
defaultFile = “Button.png”,
label = “Herd Data”,
size = 30,
fontSize = 60,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = herdData_function,
id = “herdData”,
}
local addBirth_function = function(event)
storyboard.gotoScene(“addBirth”, “fade”, 400)
end
local addBirth_button = widget.newButton
{
defaultFile = “Button.png”,
label = “Add Birth”,
size = 30,
fontSize = 60,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = addBirth_function,
id = “addBirth”,
}
local doctorVisit_function = function (event)
storyboard.gotoScene(“doctorVisit”, “fade”, 400)
end
local doctorVisit_button = widget.newButton
{
defaultFile = “Button2.png”,
label = “Doctor Visit”,
labelsize = 24,
fontSize = 40,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = doctorVisit_function,
id = “doctorVisit”,
}
local doctorRecords_function = function (event)
storyboard.gotoscene(“doctorRecords”, “fade”, 400)
end
local doctorRecords_button = widget.newButton
{
defaultFile = “Button2.png”,
label = “Doctor Records”,
size = 24,
fontSize = 40,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = doctorRecords_function,
id = “doctorRecords”,
}
addBirth_button.x = display.contentWidth/2
addBirth_button.y = display.contentHeight/2 -350
doctorVisit_button.x = display.contentWidth/2
doctorVisit_button.y = display.contentHeight/2 +10
doctorRecords_button.x = display.contentWidth/2
doctorRecords_button.y = display.contentHeight/2-150
herdData_button.x = display.contentWidth/2
herdData_button.y = display.contentWidth/2 + 500
localGroup:insert(addBirth_button)
addBirth_button:toFront()
localGroup:insert(doctorVisit_button)
localGroup:insert(doctorRecords_button)
localGroup:insert(herdData_button)
end
function scene:enterScene(event)
localGroup.alpha = 1
storyboard.purgeAll()
end
function scene:exitScene(event)
localGroup.alpha = 0
end
– “createScene” is called whenever the scene is FIRST called
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
return scene
– addBirth.lua
– Add Data
local widget = require(“widget”)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local localGroup = display.newGroup()
– require sqlite3
require “sqlite3”
– called if the cene has not been previously seen
function scene:createScene(event)
– Add text boxes to enter the data
local title = display.newText(localGroup, “*Congratulation on the new Birth!!*”, display.contentWidth/2, 100, native.systemFont, 30)
title:setFillColor(1,0,2)
local cowIDLabel = display.newText(localGroup, “Cow ID:”, 155, 200, native.systemFont, 30)
cowIDLabel:setFillColor(255,223,0)
local ageLabel = display.newText(localGroup, “Age:”, 425, 200, native.systemFont, 30)
ageLabel:setFillColor(255,223,0)
local calfIDLabel = display.newText(localGroup, “Calf ID:”, 155, 300, native.systemFont, 30)
calfIDLabel:setFillColor(255,223,0)
local sexLabel = display.newText(localGroup, “Sex:”, 425, 300, native.systemFont, 30)
sexLabel:setFillColor(255,223,0)
local dateLabel = display.newText(localGroup, “Date:”, 140, 400, native.systemFont, 30)
dateLabel:setFillColor(255,223,0)
local weightLabel = display.newText(localGroup, “Weight:”, 155, 500, native.systemFont, 30)
weightLabel:setFillColor(255,223,0)
local sireLabel = display.newText(localGroup, “Sire:”, 425, 500, native.systemFont, 30)
sireLabel:setFillColor(255,223,0)
– NOTES NEED TO BE SCROLLABLE!!
local notesLabel = display.newText(localGroup, “Notes”, display.contentWidth/2, 600, native.systemFont, 30)
notesLabel:setFillColor(255,223,0)
local line = display.newLine(340, 615, 425, 615)
line:setStrokeColor(255,233,0)
line.strokeWidth = 3
local cowID = native.newTextField(290, 200, 150, 45)
cowID.inputType = “default”
localGroup:insert(cowID)
local age = native.newTextField(550, 200, 150, 45)
age.inputType = “default”
localGroup:insert(age)
local calfID = native.newTextField(290, 300, 150, 45)
calfID.inputType = “default”
localGroup:insert(calfID)
local sex = native.newTextField(550, 300, 150, 45)
sex.inputType = “default”
localGroup:insert(sex)
– MAKE A PICKER WHEEL FOR THE DAT AND SET IT TO DEFAULT TO THE CURRENT DAY THAT DAY
local date = native.newTextField(420, 400, 410, 50)
date.inputType = “default”
localGroup:insert(date)
local weight = native.newTextField(290, 500, 150, 45)
weight.inputType = “default”
localGroup:insert(weight)
local sire = native.newTextField(550, 500, 150, 45)
sire.inputType = “default”
localGroup:insert(sire)
– NOTES NEED TO BE SCROLLABLE WHEN ENTERED
local notes = native.newTextField(display.contentWidth/2, 710, 400, 100)
notes.inputType = “default”
localGroup:insert(notes)
local addBirth_function = function(event)
– open sqlite database if it does not exist
local path = system.pathForFile(“herd.sqlite”, systemDocumentsDirectory)
db = sqlite3.open(path)
– check that the path was made
print(path)
– setup the table if it does not exist
local tablesetup = “CREATE TABLE IF NOT EXISTS birthclass (id INTEGERS PRIMARY KEY AUTOINCREMENT, CowId, Age, CalfId, Sex, Date, Weight, Sire, Notes);”
db = sqlite3.open(path)
– check that it created the table
print(path)
local tablefill = “INSERT INTO birthclass VALUES (NULL, '” …cowID.text… “’,’”…age.text…"’,’"…calfID.text…"’,’"…sex.text…"’,’"…date.text…"’,’"…weight.text…"’,’"…sire.text…"’,’"…notes…"’);"
– check that it filled the table
print(tablefill)
db:exec(tablefill)
– close database
db:close()
– check that it closed
print(“db closed”)
– clear textFields and return to menu screen
cowID:removeSelf()
age:removeSelf()
calfID:removeSelf()
sex:removeSelf()
date:removeSelf()
weight:removeSelf()
sire:removeSelf()
notes:removeSelf()
end – submitBirth_function
local saveBirth_button = widget.newButton
{
defaultFile = “Save.png”,
overFile = “Save.png”,
label = “Save”,
fontSize = 65,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = addBirth_function,
id = “addBirth”
}
saveBirth_button.x = display.contentWidth/2
saveBirth_button.y = display.contentHeight/2 +350
– add all display items to the group
localGroup:insert(saveBirth_button)
– handle the applicationExit scene to close db
local function onSystemEvent(event)
if(event.type == “applicationExit”) then
db:close()
end
end
– system listener for applicationExit to handle closing db
Runtime:addEventListener (“system”, onSystemEvent)
end
function scene:enterScene(event)
storyboard.purgeScene(“menu”)
localGroup.alpha=1
end
function scene:exitScene(event)
localGroup.alpha = 0
end
– “createScene” is called whenever the scene is FIRST called
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
return scene
– herdData.lua
– Hold the herds Data
local widget = require (“widget”)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local localGroup = display.newGroup()
– include sqlite
require “sqlite3”
– called if the scene hasnt been seen
function scene:createScene(event)
– open database
local pathe = system.pathForFile(“students.sqlite”, system.DocumentsDirectory)
db = sqlite3.open(path)
print(path)
– print all the table contents
local sql = “SELECT * FROM herd”
for row in db:nrows(sql) do
local text = row.CowID…", “…row.Age…”, “…row.CalfID…”, “…row.Sex…”, “…row.Date…”, “…row.Weight…”, “…row.Sire…”, "…row.Notes
local t = display.newText(text, 200, 30 *row.id, native.systemFont, 24)
t:setTextColor(255,255,255)
localGroup:insert(t)
end
– setup function for button to load student data
local herdData_function = function(event)
– return to menu screen
storyboard.gotoScene(“menu”, “slideRight”, 400)
end
local herdData_button = widget.newButton
{
defaultFile = “Button2.png”,
overFile = “Button2.png”,
label “Return to Menu”,
fontSize = 50,
labelColor = { default = {1,1,1}, over = {0,0,0,0.5}},
emboss = true,
onRelease = herdData_function,
id = “herdData”,
}
herdData_button.x = display.contentWidth/2 + 200
herdData_button.x = display.contentHeight/2 - 400
– add to local group
localGroup:insert(addHerd_button)
– handle the applicationexit event to close db
local function onSystemEvent(event)
if(event.type == “applicationExit”) then
db:close()
end
end
– system listener for applicationExit to handle closing database
Runtime:addEventListener (“system”, onSystemEvent)
end
function scene:enterScene(event)
storyboard.purgeScene(“menu”)
localGroup.alpha = 1
end
function scene:exitScene(event)
localGroup.alpha=0
end
– “createScene” is called whenever the scene is FIRST called
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
return scene