Corona app's purple background in xCodeSimulator - lua

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

Related

how to count how many times I clicked my image button * LUA *

I've created hello world text and a clickable button. When I click the button the hello world jumps around randomly.
The problem: I don't know how to get my program to count how many times i've clicked the button.
My progress:
local myHeight = display.contentHeight
local myWidth = display.contentWidth
local topRightHeight = display.newText ("Height "..myHeight, 300 , 40 ,nil,15)
local topRightWidth = display.newText ("Width "..myWidth, 300,60,nil,15)
local redButton = display.newImage ("button.png",0,0)
redButton.x = display.contentWidth -60
redButton.y = display.contentHeight -62.5
local textObj = display.newText ("Hello World",0,0,native.systemFont,18)
textObj: setFillColor(0,250,0)
textObj.x = 40
textObj.y = 30
local number = 0
number = display.newText (number, 30, 30 , native.systemFont, 25)
function moveButtonRandom (event)
textObj.x = math.random(50, display.contentWidth -50)
textObj.y = math.random(50, display.contentHeight -50)
end
redButton: addEventListener ("tap", moveButtonRandom)
You can use flag of int type to count the number of times button clicked.
Initially set flag=0 and if button is clicked then inside onclick() method use flag=flag+1.
Now it works like a charm
display.setStatusBar(display.HiddenStatusBar)
local redButton = display.newImage ("button.png",0,0)
redButton.x = display.contentWidth - 60
redButton.y = display.contentHeight - 62.5
local textObj = display.newText("Hello World", 0, 0, native.systemFont, 18)
textObj:setFillColor(0, 250, 0)
textObj.x = 40
textObj.y = 30
local number = 0
local textField = display.newText(number, 30, 30, native.systemFont, 25)
local function moveButtonRandom(event)
textObj.x = math.random(50, display.contentWidth - 50)
textObj.y = math.random(50, display.contentHeight - 50)
number = number + 1
textField:removeSelf()
textField = display.newText(number, 30, 30, native.systemFont, 25)
end
redButton:addEventListener("tap", moveButtonRandom)
You can track the click on monitoring some flag in your clickListener for button. Maintain a flag increment it whenever you clicked on your button through clickListener. Some thing like this
intialize a variable let say int flagForButton = 0 in your activity
OnClickListener clickListener1 = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
flagForButton++;
Toast.makeText(MarketSnap.this, String.valueOf(flagForButton), Toast.LENGTH_SHORT).show();
}
};
Set the clickListener on the button. Like this
yourbutton.setOnClickListener(clickListener1);
EDIT
While answering this question the question was tagged with Android. So i have put the code as above. But Now the question is edited and tagged as LUA. So the logic remain same you can increment the flagcount in clickListener Of the the button.
Update text number on every click. No need to delete it and create it again.
local number = 0
local textField = display.newText(number, 30, 30, native.systemFont, 25)
local function updateNumber(n)
textField.text = n
end
local function moveButtonRandom(event)
textObj.x = math.random(50, display.contentWidth - 50)
textObj.y = math.random(50, display.contentHeight - 50)
number = number + 1
UpdateNumber(number)
end
redButton:addEventListener("tap", moveButtonRandom)

Connecting my tab buttons to pages

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.

I can't see any of the buttons, or the background (Corona SDK)

I'm trying to make a game for the Android Google Play store. But, when I was making the main menu I ran into an issue.
Before I added the storyboard and the scene functions, everything worked fine. But now I can't see a thing when I run it.
Also, I get no errors.
-- Requires
local widget = require "widget"
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
function scene:createScene(event)
screenGroup = self.view
-- Background
local background = display.newImage("images/bg.png")
screenGroup:insert(background)
-- Title
local title = display.newImage("images/title.png")
title.x = display.contentCenterX
title.y = display.contentCenterY - 110
screenGroup:insert(title)
-- Play game
local button1 = widget.newButton {
label = "Play Game",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button1.x = display.contentCenterX
button1.y = display.contentCenterY - 47
screenGroup:insert(button1)
-- How To Play
local button2 = widget.newButton {
label = "How To Play",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button2.x = display.contentCenterX
button2.y = display.contentCenterY + 13
screenGroup:insert(button2)
-- Level Select
local button3 = widget.newButton {
label = "Level Select",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button3.x = display.contentCenterX
button3.y = display.contentCenterY + 73
screenGroup:insert(button3)
-- About Us
local button4 = widget.newButton {
label = "About Us",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button4.x = display.contentCenterX
button4.y = display.contentCenterY + 133
screenGroup:insert(button4)
end
function start(event)
if event.phase == "began" then
storyboard.gotoScene("level1", "fade", 400)
end
end
function scene:enterScene(event)
button1:addEventListener("touch", start)
end
return scene
Try this:
main.lua
local storyboard = require "storyboard"
storyboard.gotoScene( "scene1")
scene1.lua
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
function scene:createScene( event )
local screenGroup = self.view
-- Background
local background = display.newImage("images/bg.png")
screenGroup:insert(background)
-- Title
local title = display.newImage("images/title.png")
title.x = display.contentCenterX
title.y = display.contentCenterY - 110
screenGroup:insert(title)
-- Play game
local button1 = widget.newButton {
label = "Play Game",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button1.x = display.contentCenterX
button1.y = display.contentCenterY - 47
screenGroup:insert(button1)
-- How To Play
local button2 = widget.newButton {
label = "How To Play",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button2.x = display.contentCenterX
button2.y = display.contentCenterY + 13
screenGroup:insert(button2)
-- Level Select
local button3 = widget.newButton {
label = "Level Select",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button3.x = display.contentCenterX
button3.y = display.contentCenterY + 73
screenGroup:insert(button3)
-- About Us
local button4 = widget.newButton {
label = "About Us",
font = default,
fontSize = 24,
width = 200,
height = 50
}
button4.x = display.contentCenterX
button4.y = display.contentCenterY + 133
screenGroup:insert(button4)
end
function start(event)
if event.phase == "began" then
storyboard.gotoScene("level1", "fade", 400)
end
end
function scene:enterScene(event)
button1:addEventListener("touch", start)
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
return scene
I think you should insert background after inserting all the objects or you can also insert all the objects on the background which you have added on the screenGroup. May be background image is hiding all the objects.
Try above solution, if it will not help you. Please let me know what exactly is the problem after using my solution.
Good luck!

In onButtonEvent function t.value not updating

I am designing an Odd One Out round for a game that I am developing, every time a button is pressed, the onButtonEvent function is supposed to check if the word selected is the odd one out and then refreshes the buttons with different words.
The words are updating and the value assigned to each button is updating, I have been printing it out to the console. I think the problem is when I assign right[num] = t.value the only values that go into the table are the first 3 values that were originally assigned to the buttons. Why is this not being updated?
local function onButtonEvent( event )
local t = event.target
--local phase = event.phase
if event.phase == "release" then
--num = num + 1
displayBtns()
right[num] = t.value
print(right[1])
print(right[2])
print(right[3])
print(right[4])
-- --s3 = table.concat(right)
--print(s3)
if right[num] == answers[num].right then
print("correct!")
elseif right[num] == answers[num].wrong then
-- t.alpha = 0
-- select.alpha = 1
print("incorrect!")
elseif right[num] == answers[num].wrong2 then
else
print("incorrect!")
end
num = num + 1
num2 = num2 + 1
s3 = ""
-- display.remove( btn3 )
-- btn3 = nil
-- display.remove( btn2 )
-- btn2 = nil
-- display.remove( btn1 )
-- btn1 = nil
end
--num = math.random(1,#t)
s3 = ""
end
function displayBtns()
btn3 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].right,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn3.value = answers[num].right
print(btn3.value)
btn2 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].wrong2,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn2.value = answers[num].wrong2
print(btn2.value)
btn1 = widget.newButton{
default = "images/Round4_blue_button.png",
label = answers[num].wrong,
fontSize = 22,
labelColor = { default={ 255, 250, 250}, over={ 0, 0, 0,} },
onEvent = onButtonEvent
}
btn1.value = answers[num].wrong
print(btn1.value)
print("----------")
--btn3.label = answers[num].right
-- p1Button = display.newImage("images/SinglePlayer_button.png", 90, 140)
-- p2Button = display.newImage("images/2Player_button.png", 90, 220)
-- p1Button.touch = onSceneTouch
-- p1Button:addEventListener("touch", p1Button)
-- p2Button.touch = onSceneTouch1
-- p2Button:addEventListener("touch", p2Button)
btn1.x = 90; btn1.y = 245
btn2.x = 240; btn2.y = 245
btn3.x = 390; btn3.y = 245
-- p2Button.x = 240; p2Button.y = 260
end
displayBtns()
The problem is probably here:
elseif right[num] == answers[num].wrong2 then
else
print("incorrect!")
end
You have nothing set in the elseif statement.

how to go back in lua file from another lua file?

I have a menu lua and when i choose 1 of the category (the "songselect" lua) and go back to the menu lua i got this error:
Runtime error
...ocuments\corona projects\singingbeemenu\director.lua:151: attempt to
call field 'unloadMe' (a nil value)
stack traceback:
[C]: in function 'unloadMe'
...ocuments\corona projects\singingbeemenu\director.lua:151: in function
'_listener
'
this is 1 of my category the songselect.lua
module(..., package.seeall)
display.setStatusBar( display.HiddenStatusBar )
function new()
local localGroup = display.newGroup()
local tableView = require("tableView")
local ui = require("ui")
--------------------------------------------------------------------------
local screenOffsetW, screenOffsetH = display.contentWidth - display.viewableContentWidth, display.contentHeight - display.viewableContentHeight
local songList
local backBtn
local detailScreenText
local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(0, 0, 0)
local data = {}
--iPad: setup a color fill for selected items
local selected = display.newRect(0, 0, 50, 50) --add acolor fill to show the selected item
selected:setFillColor(67,141,241,180) --set the color fill to light blue
selected.isVisible = false --hide color fill until neede
-----------------------------------------------
data[1] = {}
data[1].title = "Crazy Song"
data[1].subtitle = "by Bruno Earth"
data[1].image = "note.png"
data[2] = {}
data[2].title = "Enter Sunman"
data[2].subtitle = "by Mentalica"
data[2].image = "note.png"
local topBoundary = display.screenOriginY + 40
local bottomBoundary = display.screenOriginY + 0
songList = tableView.newList{
data=data,
default="listItemBg.png",
over="listItemBg_over.png",
top=topBoundary,
bottom=bottomBoundary,
callback = function( row )
local g = display.newGroup()
local img = display.newImage(row.image)
g:insert(img)
img.x = math.floor(img.width*0.5 + 6)
img.y = math.floor(img.height*0.5)
local title = display.newText( row.title, 0, 0, native.systemFontBold, 16 )
title:setTextColor(0,0,0)
g:insert(title)
title.x = title.width*0.5 + img.width + 6
title.y = 30
local subtitle = display.newText( row.subtitle, 0, 0, native.systemFont, 14 )
subtitle:setTextColor(80,80,90)
g:insert(subtitle)
subtitle.x = subtitle.width*0.5 + img.width + 6
subtitle.y = title.y + title.height + 6
return g
end
}
local function scrollToTop()
songList:scrollTo(topBoundary-1)
end
local navBar = display.newImage("navBar.png")
navBar.x = display.contentWidth*.5
navBar.y = math.floor(display.screenOriginY + navBar.height*0.5)
local navHeader = display.newText("Song Lists", 0, 0, native.systemFontBold, 16)
navHeader:setTextColor(255, 255, 255)
navHeader.x = display.contentWidth*.5
navHeader.y = navBar.y
--Setup the back button
local backToMenu = function(event)
print (event.phase)
if event.phase == 'ended' then
print ("ok")
director:changeScene(event.target.scene, "fade")
print ("back!")
end
end
backBtn = display.newImage("backButton.png")
backBtn.x = math.floor(backBtn.width/2) + backBtn.width + screenOffsetW
backBtn.y = navBar.y
backBtn.scene = "menu"
backBtn:addEventListener("touch", backToMenu)
--backBtn.alpha = 0
local listBackground = display.newRect( 0, 0, songList.width, songList.height )
listBackground:setFillColor(255,255,255)
songList:insert(1,listBackground)
return localGroup
end
is it impossible the go back method in lua?
can anyone can help me and give an idea why i got the error?
thanks in advance...
I got this, i change my director.lua to the latest version...
Try this Lua file to solve your problem.

Resources