Corona SDK - display.newtext with options table returns error - textbox

my code is:
local options =
{
--parent = textGroup,
text = "Hello World",
x = 100,
y = 200,
width = 128, --required for multi-line and alignment
font = native.systemFontBold,
fontSize = 18,
align = "right" --new alignment parameter
}
local test = display.newText(options)
But it returns:
bad argument #-1 to 'newText' (Proxy expected, got nil)
Does anybody know why this happens?
Thank you

Maybe you are using old Corona SDK version which does not have newText with options table?

The code that you put it is fine. your error, maybe is on another line. look this post here
it is similar to error that you have. However It is a good information.
I hope that this help you.

Related

Jetpack Compose: Find how many lines a text will take before composition

I am trying to determine how many lines a certain text will occupy on the screen before composition. Is there a way to do this?
You can use onTextLayout on Text to get line count and some other features.
var lineCount = 1
Text(text= "", onTextLayout = {textLayoutResult: TextLayoutResult ->
lineCount = textLayoutResult.lineCount
})
While the accepted answer is correct there is an alternative way, which doesn't even require a Composable function:
val paragraph = androidx.compose.ui.text.Paragraph(
text = "Foo",
style = MaterialTheme.typography.body1,
constraints = Constraints(maxWidth = maxWidthInPx),
density = LocalDensity.current,
fontFamilyResolver = LocalFontFamilyResolver.current,
)
paragraph.lineCount
This might be better suited if it's required to know the lineCount beforehand.

How do I fix a nil value error produced when calling in a widget method in Corona SDK?

How do I fix a nil value error produced when calling in a widget method in Corona SDK?
local widget = require('widget')
-- Text Box
local myTextField = widget.newTextField(
{
top = 10,
left = 20,
width = 200,
height = 30,
cornerRadius = 8,
strokeWidth = 3,
backgroundColor = {1,1,1},
strokeColor = {0,0,0},
font = 'Helvetica',
fontsize = 24,
listener = textFieldHandler
}
)
According to the documentation widget only provides the following function:
https://docs.coronalabs.com/api/library/widget/
widget.newButton()
widget.newPickerWheel()
widget.newProgressView()
widget.newScrollView()
widget.newSegmentedControl()
widget.newSlider()
widget.newSpinner()
widget.newStepper()
widget.newSwitch()
widget.newTabBar()
widget.newTableView()
widget.setTheme()
You can avoid the error message, which I assume is something like Error: Attempt to calla nil value, by simply not calling the nil value widget.newTextField.
Another option is to implement it yourself as described here in detail:
https://coronalabs.com/blog/2013/12/03/tutorial-customizing-text-input/
As this tutorial exactly matches your code I assume you were just to lazy to read beyond the first paragraph...

Corona SDK display.remove() in lua

this code (lua) gives you a random value from the table "local a" by pressing the text "new". Unfortunately the new random value just appears above the old one. I've tried to remove the old value e.g. with display.remove(mmDis), but it doesn't work.
The second problem is that sometimes I also get back the value "nil" and not only the four entries from the table.
Both things must be easy to solve, but as newbie to lua and working on these small things for almost 4 hours now I just don't get what to change to make it work.
-- references
local mmDis
-- functions
function randomText(event)
display.remove(mmDis)
local a = {"Banana!","Apple!","Potato","Pie"}
com = (a[math.random(0.5,#a)])
local mmDis = display.newText(tostring(com),
display.contentWidth*0.57, display.contentHeight*0.7,
display.contentWidth*0.9, display.contentHeight*0.8, "Calibri", 60)
end
-- menu button
local textnew = display.newText("New", 0, 0, "Calibri", 40)
textnew.x = display.contentWidth*0.2
textnew.y = display.contentHeight*0.9
textnew:addEventListener ("tap", randomText )
It's hard to understand what you are trying to do because when you create textnew you have it in one position and size, while in randomeText() you seem to want to replace that text object with a new one, but you put it at a different pos and size. It appears you want to change the object's text every time you press on tap; in this case, you don't need to replace the text object, you just replace its text.
Also:
you have two "local mmDis", the second one will hide the first, not sure what you're after there
read carefully the docs for math.random
com is already a string so you don't need tostring
Try this code and let me know if this is not what you are after:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
textnew.text = com
-- if you want to change position too:
-- textnew.x = display.contentWidth*0.2
-- textnew.y = display.contentHeight*0.9
-- if you want to change size too, but only used for multiline text:
-- textnew.width = display.contentWidth*0.9
-- textnew.height = display.contentHeight*0.8
end
textnew:addEventListener ("tap", randomText )
Sometimes it looks like the button doesn't do anything when you tap but that's because the random number happens to be same as previous, you could put a loop to guard against that. Ig you actually want to change the pos and/or width, then the above code makes it clear where you should do that.
So, that's the basic code (without any extras at the moment :)) how it should be. Big thanks to Scholli!:
local menuTextOptions = {
text = "New",
x = display.contentWidth*0.2,
y = display.contentHeight*0.9,
align = 'left',
font = "Calibri",
fontSize = 40,
}
local textnew = display.newText(menuTextOptions)
local replacement = display.newText(menuTextOptions)
replacement.y = display.contentHeight*0.5
-- functions
function randomText(event)
local a = {"Banana!","Apple!","Potato","Pie"}
local com = a[math.random(1,#a)]
replacement.text = com
end
textnew:addEventListener ("tap", randomText )

How to hide native.newTextField after clicking submit?

I'm trying trying to have users name fade in one letter at a time vertically. Example: Adam "A" would appear after 1 second , "d" would appear after 3 seconds under the displayed A, "a" would appear after 5 seconds under the displayed d, "m" would appear after 7 seconds under the displayed a. The visuals would have a sort of domino effect.When they appear they would stay displayed on screen.
When if I comment out userNameField:removeSelf () then the code works fine. I get the effect I want, but the problem is that I still have the userNamefield showing.
Please let me know if you need to see more code.
local greeting = display.newText( "Greetings, enter your name",0,0,native.systemFont, 20 )
greeting.x = display.contentWidth/2
greeting.y = 100
local submitButton = display.newImage( "submit.png" ,display.contentWidth/2, display.contentHeight - 50 )
local userNameField = native.newTextField( display.contentWidth * 0.5 , 150, 250, 45)
userNameField.inputtype = "defualt"
local incrementor = 0
function showNameLetters()
userNameField:removeSelf( )
if incrementor <= string.len ("userNameField.text") then
incrementor = incrementor + 1
personLetters = string.sub (userNameField.text, incrementor,incrementor)
display_personLetters = display.newText (personLetters, 290,30*incrementor, native.systemFont, 30)
display_personLetters.alpha = 0
transition.to(display_personLetters,{time = 3000, alpha = 1, onComplete = showNameLetters})
end
end
Update:
I've found a solution to my problem, by adding userNameField.isVisible = false in my function.
I've also found something very weird, and wish to have someone explain why this happens. If I add greeting:removeSelf() and submitButton:removeSelf() (I've commented them out in my code below to show you where I put them for testing). I get weird result of only the first letter fading in. If i set greeting.isVisible = false and submitButton.isVisible = false. The code works fine.
I'm so confused why object:removeSelf() wouldn't work. Can someone please clear this up for me.
That is, if I replace the following line:
userNameField:removeSelf( )
with:
userNameField.isVisible = false
then the app works fine. Please suggest me why/ any solution for the question. THanks in advance...
It seems like you're calling the showNameLetters multiple times, this means that you're removing the native text field more than once. Nil it and check for nil before removing it like this:
if userNameField ~= nil then
userNameField:removeSelf()
userNameField = nil
end
It shows that you are using data from userNameField after removing it (in the line below):
personLetters = string.sub (userNameField.text, incrementor,incrementor)
As well as you are calling object:removeSelf() again and again without checking its existance (as mentioned by hades2510). So before removing userNameField, check for it's existance:
if userNameField ~= nil then
userNameField:removeSelf()
userNameField = nil
end
And you won't get userNameField.text while userNameField is nil. So use a temporary variable to hold the previous userNameField.text and get the saved data from that variable when needed.
Extra note: Are you sure, you have to check the length of the text "userNameField.text" in the following line, or the variable userNameField.text? If you have to use the data from the text field, then this will also matter.
if incrementor <= string.len ("userNameField.text") then
........
end
Keep coding....................... :)

Corona always display a black screen in the build

I'm trying to make a build with Corona for Android devices.
Is a very simple app, only use a reference to de 'ui'
local ui = require("ui");
local btnright = ui.newButton{
defaultSrc = "button-right.png",
defaultX = 100,
defaultY = 100,
overSrc = "button-right-over.png",
overX = 100,
overY = 100,
onEvent = buttonHandler,
id = "btn-right"
}
But, when I generate the apk, only display me a black screen and in the simulator shows me the button
in android when naming the file or the image you must not use special character it must only contain [a-z0-9_.] and no capital letters too. you can see the widget button on corona simulator because simulator can read special character.
for more details about resource names refer to this link
and change your widget name from:
local ui = require("ui");
local btnright = ui.newButton{
defaultSrc = "button-right.png",
defaultX = 100,
defaultY = 100,
overSrc = "button-right-over.png",
overX = 100,
overY = 100,
onEvent = buttonHandler,
id = "btn-right"
}
to this
local ui = require("ui");
local btnright = ui.newButton{
defaultSrc = "button_right.png",
defaultX = 100,
defaultY = 100,
overSrc = "button_right_over.png",
overX = 100,
overY = 100,
onEvent = buttonHandler,
id = "btn-right"
}
Which version of corona are you using?
Why not try the widget.newButton()?
check this out. http://docs.coronalabs.com/api/library/widget/newButton.html
Capital letters are allowed. I'm not sure about hyphens, but they are valid characters in Linux and Unix (the core OS used by Android and iOS) However what is likely the cause is case sensitivity. Devices like Android and iOS are case sensitive. The simulator is not, so if you have a button named:
button-right.png
And the actual file name is:
Button-right.PNG
it won't match and it will generate an error. You need to tether your device to your computer and view the device's console log. If you do not know how to do that please see this blog post:
http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Resources