How to read a specific file using corona sdk - file

I use to program although it has been quite a few years so Im having to relearn most of it, Im trying to make it so when one of the 3 buttons are clicked it reads the file attached to the button but all i get at the moment is nul in the terminal when i run it
Ive tried moving around sections of code but I cant find anything that works
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local background = display.newImage("background.png")
background.x = centerX
background.y = centerY
local widget = require( "widget" )
-- Path for the file to read
local cluthaDirect = system.pathForFile( "clutha_Run.txt" )
local ranfDirect = system.pathForFile( "ranf_Run.txt" )
local centDirect = system.pathForFile( "cent_Run.txt" )
local function cluthaButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print(cluthaFile)
end
end
local function centButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print("C E N T R A L")
end
end
local function ranfButtonEvent(event)
local phase = event.phase
if "ended" == phase then
print("R A N F U R L Y")
end
end
local cluthaButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "clutha",
onEvent = cluthaButtonEvent,
}
local centButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 80,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "central",
onEvent = centButtonEvent,
}
local ranfButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 160,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "ranf",
onEvent = ranfButtonEvent,
}
-- Path for the file to read
--local cluthaDirect = system.pathForFile( "clutha_Run.txt" )
--local ranfDirect = system.pathForFile( "ranf_Run.txt" )
--local centDirect = system.pathForFile( "cent_Run.txt" )
-- Open the file handle
local cluthaFile, errorString = io.open( cluthaDirect, "r" )
local centFile, errorString = io.open( centDirect, "r" )
local ranfFile, errorString = io.open( ranfDirect, "r" )
if not cluthaFile then
-- Error occurred; output the cause
print( "cluthaFile error: " .. errorString )
else
-- Output lines
for line in cluthaFile:lines() do
print( line )
end
-- Close the file handle
--io.close( cluthaFile )
end
cluthaFile = nil

Try (not tested)
local widget = require( "widget" )
local function getContentOfFile( fileName, dir )
local contents = ''
local dir = dir or system.ResourceDirectory
local path = system.pathForFile( fileName, dir )
local file, errorString = io.open( path, "r" )
-- Open the file handle
local file, errorString = io.open( path, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Read data from file
contents = file:read( "*a" )
-- Close the file handle
io.close( file )
end
file = nil
return contents
end
local function cluthaButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("clutha_Run.txt") )
end
end
local function centButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("cent_Run.txt") )
end
end
local function ranfButtonEvent( event )
local phase = event.phase
if "ended" == phase then
print( getContentOfFile("ranf_Run.txt") )
end
end
local cluthaButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "clutha",
onEvent = cluthaButtonEvent,
}
local centButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 80,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "central",
onEvent = centButtonEvent,
}
local ranfButton = widget.newButton
{
left = centerX - 60,
top = centerY - centerY + 160,
width = display.contentWidth/2,
height = 60,
defaultFile = "buttonUnpressed.png",
overFile = "buttonPressed.png",
label = "ranf",
onEvent = ranfButtonEvent,
}

Related

Fixing Plotting of Initial Balance Indicator in AmiBroker AFL

I am using the following AFL code for "Initial Balance with Range Extension" in AmiBroker -
_SECTION_BEGIN("Initial Balance with Range Extensions");
P11 = Param("IB Start Time",091500, 0 , 235959, 1 ) ;
P12 = Param("IB END Time",101500, 0 , 235959, 1 ) ;
START = (TimeNum()>= P11);
END = (TimeNum()<= P12);
ZONE = START AND END;
ST = (TimeNum()>= P12);
NewTime = ZONE!= Ref(ZONE, -1);
highestoftheday = HighestSince(NewTime,H,1);
Lowestoftheday = LowestSince(NewTime,L,1);
IBHigh = ValueWhen(ZONE,highestoftheday,1);
IBLow = ValueWhen(ZONE,lowestoftheday,1);
ORBClose = ValueWhen(zone,C,1);
IBrange = IBHigh - IBLow; // First Hour Range
IBM = IBLow+IBrange/2;
IB1xh = IBHigh+IBrange ; // Target 1 for range extension upside
IB2xh = IBHigh+2*IBrange ;
IB3xh = IBHigh+3*IBrange ;
IB1xl = IBLow-IBrange ;
IB2xl = IBLow-2*IBrange ; // target 1 for range extension downside
IB3xl = IBLow-3*IBrange ;
PlotGrid(LastValue(IBHigh, True), colorPlum, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IBLow, True), colorPlum, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IBM, True), colorBrown, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IB1xh, True), colorGreen, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IB2xh, True), colorGreen, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IB3xh, True), colorGreen, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IB1xl, True), colorRed, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IB2xl, True), colorRed, pattern=10, width = 2, label = True);
PlotGrid(LastValue(IB3xl, True), colorRed, pattern=10, width = 2, label = True);
_SECTION_END();
This code is plotting continuous horizontal lines. But I need disjointed horizontal lines for individual sessions. How can I get that? Please help me to fix this issue. Thanks for your time and effort. Regards.

2 object of same type returns true when != checked

I am using model.GetType().GetProperties() with foreach to compare properties of 2 object of same class.
like this
foreach (var item in kayit.GetType().GetProperties())
{
var g = item.GetValue(plu);
var b = item.GetValue(kayit);
if (g is string && b is string&& g!=b)
{
a += item.Name + "*";
}
else if (g is DateTime&& b is DateTime&& g!=b)
{
a += item.Name + "*";
}
}
But the problem is even if they have the same value g!=b returns a true all the time. I have used a break point to prove this and they are literally same thing. Actually I am taking the value putting it in textbox then creating another class after button click and comaring to see the changed properties. So even if I don't change anything it doesn't read the mas equals. Can someone help me about this please?
more info:
I get the plu from database and populate my control with it:
txtorder.Text = plu.OrderNo;
dtporder.Value = nulldate(plu.OrderDate);
dtp1fit.Value = nulldate(plu.FirstFitDate);
dtp1yorum.Value = nulldate(plu.FirstCritDate);
dtp2fit.Value = nulldate(plu.SecondFitDate);
dtp2yorum.Value = nulldate(plu.SecondCritDate);
dtpsizeset.Value = nulldate(plu.SizeSetDate);
dtpsizesetok.Value = nulldate(plu.SizeSetOkDate);
dtpkumasplan.Value = nulldate(plu.FabricOrderByPlan);
txtTedarikci.Text = plu.Fabric_Supplier;
dtpkumasFP.Value = nulldate(plu.FabricOrderByFD);
dtpfabarrive.Value = nulldate(plu.FabricArrive);
dtpbulk.Value = nulldate(plu.BulkFabricDate);
dtpbulkok.Value = nulldate(plu.BulkConfirmDate);
dtpaccessory.Value = nulldate(plu.AccessoriesDate);
dtpaccessoryarrive.Value = nulldate(plu.AccessoriesArriveDate);
dtpcutok.Value = nulldate(plu.ProductionStartConfirmation);
dtpcutstart.Value = nulldate(plu.ProductionStart);
dtpshipmentdate.Value = nulldate(plu.ShipmentDate);
dtpshipmentsample.Value = nulldate(plu.ShipmentSampleDate);
dtpshippedon.Value = nulldate(plu.Shippedon);
nulldate is just a method where I change null values to my default value.
And this is what I do after button click:
var kayit = new uretim();
kayit.OrderNo = txtorder.Text.ToUpper();
kayit.OrderDate = vdat(dtporder.Value);
kayit.FirstFitDate = vdat(dtp1fit.Value);
kayit.FirstCritDate = vdat(dtp1yorum.Value);
kayit.SecondFitDate = vdat(dtp2fit.Value);
kayit.SecondCritDate = vdat(dtp2yorum.Value);
kayit.SizeSetDate = vdat(dtpsizeset.Value);
kayit.SizeSetOkDate = vdat(dtpsizesetok.Value);
kayit.FabricOrderByPlan = vdat(dtpkumasplan.Value);
kayit.Fabric_Supplier = txtTedarikci.Text;
kayit.FabricOrderByFD = vdat(dtpkumasFP.Value);
kayit.FabricArrive = vdat(dtpfabarrive.Value);
kayit.BulkFabricDate = vdat(dtpbulk.Value);
kayit.BulkConfirmDate = vdat(dtpbulkok.Value);
kayit.AccessoriesDate = vdat(dtpaccessory.Value);
kayit.AccessoriesArriveDate = vdat(dtpaccessoryarrive.Value);
kayit.ProductionStartConfirmation = vdat(dtpcutok.Value);
kayit.ProductionStart = vdat(dtpcutstart.Value);
kayit.ShipmentDate = vdat(dtpshipmentdate.Value);
kayit.ShipmentSampleDate = vdat(dtpshipmentsample.Value);
kayit.Shippedon = vdat(dtpshippedon.Value);
kayit.Status = true;
kayit.WrittenDate = DateTime.Now;
kayit.GuidKey = plu.GuidKey != null ? plu.GuidKey : Guid.NewGuid().ToString("N");
I have proven by breakpoint that values are actually same. But the != check retruns a true.
When you are doing
g != b
compiler doesn't know that these objects are strings to compare so it compares their references. You can do:
g.Equals(b) //be carefull if one of them is null
or
g.ToString() != b.ToString()
EDIT
You can compare them after you check the type:
if (g is string && b is string)
{
if( g.ToString() != b.ToString() ){
}
}

lights out game using CORONA SDK

am trying to devalope a lights out game with CORONA SDK
but am not able to figure out a way for looping it !!!
how many functions to create and the way to keep this going
here is my code (its dummy but a friend gave it to me as am trying to go on from there )
obj = nil
px = 35
py = 50
r = 22
xi = 60
yi = 60
x1y1 = display.newCircle(px+xi*0,py+yi*0,r) x1y1.id = "x1y1"
x2y1 = display.newCircle(px+xi*1,py+yi*0,r) x2y1.id = "x2y1"
x3y1 = display.newCircle(px+xi*2,py+yi*0,r) x3y1.id = "x3y1"
x4y1 = display.newCircle(px+xi*3,py+yi*0,r) x4y1.id = "x4y1"
x5y1 = display.newCircle(px+xi*4,py+yi*0,r) x5y1.id = "x5y1"
x1y2 = display.newCircle(px+xi*0,py+yi*1,r) x1y2.id = "x1y2"
x2y2 = display.newCircle(px+xi*1,py+yi*1,r) x2y2.id = "x2y2"
x3y2 = display.newCircle(px+xi*2,py+yi*1,r) x3y2.id = "x3y2"
x4y2 = display.newCircle(px+xi*3,py+yi*1,r) x4y2.id = "x4y2"
x5y2 = display.newCircle(px+xi*4,py+yi*1,r) x5y2.id = "x5y2"
x1y3 = display.newCircle(px+xi*0,py+yi*2,r) x1y3.id = "x1y3"
x2y3 = display.newCircle(px+xi*1,py+yi*2,r) x2y3.id = "x2y3"
x3y3 = display.newCircle(px+xi*2,py+yi*2,r) x3y3.id = "x3y3"
x4y3 = display.newCircle(px+xi*3,py+yi*2,r) x4y3.id = "x4y3"
x5y3 = display.newCircle(px+xi*4,py+yi*2,r) x5y3.id = "x5y3"
x1y4 = display.newCircle(px+xi*0,py+yi*3,r) x1y4.id = "x1y4"
x2y4 = display.newCircle(px+xi*1,py+yi*3,r) x2y4.id = "x2y4"
x3y4 = display.newCircle(px+xi*2,py+yi*3,r) x3y4.id = "x3y4"
x4y4 = display.newCircle(px+xi*3,py+yi*3,r) x4y4.id = "x4y4"
x5y4 = display.newCircle(px+xi*4,py+yi*3,r) x5y4.id = "x5y4"
x1y5 = display.newCircle(px+xi*0,py+yi*4,r) x1y5.id = "x1y5"
x2y5 = display.newCircle(px+xi*1,py+yi*4,r) x2y5.id = "x2y5"
x3y5 = display.newCircle(px+xi*2,py+yi*4,r) x3y5.id = "x3y5"
x4y5 = display.newCircle(px+xi*3,py+yi*4,r) x4y5.id = "x4y5"
x5y5 = display.newCircle(px+xi*4,py+yi*4,r) x5y5.id = "x5y5"
bb = {x1y1,x2y1,x3y1,x4y1,x5y1,x1y2,x2y2,x3y2,x4y2,x5y2,x1y3,x2y3,x3y3,x4y3,x5y3,x1y4,x2y4,x3y4,x4y4,x5y4,x1y5,x2y5,x3y5,x4y5,x5y5}
iClicked = 0
function click(e)
if(e.phase == "ended") then
--circleID = e.target.id
--whichCircle()
print(e.target.id)
obj = e.target
for u=1,25 do
if(obj==bb[u]) then
iClicked = u
end
end
if((iClicked-5) > 0 and (iClicked-5) < 26) then
bb[iClicked-5]:setFillColor(1,0,0)
end
if((iClicked-1) > 0 and (iClicked-1) < 26) then
bb[iClicked-1]:setFillColor(1,0,0)
end
obj:setFillColor(1,0,0)
if((iClicked+1) > 0 and (iClicked+1) < 26) then
bb[iClicked+1]:setFillColor(1,0,0)
end
if((iClicked+5) > 0 and (iClicked+5) < 26) then
bb[iClicked+5]:setFillColor(1,0,0)
end
end
end
for k=1,25 do
bb[k]:addEventListener("touch",click)
end
its all about having 25 circles and lighting them on and off but it doesnt seem to work for me
any good help will be great
Thanks
local myCircles = {}
for y = 1, 5 do
myCircles[y] = {}
for x = 1, 5 do
myCircles[y][x] = display.newCircle(px+xi*0,py+yi*4,r)
myCircles[y][x].id = .id = "x" .. x .. "y" .. y
end
end
or something like that.
Rob

Display objects (button widgets) aren't shown when they are added to the storyboard display group

I have a simple scene. There are three button widgets, and a title.
When I include them into the group, the objects aren't drawn. However, when I don't include them, they appear. But they won't go away when the scene is exited.
I've been reading about the storyboard module, and I'm sure that I'm using it the right way by including them into the group.
Why aren't the display objects drawn when they are included in the group?
I'll include my main.lua, just in case.
main.lua:
local storyboard = require("storyboard")
local mydata = require("mydata")
local widget = require( "widget" )
centerX = display.contentCenterX
centerY = display.contentCenterY
_W = display.contentWidth
_H = display.contentHeight
display.setStatusBar( display.HiddenStatusBar )
local bkg = display.newImage( "stripes.png", centerX, centerY )
local disclaimer = display.newText("DISCLAIMER", 300-75, centerY-175, 320, 0, "Helvetica", 30 )
local message = display.newText("The creators take no responsibility for\nany damage done by this app, etc.\n", 300-110, centerY-120, 320, 0, "Helvetica", 16)
local howtoTitle = display.newText("HOW TO PLAY", 300-85, centerY-50,320, 0, "Helvetica", 30)
local howto = display.newText("1. Select Time Frame\n 2.Select Sides\n 3.Have Fun", 300-60, centerY, 320, 0, "Helvetica", 16)
mydata.time = 0
mydata.sides = 0
mydata.hits = 0
-- Function to handle button events
local function handleButtonEvent( event )
local phase = event.phase
if "ended" == phase then
event.target:removeSelf()
disclaimer:removeSelf()
message:removeSelf()
howto:removeSelf()
howtoTitle:removeSelf()
storyboard.loadScene("time_select")
end
end
local playButton = widget.newButton {
left = 100,
top = 350,
width = 105,
height = 39,
defaultFile = "start.png",
overFile = "start_pressed.png",
label = "",
onEvent = handleButtonEvent,
}
time_select.lua:
local storyboard = require("storyboard")
local widget = require("widget")
local scene = storyboard.newScene()
local mydata = require("mydata")
local function fifteenSecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 15
storyboard.gotoScene("play")
end
end
local function thirtySecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 30
storyboard.gotoScene("play")
end
end
local function sixtySecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 60
storyboard.gotoScene("play")
end
end
function scene:createScene( event )
local group = self.view
local timeText = display.newText("TIME", 160, 70, "Helvetica", 30)
group:insert( timeText )
local fifteenButton = widget.newButton {
time = 15,
left = 75,
top = 150,
width = 164,
height = 42,
defaultFile = "fifteen_button.png",
overFile = "fifteen_button_pressed.png",
label = "",
onRelease = fifteenSecondButtonEvent
}
group:insert(fifteenButton)
local thirtyButton = widget.newButton {
time = 30,
left = 75,
top = 250,
width = 164,
height = 42,
defaultFile = "thirty_button.png",
overFile = "thirty_button_pressed.png",
label = "",
onRelease = thirtySecondButtonEvent
}
group:insert(thirtyButton)
local sixtyButton = widget.newButton {
time = 60,
left = 75,
top = 350,
width = 164,
height = 42,
defaultFile = "sixty_button.png",
overFile = "sixty_button_pressed.png",
label = "",
onRelease = sixtySecondButtonEvent
}
group:insert(sixtyButton)
print( "Number of children in Display Group: " .. group.numChildren )
end
function scene:willEnterScene( event )
local group = self.view
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
fifteenButton:removeEventListener( 'onRelease', fifteenSecondButtonEvent ) -- line 92
thirtyButton:removeEventListener( 'onRelease', thirtySecondButtonEvent )
sixtyButton:removeEventListener( 'onRelease', sixtySecondButtonEvent )
timeText:removeSelf()
timeText = nil
if fifteenButton then
fifteenButton:removeSelf()
fifteenButton = nil
end
if thirtyButton then
thirtyButton:removeSelf()
thirtyButton = nil
end
if sixtyButton then
sixtyButton:removeSelf()
sixtyButton = nil
end
display.remove(group)
storyboard.removeScene( "time_select" )
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener("createScene", scene)
scene:addEventListener("willEnterScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
Okay, so I found an example storyboard that worked on my build (2013.2100), and I played around with it to find my problem. It was the fact that I was drawing display objects in the main.lua file. It seems to be convention that the main.lua's only purpose is to direct you to the first scene. I broke up the previous two files into three separate files, and it all works the way that it's suppose to.
I believe the reason is also a part of #Schollii's answer:
Display objects that are not placed into a specific group become part of the stage
The background was part of the stage, so when it switched to the next scene, it was covered by the display objects on the stage.
#user3439409 I've heard that you must put the listener function inside of the createScene, like how I have it in the code below. As for the addEventListener, removeEventListener, etc. You shouldn't need to do addEventListener because widget does that for you. However, I heard that you should remove the event listener because it lingers in memory. You can check for memory leaks by adding a print statement about your memory. For that you can use storyboard.printMemUsage()
main.lua (now is just 3 lines):
display.setStatusBar( display.HiddenStatusBar )
local storyboard = require("storyboard")
storyboard.gotoScene( "start", "fade", 500 )
start.lua (what my main.lua had before):
local storyboard = require( "storyboard")
local scene = storyboard.newScene()
mydata = require("mydata")
widget = require( "widget" )
centerX = display.contentCenterX
centerY = display.contentCenterY
_W = display.contentWidth
_H = display.contentHeight
mydata.time = 0
mydata.sides = 0
mydata.hits = 0
function scene:createScene( event )
local group = self.view
local background = display.newImage( "stripes.png", centerX, centerY )
background.anchorX, background.anchorY = 0.0,0.0
background.x, background.y = 0, 0
group:insert( background )
local disclaimer = display.newText("DISCLAIMER", 300-75, centerY-175, 320, 0, "Helvetica", 30 )
group:insert( disclaimer )
local message = display.newText("The creators take no responsibility for\nany damage done by this app, etc.\n", 300-110, centerY-120, 320, 0, "Helvetica", 16)
group:insert( message )
local howtoTitle = display.newText("HOW TO PLAY", 300-85, centerY-50,320, 0, "Helvetica", 30)
group:insert( howtoTitle )
local howto = display.newText("1. Select Time Frame\n 2.Select Sides\n 3.Have Fun", 300-60, centerY, 320, 0, "Helvetica", 16)
group:insert( howto )
local playButton = nil
local function handleButtonEvent( event )
if "ended" == event.phase then
storyboard.gotoScene("time_select", "fade", 500)
end
return true
end
playButton = widget.newButton {
left = 100,
top = 350,
width = 105,
height = 39,
defaultFile = "start.png",
overFile = "start_pressed.png",
label = "",
onEvent = handleButtonEvent,
}
playButton.isActive = true
group:insert(playButton)
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
-- Function to handle button events
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
time_select.lua (I put the listener functions inside of the createScene):
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local widget = require "widget"
function scene:createScene( event )
local group = self.view
--local background = display.newImage("stripes.png", centerX, centerY)
--group:insert(backgroundz)
local background = display.newImage( "stripes.png", centerX, centerY )
background.anchorX, background.anchorY = 0.0,0.0
background.x, background.y = 0, 0
group:insert( background )
local timeText = display.newText("TIME", 0, 0, "Helvetica", 30)
timeText.x = _W/2
timeText.y = _H/2 - 150
timeText:setFillColor( 255, 255, 255 )
group:insert(timeText)
local fifteenButton = nil
local function fifteenSecondButtonEvent( event )
if "ended" == event.phase then
mydata.time = 15
storyboard.gotoScene("sides_selection")
end
return true
end
fifteenButton = widget.newButton {
left = 75,
top = 150,
width = 164,
height = 42,
defaultFile = "fifteen_button.png",
overFile = "fifteen_button_pressed.png",
label = "",
onRelease = fifteenSecondButtonEvent
}
fifteenButton.isActive = true
group:insert(fifteenButton)
local thirtyButton = nil
local function thirtySecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 30
storyboard.gotoScene("play")
end
end
thirtyButton = widget.newButton {
left = 75,
top = 250,
width = 164,
height = 42,
defaultFile = "thirty_button.png",
overFile = "thirty_button_pressed.png",
label = "",
onRelease = thirtySecondButtonEvent
}
thirtyButton.active = true
group:insert(thirtyButton)
local sixtySecondButtonEvent = nil
local function sixtySecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 60
storyboard.gotoScene("play")
end
end
sixtyButton = widget.newButton {
left = 75,
top = 350,
width = 164,
height = 42,
defaultFile = "sixty_button.png",
overFile = "sixty_button_pressed.png",
label = "",
onRelease = sixtySecondButtonEvent
}
group:insert(sixtyButton)
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
It is because you are using storyboard.loadScene("time_select"). This loads the scene but does not show it, so you don't see the objects that you add to its view group. When you don't add the objects to the view group, they do show up, because by default objects get put in the default "root level" group, called the stage. From Drawing Model section of Group Programming Guide:
Display objects that are not placed into a specific group become part of the stage
Use instead storyboard.gotoScene("time_select") so that your app transitions to the other scene (unless you meant for the time_select to be an overlay, then use storyboard.showOverlay).
Update: looks like there were two issues, as demonstrated by your own answer. I copied your fixed code to my simulator and executed it and sure enough, with loadScene() I couldn't see anything; I had to use gotoScene().
However, there are couple things that are incorrect in your answer:
the main.lua can create first scene: scene = storbyboard.newScene("scene1") then storyboard.gotoScene("uniqueName") and all the scene1 event handlers (createScene etc) can be in main.lua.
You can put the listener functions anywhere you want.
Listeners registered on objects are automatically unregistered when object calls removeSelf. Runtime is not a display object so for Runtime listeners like "enterFrame", you must explicitely removeEventHandler when exiting a scene.
You don't need to removeSelf objects in destroyScene, because this automatically done for you when scene.view is removed (automatic after destroyScene called).
You should not be calling removeScene('time_select') in time_select.exitScene, that makes no sense! the removeScene in exit will cause destroyScene to be called from exitScene! If you know you no longer need time_select scene, you would remove it from (for example) the main scene's enterScene. This will call time_select's destroyScene, and remove the scene.view group. The latter will remove every object in the group (which will unregister listeners of those objects), recursively.
These two points are demonstrated below: I moved your scene1.lua into main.lua and fixed main.lua accordingly; and I moved the handleButtonEvent out of the createScene.
main.lua:
local storyboard = require "storyboard"
local widget = require "widget"
local scene = storyboard.newScene("scene1")
local mydata = {} -- require("mydata")
local widget = require( "widget" )
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
mydata.time = 0
mydata.sides = 0
mydata.hits = 0
local function handleButtonEvent( event )
if "ended" == event.phase then
storyboard.gotoScene("time_select", "fade", 500)
end
return true
end
function scene:createScene( event )
local group = self.view
local background = display.newImage( "stripes.png", centerX, centerY )
background.anchorX, background.anchorY = 0.0,0.0
background.x, background.y = 0, 0
group:insert( background )
local disclaimer = display.newText("DISCLAIMER", 300-75, centerY-175, 320, 0, "Helvetica", 30 )
group:insert( disclaimer )
local message = display.newText("The creators take no responsibility for\nany damage done by this app, etc.\n", 300-110, centerY-120, 320, 0, "Helvetica", 16)
group:insert( message )
local howtoTitle = display.newText("HOW TO PLAY", 300-85, centerY-50,320, 0, "Helvetica", 30)
group:insert( howtoTitle )
local howto = display.newText("1. Select Time Frame\n 2.Select Sides\n 3.Have Fun", 300-60, centerY, 320, 0, "Helvetica", 16)
group:insert( howto )
local playButton = widget.newButton {
left = 100,
top = 350,
width = 105,
height = 39,
label = "Play",
onEvent = handleButtonEvent,
}
playButton.isActive = true
group:insert(playButton)
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
-- load first scene
storyboard.gotoScene( "scene1", "fade", 400 )
The time_select.lua`:
local storyboard = require("storyboard")
local widget = require("widget")
local scene = storyboard.newScene()
local mydata = {} -- require("mydata")
local function fifteenSecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 15
storyboard.gotoScene("play")
end
end
local function thirtySecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 30
storyboard.gotoScene("play")
end
end
local function sixtySecondButtonEvent( event )
local phase = event.phase
if "ended" == phase then
mydata.time = 60
storyboard.gotoScene("play")
end
end
function scene:createScene( event )
local group = self.view
local timeText = display.newText("TIME", 160, 70, "Helvetica", 30)
group:insert( timeText )
local fifteenButton = widget.newButton {
time = 15,
left = 75,
top = 150,
width = 164,
height = 42,
label = "15 Button",
onRelease = fifteenSecondButtonEvent
}
group:insert(fifteenButton)
local thirtyButton = widget.newButton {
time = 30,
left = 75,
top = 250,
width = 164,
height = 42,
label = "30 Button",
onRelease = thirtySecondButtonEvent
}
group:insert(thirtyButton)
local sixtyButton = widget.newButton {
time = 60,
left = 75,
top = 350,
width = 164,
height = 42,
label = "45 Button",
onRelease = sixtySecondButtonEvent
}
group:insert(sixtyButton)
print( "Number of children in Display Group: " .. group.numChildren )
end
function scene:willEnterScene( event )
local group = self.view
end
local demoNonObjListenerCount
local function enterFrame(event)
demoNonObjListenerCount = demoNonObjListenerCount + 1
end
function scene:enterScene( event )
local group = self.view
Runtime:addEventListener('enterFrame', enterFrame)
end
function scene:exitScene( event )
local group = self.view
Runtime:removeEventListener('enterFrame', enterFrame)
end
function scene:destroyScene( event )
local group = self.view
-- self.view will be removed upon return, which will
-- remove display objects contained, recursively, so
-- here not much else to do
end
scene:addEventListener("createScene", scene)
scene:addEventListener("willEnterScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
The contents of time_select.lua could even be put in main.lua if you replaced every occurence of "scene" by, say, "timeSelect" and used timeSelect = storyboard.newScene("time_select"). However, modularity is better, so better leave time_select scenario in its own file, but the scene1 does not have to be in separate file from main.lua.
If you are using story board then you need to call storyboard.removeScene() method for removing all the object from display group.
Here is the reference.
http://docs.coronalabs.com/api/library/storyboard/removeScene.html
http://docs.coronalabs.com/api/library/storyboard/

Remove animation at the end of the loop count

I'm using the following code to play explosion animation, How can I removeSelf the animation when it finished the loop-count?
function showExplotion(event)
local sheetData = { width=32, height=32, numFrames=13, sheetContentWidth=128, sheetContentHeight=128 }
local mySheet = graphics.newImageSheet( "media/fire.png", sheetData )
local sequenceData = {
--{ name = "normalRun", start=1, count=13, loopCount = 1, time=800 }
{ name = "fastRun", frames={ 1,2,4,5,6,7,8,9,10,11,12,13 }, time=800, loopCount = 1 }
}
local animation = display.newSprite( mySheet, sequenceData )
animation.x = event.x
animation.y = event.y
animation:play()
end
you can add listener to your sprite animation to detect it's phase
function showExplotion(event)
local sheetData = { width=32, height=32, numFrames=13, sheetContentWidth=128, sheetContentHeight=128 }
local mySheet = graphics.newImageSheet( "media/fire.png", sheetData )
local sequenceData = {
--{ name = "normalRun", start=1, count=13, loopCount = 1, time=800 }
{ name = "fastRun", frames={ 1,2,4,5,6,7,8,9,10,11,12,13 }, time=800, loopCount = 1 }
}
local animation = display.newSprite( mySheet, sequenceData )
animation.x = event.x
animation.y = event.y
animation:play()
local function mySpriteListener( event )
if ( event.phase == "ended" ) then
animation:removeSelf()
animation = nil
end
end
animation:addEventListener( "sprite", mySpriteListener )
end

Resources