I am trying to create a mobile application about healthcare. I have to create a survey like application to screen the patients. Right now i have completed a part of the survey using radio button, the other part also look something like this(see picture below) but i want the radio button change to numeric textbox. My application look something like this:
Right now I want to convert the radio button to a numeric textbox.
The question is: how can i make minimum code changes and replace all the radio button to numeric textbox?
Here is my code for the radio buttons:
local radioGroup = display.newGroup()
if(ebasRating_Arr[i] == 0) then
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = true,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = false,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
elseif (ebasRating_Arr[i] == 1) then
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = false,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = true,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
else
radioButton_0 = widget.newSwitch {
left = 565,
style = "radio",
initialSwitchState = false,
id = tostring(i),
width = 60,
height = 60,
onPress = setEBASRating0
}
radioGroup:insert( radioButton_0 )
radioButton_1 = widget.newSwitch {
style = "radio",
id = tostring(i),
initialSwitchState = false,
width = 60,
height = 60,
onPress = setEBASRating1
}
radioGroup:insert( radioButton_1 )
end
radioButton_0.y = 150 + (i * 450)
radioButton_1.x = 18 + radioButton_0.x+radioButton_0.width
radioButton_1.y = 150 + (i * 450)
scrollView:insert( radioGroup )
end
I'm building a game where I have a start screen and clicking the start button it should take me to the FIRST level. Some stuff happens and then I will be taken to the SECOND level.
However I'm running into problems and can't seem to find a solution.
When I click the start button it seems to get stuck in the create function because i prints out something. I was told you don't have to place anything in the create function and just put everything in the SHOW FUNCTION. Was i mislead?
START SCENE
local composer = require("composer")
local widget = require("widget")
local options = {effect = "fade", time = 800}
local startBtn;
local function start(event)
-- load first scene
composer.gotoScene( "level1", options);
startBtn:removeSelf();
print("Start Game")
end
startBtn = widget.newButton(
{
left = 75,
top = 100,
id = "startBtn",
label = "Start",
onEvent = start
}
)
When I click the START button it should take me to the FIRST LEVEL which is here
and is where i'm running into problems.
local composer = require( "composer" );
local scene = composer.newScene();
local widget = require ("widget");
function scene:create( event )
local sceneGroup = self.view;
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
local params = event.params;
print(params);
if (phase == "will") then
print("Will")
elseif (phase == "did") then
print("Did")
local bg = display.newImage ("bg.png",
display.contentCenterX, display.contentCenterY);
------- ALEX KIDD ---------------------------------
local options =
{
frames = {
{ x = 1, y = 2, width = 16, height = 25}, --frame 1
{ x = 18, y = 2, width = 16, height = 25}, --frame 2
{ x = 35, y = 2, width = 16, height = 25}, --frame 3
{ x = 52, y = 2, width = 16, height = 25}, --frame 4
{ x = 1, y = 54, width = 16, height = 24}, --ready1
{ x = 19, y = 54, width = 16, height = 24}, --ready2
{ x = 37, y = 54, width = 29, height = 24}, -- rock
{ x = 67, y = 54, width = 33, height = 24}, -- scissor
{ x = 101, y = 54, width = 33, height = 24}, -- paper
}
};
local sheet = graphics.newImageSheet( "kidd.png", options );
-- Create animation sequence for animation
local seqData = {
{name = "normal", start=1 , count = 4, time = 800},
{name = "faster", frames={1,2,3,4}, time = 400},
{name = "shake", frames={5,6}, time = 500},
{name = "rock", frames={7}},
{name = "paper", frames={9}},
{name = "scissor", frames={8}},
}
local alex = display.newSprite (sheet, seqData);
alex.x = display.contentCenterX-100;
alex.y = display.contentCenterY+83;
alex.anchorX = 1;
alex.anchorY = 1;
---------- JANKEN ---------------------------------
local jankenOpt =
{
frames = {
{x= 154, y= 13, width= 39, height= 48 }, -- shake1
{x= 195, y= 13, width= 39, height= 48 }, -- shake2
{x= 236, y= 13, width= 32, height= 48 }, -- set
{x= 270, y= 13, width= 16, height= 48 }, --r/p/s
{x= 287, y= 13, width= 16, height= 48 }, --r/p/s
{x= 305, y= 13, width= 15, height= 48 }, --r/p/s
{x= 69, y= 13, width= 41, height= 48 }, --flap1
{x= 110, y= 13, width= 40, height= 48 }, --flap2
}
};
local jankenSheet = graphics.newImageSheet( "chars.png", jankenOpt );
-- Create animation sequence janken
local seqDataJanken = {
{name = "flap", frames={7,8}, time = 500},
{name = "shake", frames={1,2}, time = 500},
{name = "set", frames={3}, time = 10, loopCount=1},
}
local janken = display.newSprite (jankenSheet, seqDataJanken);
janken.x = display.contentCenterX+100;
janken.y = display.contentCenterY+83;
janken.anchorX = 1;
janken.anchorY = 1;
-------------- button setup
local btnOpt =
{
frames = {
{ x = 3, y = 2, width=70, height = 22}, --frame 1
{ x = 78, y = 2, width=70, height = 22}, --frame 2
}
};
local buttonSheet = graphics.newImageSheet( "button.png", btnOpt );
local function foo (wow)
alex:play();
janken:play();
end
local function bar (wow)
alex:pause();
janken:pause();
--alex:setFrame(2); --keep it at this frame
-- NEXT: generate more buttons
end
-- Function to handle button event
local function go( event )
-- event.target is the button widget
print (event.phase, event.target.id)
transition.to(alex, {time=2000, x=170, onStart = foo, onComplete = bar})
end
local btnGo = widget.newButton(
{
x = 200,
y = 20,
id = "btnGo",
label = "Go!",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = go,
}
);
-------------- play buttons
local function shoot (buttonID)
local randomHand = math.random(4,6);
-- position Janken and draw his hands
janken:setSequence("set");
hand = display.newImage (jankenSheet, randomHand, display.contentCenterX+61, display.contentCenterY+60);
if (buttonID == "btnRock") then
alex:setSequence("rock"); -- just show rock for now
elseif (buttonID == "btnScissor") then
alex:setSequence("scissor"); -- just show rock for now
else
alex:setSequence("paper"); -- just show rock for now
end
end
local function play (event)
if (event.phase == "ended") then
alex:setSequence ("shake");
alex:play();
janken:setSequence("shake");
janken:play();
local t = timer.performWithDelay (1500, function() shoot(event.target.id) end, 1);
print (event.target.id); -- which button was it?
end
end
local btnRock = widget.newButton(
{
x = 80,
y = 20,
id = "btnRock",
label = "Rock",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local btnPaper = widget.newButton(
{
x = 80,
y = 50,
id = "btnPaper",
label = "Paper",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local btnScissor = widget.newButton(
{
x = 80,
y = 80,
id = "btnScissor",
label = "Scissor",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onEvent = play,
}
);
local scoreAlex = display.newText ( {text="Alex: 0", x=230, y=60, fontSize=20});
scoreAlex:setFillColor (0,0,0);
scoreAlex.anchorX = 1;
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
transition.cancel(enemy);
elseif ( phase == "did" ) then
end
end
scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )
scene:addEventListener( "hide", scene )
return scene
Well, you are adding a listener scene:addEventListener( "enter", scene ) which should be scene:addEventListener( "show", scene ) (Change enter to show)
Your listener is not even being fired, because it was not added with the correct name.
Composer uses the following events: create, destroy, show, hide.
I am new to corona, all the work is from a scratch of others. Let see, I have 3 images that entitled shop1price shop2price shop3price. Now I want it to be simplified as the code below
local options =
{
{ defaultFile = 'images/shop1price.png' },
{ defaultFile = 'images/shop2price.png' },
{ defaultFile = 'images/shop3price.png' },
}
local priceTag = {}
for i = 1,3 do
priceTag[i] = widget.newButton{
options[i],
overColor = {128,128,128,255},
width = 73,
height = 38,
left = (centerX-155) + (i-1)*118,
top = centerY * 0.88,
id = i,
onEvent = function (e)
if e.phase == 'ended' then
onTouchBuy(e.target.id)
end
return true
end
}
-- priceTag[i] : setReferencePoint( display.CenterReferencePoint )
priceTag[i] : scale( 0.8 , 0.8 )
buttonGroup : insert( priceTag[i] )
end
But the button does not appear, I think the wrong is in options[i]. But the problem always is I don't know how the right. I know I can make the code themselves one by one, but it is certainly very tiring. What if I have for example of 100 buttons.
Any help would be appreciated.
local options = {}
[#options+1] = 'images/shop1price.png'
[#options+1] = 'images/shop2price.png'
[#options+1] = 'images/shop3price.png'
local priceTag = {}
for i = 1,#options do
priceTag[i] = widget.newButton{
defaultFile = options[i],
overColor = {128,128,128,255},
width = 73,
height = 38,
left = (centerX-155) + (i-1)*118,
top = centerY*0.88,
id = i,
onEvent = function (e)
if e.phase == 'ended' then
onTouchBuy(e.target.id)
end
return true
end
}
-- priceTag[i] : setReferencePoint( display.CenterReferencePoint )
priceTag[i] : scale( 0.8 , 0.8 )
buttonGroup : insert( priceTag[i] )
end
Try this , should work fine.
I have a corona app, with two storyboard scenes (scene1.lua, addDesire.lua).
I show addDesire.lua as an overlay:
function onAddPurchase( event )
if event.phase == "ended" then
local options = {
effect = "fromBottom",
time = 500,
isModal = true,
}
storyboard.showOverlay( "addDesire", options )
end
end
In Corona Simulator everything works, but in xCode simulator a pink background appears in several cases.
1) When addDesire.lua appears after onAddPurchase it looks like:
When it should look like:
2) When I close addDesire.lua (tapping on Cancel button) this appears:
There is something even stranger going on under the hood:
addDesire.lua has 2 textFields and 1 textBox which is created in function scene:createScene( event ). If I comment out the code that creates these objects everything works perfectly.
function scene:createScene( event )
local group = self.view
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local bg = display.newImageRect( "assets/dollar.png", 360, 570 )
bg:toBack()
bg.x, bg.y = _W/2, _H/2
bg:addEventListener( "tap", function() native.setKeyboardFocus(nil); end)
group:insert(bg)
-- Rounded Rect Alpha
roundedRect = display.newRoundedRect( 5, 5, _W*0.9, _H*0.8, 10 )
roundedRect.x, roundedRect.y = centerX, centerY
roundedRect:setFillColor( 0/255, 0/255, 0/255, 170/255 )
group:insert(roundedRect)
-- Label Title
titleLabel = display.newText( "Purchase", 0, 0, "AmericanTypewriter-Bold", 20 )
titleLabel.x, titleLabel.y = centerX, _H*0.15
group:insert(titleLabel)
-- Label Fam
nameLabel = display.newText( "Name", 0, 0, "AmericanTypewriter", 18 )
nameLabel.x, nameLabel.y = centerX, _H*0.20
group:insert(nameLabel)
-- Edit Fam
nameText = native.newTextField(_W/2, _H*0.26, 240, 30)
nameText.font = native.newFont(native.systemFont, 18)
nameText:addEventListener( "userInput", inputListener )
group:insert(nameText)
-- Label Name
descriptionLabel = display.newText( "Description", 0, 0, "AmericanTypewriter", 16 )
descriptionLabel.x, descriptionLabel.y = centerX, _H*0.31
group:insert(descriptionLabel)
-- Edit Name
descriptionText = native.newTextBox(_W/2, _H*0.44, 240, 100)
descriptionText.font = native.newFont(native.systemFont, 14)
descriptionText.isEditable = true
descriptionText:addEventListener( "userInput", inputListener )
group:insert(descriptionText)
-- Label Deposit
costLabel = display.newText( "Cost", 0, 0, "AmericanTypewriter", 16 )
costLabel.x, costLabel.y = centerX, _H*0.57
group:insert(costLabel)
-- Edit Deposit
costText = native.newTextField(_W/2, _H*0.64, 240, 30)
costText.font = native.newFont(native.systemFont, 18)
costText.inputType = "number"
costText.align = "center"
costText:addEventListener( "userInput", inputListener )
group:insert(costText)
-- Button Save & Start game
btnSave = widget.newButton {
width = _W*0.4,
height = 50,
--defaultFile = "buttonDefault.png",
--overFile = "buttonOver.png",
label = "Buy",
onEvent = onSaveData
}
btnSave.x, btnSave.y = _W/2, costLabel.y+150
group:insert(btnSave)
-- Button Cancel
btnCancel = widget.newButton {
width = _W*0.8,
height = 50,
--defaultFile = "buttonDefault.png",
--overFile = "buttonOver.png",
label = "Cancel",
onEvent = onExit,
}
btnCancel.x, btnCancel.y = _W/2, btnSave.y+50
group:insert(btnCancel)
end
What is going on?
The answer is here guys: native.textField
I'm creating a basic quiz game in corona for a class and am having troubles getting my tab bar buttons to connect to the other pages that have been created for them. I was just wondering if anyone out there could help me out.
main.lua
local background = display.newImage ("basketball_court.jpg")
print("PE Sports Quiz")
display.setStatusBar( display.HiddenStatusBar )
local textObj = display.newText("PE Sports Quiz",
100, 50, m11, 24)
textObj:setTextColor(250,250,250)
local widget = require ( "widget" )
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local localGroup = display.newGroup()
local tabButtons =
{
{
width = 32, height = 32,
defaultFile = "tab.png",
overFile = "tab2.png",
label = "Play",
onPress = function() storyboard.gotoScene( "basketball1" ); end,
selected = true
},
{
width = 32, height = 32,
defaultFile = "tab.png",
overFile = "tab2.png",
label = "Credits",
onPress = function() storyboard.gotoScene( "credits" ); end,
}
}
local demoTabs = widget.newTabBar
{
top = display.contentHeight - 50,
width = display.contentWidth,
backgroundFile = "back.png",
tabSelectedLeftFile = "tab.png",
tabSelectedMiddleFile = "tab.png",
tabSelectedRightFile = "tab.png",
tabSelectedFrameWidth = 20,
tabSelectedFrameHeight = 52,
buttons = tabButtons
}
local tabBar = widget.newTabBar
{
top = display.contentHeight - 50,
width = display.contentWidth,
buttons = tabButtons
}
The only thing I see is that you should rename your credits.lua to gamecredits.lua and change the gotoScene() call for it. There is an internal module called "credits" that can cause issues.