Spawning objects by typing in an x and y coordinate

Hello everyone, I have recently decided to start experimenting with lua and I have something I would like to add to add to my explosive projectiles project. I want to create a text field that accepts two values. First, an x-coordinate value, then a y-coordinate value, then once they are inputed they will spawn a crate at that x and y coordinate. Any ideas on how to this would be greatly appreciated. Thanks!

Well, you can create native textfields, with only numerics inputs:

– Create text field

numericField = native.newTextField( 150, 150, 180, 30 ) numericField.inputType = "number" numericField:addEventListener( "userInput", textListener )

this is the function:

local function textListener( event )     if ( event.phase == "began" ) then         -- User begins editing "defaultField"     elseif ( event.phase == "ended" or event.phase == "submitted" ) then         -- Output resulting text from "defaultField"         print( event.target.text )     elseif ( event.phase == "editing" ) then         print( event.newCharacters )         print( event.oldText )         print( event.startPosition )         print( event.text )     end end

well, instead of printing values, you will call a function called createCrate(paramsX,paramsY) I guess.

You can do 2 textboxs, one for X and one for Y , and limit the numbers to 3 digits maybe.

How can I limit the value to three numbers? Also, would I create the createCrate() function in the event.phase == ended or submitted?

For some reason, it accepts values that are not numbers. Also, how would I do the xParams and yParams thing, sorry for so many question, I am relatively new to this.

if event.phase == "editing" then local txt = event.text if(string.len(txt)\>3)then txt=string.sub(txt, 1, 3) event.text=txt end end

Using this substring , would cut out of the box the extra numbers (they can imput them, but will not accept after 3).

You will need to use it in submit , because if not, you will be spawnin crates every time the number changes, so if you want to put in “123”, you will create crates at 1, at 12, and at 123. (and that is not what you want)

You should use  vars, where you can save the value after the user imput them. Define them outside the scene sections, but assign them when they submit the values.

when both submit are dones, you call the createCrate function, which will recive those 2 vars by parammeters. 

I read people use a confirm button , so they know when the textfields are ready (but I’ve no idea how to do that in Corona)

Don’t worry, I’m new too.

I’ve programmed in other languages and frameworks, but I have just a few hours of coding in Corona SDK.

Good Luck, I will try some more code , tell me if you find something new.

 

 

So, is there a way to have the textField clear itself, save the previous value, then another value is typed in, then saved and both are stored in two variables, paramsX and paramsY? 

Also, how would I set up the createCrate() function? Would I do something like:

function createCrate(x,y) local crate = display.newImageRect("crate.png", 90, 90) crate.x = x crate.y = y end --And then later on: createCrate(paramsX, paramsY)

Right now I am trying this, but it is not working:

local function createCrate(x,y) local crate = display.newImageRect("crate.png", 90, 90) crate.x = x crate.y = y end function textListener( event ) if ( event.phase == "began" ) then -- User begins editing "defaultField" elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- Output resulting text from "defaultField" local text = event.text paramsX = event.text createCrate(paramsX, -200) elseif ( event.phase == "editing" ) then local txt = event.text if(string.len(txt)\>3)then txt=string.sub(txt, 1, 3) event.text=txt end end end numericField = native.newTextField( 150, 150, 180, 30 ) numericField.inputType = "number"

I figured it out, I just had to add physics to the crate.

Now, it only spawns crates at an x coordinate of 0, this is my code:

function textListener( event ) if ( event.phase == "began" ) then -- User begins editing "defaultField" elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- Output resulting text from "defaultField" local text = event.text paramsX = text createCrate(paramsX, -200) elseif ( event.phase == "editing" ) then local txt = event.text if(string.len(txt)\>3)then txt=string.sub(txt, 1, 3) event.text=txt end end end

Well, you can create native textfields, with only numerics inputs:

– Create text field

numericField = native.newTextField( 150, 150, 180, 30 ) numericField.inputType = "number" numericField:addEventListener( "userInput", textListener )

this is the function:

local function textListener( event )     if ( event.phase == "began" ) then         -- User begins editing "defaultField"     elseif ( event.phase == "ended" or event.phase == "submitted" ) then         -- Output resulting text from "defaultField"         print( event.target.text )     elseif ( event.phase == "editing" ) then         print( event.newCharacters )         print( event.oldText )         print( event.startPosition )         print( event.text )     end end

well, instead of printing values, you will call a function called createCrate(paramsX,paramsY) I guess.

You can do 2 textboxs, one for X and one for Y , and limit the numbers to 3 digits maybe.

How can I limit the value to three numbers? Also, would I create the createCrate() function in the event.phase == ended or submitted?

For some reason, it accepts values that are not numbers. Also, how would I do the xParams and yParams thing, sorry for so many question, I am relatively new to this.

if event.phase == "editing" then local txt = event.text if(string.len(txt)\>3)then txt=string.sub(txt, 1, 3) event.text=txt end end

Using this substring , would cut out of the box the extra numbers (they can imput them, but will not accept after 3).

You will need to use it in submit , because if not, you will be spawnin crates every time the number changes, so if you want to put in “123”, you will create crates at 1, at 12, and at 123. (and that is not what you want)

You should use  vars, where you can save the value after the user imput them. Define them outside the scene sections, but assign them when they submit the values.

when both submit are dones, you call the createCrate function, which will recive those 2 vars by parammeters. 

I read people use a confirm button , so they know when the textfields are ready (but I’ve no idea how to do that in Corona)

Don’t worry, I’m new too.

I’ve programmed in other languages and frameworks, but I have just a few hours of coding in Corona SDK.

Good Luck, I will try some more code , tell me if you find something new.

 

 

So, is there a way to have the textField clear itself, save the previous value, then another value is typed in, then saved and both are stored in two variables, paramsX and paramsY? 

Also, how would I set up the createCrate() function? Would I do something like:

function createCrate(x,y) local crate = display.newImageRect("crate.png", 90, 90) crate.x = x crate.y = y end --And then later on: createCrate(paramsX, paramsY)

Right now I am trying this, but it is not working:

local function createCrate(x,y) local crate = display.newImageRect("crate.png", 90, 90) crate.x = x crate.y = y end function textListener( event ) if ( event.phase == "began" ) then -- User begins editing "defaultField" elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- Output resulting text from "defaultField" local text = event.text paramsX = event.text createCrate(paramsX, -200) elseif ( event.phase == "editing" ) then local txt = event.text if(string.len(txt)\>3)then txt=string.sub(txt, 1, 3) event.text=txt end end end numericField = native.newTextField( 150, 150, 180, 30 ) numericField.inputType = "number"

I figured it out, I just had to add physics to the crate.

Now, it only spawns crates at an x coordinate of 0, this is my code:

function textListener( event ) if ( event.phase == "began" ) then -- User begins editing "defaultField" elseif ( event.phase == "ended" or event.phase == "submitted" ) then -- Output resulting text from "defaultField" local text = event.text paramsX = text createCrate(paramsX, -200) elseif ( event.phase == "editing" ) then local txt = event.text if(string.len(txt)\>3)then txt=string.sub(txt, 1, 3) event.text=txt end end end