I think it a big limitation that you can't use "newTextField" in corona simulator, I actually need to build the app and install it on my device to see if it's working.
The following code for some reason not working for me, and I don't know how to debug it.
I simply want to save "players name"
local function textListener( event )
if ( event.phase == "began" ) then
-- user begins editing text field
print( event.text )
myGameSettings.playerName = event.text
saveSettings()
elseif ( event.phase == "ended" ) then
-- text field loses focus
myGameSettings.playerName = event.text
saveSettings()
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
myGameSettings.playerName = event.text
saveSettings()
-- do something with defaultField's text
elseif ( event.phase == "editing" ) then
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
local playerName = native.newTextField( centerX, display.contentCenterY-100, display.contentWidth, 50 )
display.newText("Choose a name", 20, playerName.y-70, native.systemFont)
playerName:addEventListener( "userInput", textListener )
It appears to be supported only in the Enterprise version.
Is your problem that it isn't working in the Simulator? Because they don't work on the Windows PC simulator but they work in the Mac simulator. I develop on Mac and PC and can use them perfectly on Mac but not on PC. I'm also no on the Enterprise edition but am on the free edition.
When I'm working on my PC how I debug is to hook my device up to the computer and print out the debug log as it will print any errors (I obviously can only use Android devices hooked up to my PC).
First of all the native.newTextField does not display on window machine simulator. you need to build it on device for testing. But you can debug it on simulator though.
Use below code for debug on simulator.
Note: the textField area is not visible but when you click on it(Assuming text field position), will show you the textfield with blue rectangle border.
-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )
-- Set the background to white
display.setDefault( "background", 255, 255, 255 )
-- Require the widget & storyboard libraries
local widget = require( "widget" )
local function textListener( event )
if ( event.phase == "began" ) then
print( event.text )
elseif ( event.phase == "ended" or event.phase == "submitted" ) then
-- do something with defaultField's text
elseif ( event.phase == "editing" ) then
print("in move")
print( event.newCharacters )
print( event.oldText )
print( event.startPosition )
print( event.text )
end
end
local playerName = native.newTextField( 300, 300, 400, 50 )
local a = display.newText("Choose a name", 20, playerName.y-70, native.systemFont)
a.x=100
a.y = playerName.y-70
a:setTextColor(0,0,0)
playerName:addEventListener( "userInput", textListener )
Related
I am trying to add a touch event listener to the image object being loaded. Although this is practically an exact copy and paste from the documentation:
https://docs.coronalabs.com/api/type/EventDispatcher/addEventListener.html
It returns the following error:
36: attempt to index local 'object' (a nil value)
local t = {}
local img = {}
local i = 1
local function showImages ()
local function networkListenerImg( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
end
for k,v in pairs(t) do
img[#img + 1] = v
end
local object = display.loadRemoteImage( event.params.chapter .. img[i], "GET", networkListenerImg, img[i], system.TemporaryDirectory, 50, 50 )
function object:touch( event )
if event.phase == "began" then
print( "You touched the object!" )
return true
end
end
object:addEventListener( "touch", object )
end
The table, t, is populated elsewhere in the code and is populated properly.
While you didn't mention which of those lines is line 36 (there are only 28 lines there), I can still see your error. The issue is that object is and always will be nil: display.loadRemoteImage() does not return anything, see this.
What you need to do is have your listener callback capture object, which must be declared before the callback is. The callback should then set the value of object to the results of the download. Like so...
local t = {}
local img = {}
local i = 1
local function showImages ()
local object
local function networkListenerImg( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
-- fill in code to save the download object into "object"
end
end
for k,v in pairs(t) do
img[#img + 1] = v
end
display.loadRemoteImage( event.params.chapter .. img[i], "GET", networkListenerImg, img[i], system.TemporaryDirectory, 50, 50 )
function object:touch( event )
if event.phase == "began" then
print( "You touched the object!" )
return true
end
end
object:addEventListener( "touch", object )
end
I've started learning Maxscripts and i've now hit a wall,
im trying to get the name of my selection, if it's a single object and
then if its more than 1, have the label display the number of objects as a string.
but i keep getting an error... any idea?
group "Current Selection:"
(
label lbl_01 "Nothing Selected"
)
---------------------------------------------------------------------------------------------------------------// Current Selection Function
fn letmeknow obj=
(
local contador = (selection.count as string)
if selection.count != 0 then
(
lbl_01.text = ("Name: " + obj.name)
)
else
(
lbl_01.text = "Nothing Selected"
)
if selection.count >= 2 do (lbl_01.text = ("Objects: " + contador))
)
Looks like the issue is outside the code you've supplied and without seeing the rest of the code, it's hard to tell. Anyway, here's a working example using case expression instead of multiple ifs:
rollout test "Test"
(
group "Current Selection:"
(
label lbl_01 "Nothing Selected"
)
button btnTest "Test"
fn getSelectionString =
(
case selection.count of
(
0 : "Nothing Selected"
1 : "Name: " + selection[1].name
default : "Objects: " + selection.count as string
)
)
on btnTest pressed do
lbl_01.text = getSelectionString()
)
createDialog test
So I have a function which runs if someone is holding a button (an object moves). But I also have this collision between that object and another static one, when the first object hits the second one, it gets sent back to the start point. But the problem is that when I keep holding the button to move the object. It doesn't send it back to start when it collides. It just messes up.
This is the code I have for the hold button function:
local holding = false
function enterFrameListener()
if holding then
transition.to( cube, {time = 0, x= cube.x - 5} )
end
end
function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
leftbutton:addEventListener( "touch", touchHandler )
This is the code I have for the collision:
function onCollision( event )
if ( event.phase == "began" ) then
transition.cancel( )
transition.moveTo( cube, {time = 0, x = 35, y = 100} )
end
end
redblock:addEventListener( "collision", onCollision )
Try to insert a return true at the end of the onCollision(event) function as below:
function onCollision( event )
if ( event.phase == "began" ) then
transition.cancel( )
transition.moveTo( cube, {time = 0, x = 35, y = 100} )
end
return true
end
Make object as bullet.
There is a property in corona.
isBullet=true
will make your physics object as bullet so it'll check collision for several times in a second.
Then this event can thrown, otherwise it'll not raise that event.
I'm using XInput for a project and I'm having a problem using the xbox controller. When I use the B button to return to a previous state, its registering the button press so fast that it continues to push back multiple states that use the B button to exit.
if( ( player1.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) || ( player2.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) ) {
return ESC_BUTTON;
}
I've tried resetting the buffer by adding an & 1 but than the input never registers. Is there an easy way to fix this problem?
UPDATE:
current = (DWORD)( ( start - GetTickCount() ) / 1000.f );
if( current > 0.005f )
{
if( ( player1.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) || ( player2.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) )
{
start = GetTickCount();
return ESC_BUTTON;
}
}
Still having issues with resetting. Sometimes it works, sometimes it doesn't...thoughts?
I using Corona SDK. I parse json and in the listener function I try to load images using links from json, but it says network error. It seems I cannot run network methods in parallel?
Even default corona demo code doesn't work for some reason:
local function networkListener2( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
print ( "RESPONSE: " .. event.response )
end
myImage2 = display.loadRemoteImage(
"http://developer.anscamobile.com/demo/hello.png",
"GET",
networkListener2,
"helloCopy2.png",
system.TemporaryDirectory,
60, 280 )
it fails to run on android emulator.