Drag native.newTexfield

Is there a way to Drag this : 

local field1 = native.newTextField( 50, 100, 100, 35 )
field1.align = “center”
field1.size = 32
field1.text = “Hello, world!”
field1:setTextColor( 255, 128, 0 )

Thank you.

Hi,

Look at this tutorial and apply the drag event to “field1”:

http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

Best regards,

Tomas

Hi there,

I already did it but maybe that is because i’m making a mistake inside the code…

Here is my code : 

-- touch listener function function field1:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end -- make 'myObject' listen for touch events field1:addEventListener( "touch", myObject )

Pretty sure i’m missing something there?

Thanks for your help

I copied the code and it works for me:

local field1 = native.newTextField( 50, 100, 100, 35 ) field1.align = "center" field1.size = 32 field1.text = "Hello, world!" field1:setTextColor( 255, 128, 0 ) -- touch listener function function field1:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end -- make 'myObject' listen for touch events field1:addEventListener( "touch", myObject )

Post all of the code if this code (for some reason) doesn’t work. Maybe you have something extra that I don’t see?

Best regards,

Tomas

Also change this, field1:addEventListener( “touch”, myObject ), to field1:addEventListener( “touch”, field1  )

Here is my code (i’m newbie and training)

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local sky = display.newImage("background.png") -- create table of colors local colors = { {255,0,0,255},{0,255,0,255},{0,0,255,255},{255,255,255,255},{0,0,0,255}} local field1 = native.newTextField( 300, 200, 350, 150 ) field1.align = "center" field1.width = 1024 field1.x = display.contentCenterX field1.size = 32 field1.hasBackground = false field1.text = "Your text will render here" -- create buttons and color change function for a = 1, #colors do local btn = display.newCircle( (a-1)\*50+60,300, 20) btn:setFillColor(colors[a][1], colors[a][2], colors[a][3], colors[a][4]) btn.color = a function btn.touch(self, event) if event.phase == "ended" then field1:setTextColor(colors[self.color][1], colors[self.color][2], colors[self.color][3], colors[self.color][4]) end end btn:addEventListener("touch", colorChange) btn.y = display.contentHeight - 50 end -- create table of colors local sizes = { 24, 32, 48, 64, 72 } -- create text local txt = field1 function txt.touch(self,event) if event.phase == "moved" then txt.x = event.x txt.y = event.y end end -- create buttons and color change function for a = 1, #colors do local btn = display.newCircle( (a-1)\*50+60,300, 20) btn:setFillColor(colors[a][1], colors[a][2], colors[a][3], colors[a][4]) btn.color = a function btn.touch(self, event) if event.phase == "ended" then txt.size = sizes[self.color] end end btn:addEventListener("touch", btn) btn.y = display.contentHeight - 100 end -- touch listener function function field1:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end -- make 'myObject' listen for touch events field1:addEventListener( "touch", field1 )

Still doesn’t work ; )

I just copied the code and I could move field1 without any problem. However I’m guessing you’re having some other problems.

What I would do with your code is to start over and make one piece work and then go to the next piece instead of trying to get all of the pieces working.

It seems to be redundant to have two for loops to create the circles.

You should also read up on the event “tap” instead of “touch”. Works a little bit different but I think that’s what you’re looking for here.

Also a function should be written like btn:touch and not btn.touch. I don’t recall any good page right now but google it and you will find some explanations.

Also when you add the event listeners to the circle you’re trying to add them to a method (colorChange) that doesn’t exist.

Another biggy is that, according to the “Corona Simulator Output” the simulator doesn’t support native text fields in the simulator so you should build for device.

So to summarize:

* Start over and work in pieces. Try to get one part of your code working before doing anything bigger.

* Don’t use native text fields for now

Not a direct answer to your question but hopefully you will still be able to solve it with these hints and if still not come back and I will give answer your question with some code.

Best regards,

Tomas

Hi,

Look at this tutorial and apply the drag event to “field1”:

http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/

Best regards,

Tomas

Hi there,

I already did it but maybe that is because i’m making a mistake inside the code…

Here is my code : 

-- touch listener function function field1:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end -- make 'myObject' listen for touch events field1:addEventListener( "touch", myObject )

Pretty sure i’m missing something there?

Thanks for your help

I copied the code and it works for me:

local field1 = native.newTextField( 50, 100, 100, 35 ) field1.align = "center" field1.size = 32 field1.text = "Hello, world!" field1:setTextColor( 255, 128, 0 ) -- touch listener function function field1:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end -- make 'myObject' listen for touch events field1:addEventListener( "touch", myObject )

Post all of the code if this code (for some reason) doesn’t work. Maybe you have something extra that I don’t see?

Best regards,

Tomas

Also change this, field1:addEventListener( “touch”, myObject ), to field1:addEventListener( “touch”, field1  )

Here is my code (i’m newbie and training)

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here local sky = display.newImage("background.png") -- create table of colors local colors = { {255,0,0,255},{0,255,0,255},{0,0,255,255},{255,255,255,255},{0,0,0,255}} local field1 = native.newTextField( 300, 200, 350, 150 ) field1.align = "center" field1.width = 1024 field1.x = display.contentCenterX field1.size = 32 field1.hasBackground = false field1.text = "Your text will render here" -- create buttons and color change function for a = 1, #colors do local btn = display.newCircle( (a-1)\*50+60,300, 20) btn:setFillColor(colors[a][1], colors[a][2], colors[a][3], colors[a][4]) btn.color = a function btn.touch(self, event) if event.phase == "ended" then field1:setTextColor(colors[self.color][1], colors[self.color][2], colors[self.color][3], colors[self.color][4]) end end btn:addEventListener("touch", colorChange) btn.y = display.contentHeight - 50 end -- create table of colors local sizes = { 24, 32, 48, 64, 72 } -- create text local txt = field1 function txt.touch(self,event) if event.phase == "moved" then txt.x = event.x txt.y = event.y end end -- create buttons and color change function for a = 1, #colors do local btn = display.newCircle( (a-1)\*50+60,300, 20) btn:setFillColor(colors[a][1], colors[a][2], colors[a][3], colors[a][4]) btn.color = a function btn.touch(self, event) if event.phase == "ended" then txt.size = sizes[self.color] end end btn:addEventListener("touch", btn) btn.y = display.contentHeight - 100 end -- touch listener function function field1:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above end return true end -- make 'myObject' listen for touch events field1:addEventListener( "touch", field1 )

Still doesn’t work ; )

I just copied the code and I could move field1 without any problem. However I’m guessing you’re having some other problems.

What I would do with your code is to start over and make one piece work and then go to the next piece instead of trying to get all of the pieces working.

It seems to be redundant to have two for loops to create the circles.

You should also read up on the event “tap” instead of “touch”. Works a little bit different but I think that’s what you’re looking for here.

Also a function should be written like btn:touch and not btn.touch. I don’t recall any good page right now but google it and you will find some explanations.

Also when you add the event listeners to the circle you’re trying to add them to a method (colorChange) that doesn’t exist.

Another biggy is that, according to the “Corona Simulator Output” the simulator doesn’t support native text fields in the simulator so you should build for device.

So to summarize:

* Start over and work in pieces. Try to get one part of your code working before doing anything bigger.

* Don’t use native text fields for now

Not a direct answer to your question but hopefully you will still be able to solve it with these hints and if still not come back and I will give answer your question with some code.

Best regards,

Tomas