does not work, have changed the code a few times and still not can get to work

David,
See if this helps.
Start a new app.
Paste into main.lua the code I show below.
Run the app.
Drag any of the circles onto the field and release where you want them. If you drag them off the field and release them, they return automatically to their original position, just off the bottom edge of the field.
Is that what you are wanting your code to do?
local tokens = {} local tokenColors = { {.7, .1, .1}, {.8, .8, .8}, {.1, .1, .7} } local leftEdge, topEdge, rightEdge, botEdge local field = display.newRect(display.contentCenterX, display.contentCenterY, 250, 200) field:setFillColor(.1,.8,.1) leftEdge = field.x - (field.width \* .5) topEdge = field.y - (field.height \* .5) rightEdge = field.x + (field.width \* .5) botEdge = field.y + (field.height \* .5) local function moveToken(e) local t = e.target if e.phase == "began" then display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = e.x - t.x t.y0 = e.y - t.y elseif t.isFocus then if e.phase == "moved" then t.x = e.x - t.x0 t.y = e.y - t.y0 elseif e.phase == "ended" then if t.x \< leftEdge then t.x = t.base.x t.y = t.base.y elseif t.x \> rightEdge then t.x = t.base.x t.y = t.base.y end if t.y \< topEdge then t.x = t.base.x t.y = t.base.y elseif t.y \> botEdge then t.x = t.base.x t.y = t.base.y end -- note that the token is set in place display.getCurrentStage():setFocus(nil) end end return true end for i = 1 , 3 do table.insert(tokens, display.newCircle(50,50,20)) tokens[i]:setFillColor(tokenColors[i][1],tokenColors[i][2], tokenColors[i][3] ) tokens[i].x = i \* 80 tokens[i].y = field.y + (field.height \* .6) tokens[i]:addEventListener("touch", moveToken) tokens[i].base = {x = tokens[i].x, y = tokens[i].y} end
Hope it helps!
Bob
WORK!!! WORK!!!
I have to do some changes but WORK !!!
TY MAN !! :wub: :wub: :wub:
Since your rectangles are only 1 pixel wide (or high), there is no interior area to fill – have you tried rect:setStrokeColor(…) and rect.strokeWidth=… ??
Continuo sem conseguir com que o meu código funcione

I’m not sure what you are trying to achieve or what exactly isn’t working.
Can you clarify your question?
I want my screen has limits, that is , I have an image of a field as background and the rest could not leave the background , could not leave the mobile phone screen
Hey, I’m just guessing here, as your english is quite horrible. (due to your translation software I suppose)
The way you created the borders is correct, but you have to keep two things in mind.
-
The things you want to keep inside the screen boundaries needs to have a body as well. (a dynamic one) https://docs.coronalabs.com/api/library/physics/addBody.html
-
You should adjust the borders to the actual screen size (not a fixed value like 770).
https://docs.coronalabs.com/api/library/display/viewableContentWidth.html
https://docs.coronalabs.com/api/library/display/viewableContentHeight.html
Hope that helps.
If not, please clarify your question.
Irony at it’s finest 
I wish I could say, that it was intended 
Still does not work …
I know that English is terrible , but Google Tradudor not help much xD
local composer = require( "composer" ) local scene = composer.newScene() local function buttonHit(event) composer.gotoScene( event.target.destination ) return true end -- inclui a livraria "physics" local physics = require "physics" physics.start() -------------------------------------------- function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then Limites ecrã xminscreen=display.contentCenterX-0.5\*display.actualContentWidth yminscreen=display.contentCenterY-0.5\*display.actualContentHeight xmaxscreen=0.5\*display.actualContentWidth+display.contentCenterX ymaxscreen=display.actualContentHeight+(yminscreen) local borderTop = display.newRect( 0, 0, 115, 100 ) borderTop:setFillColor( 0, 0, 0, 1) -- Invisivel borderTop.strokeWidth = 100 borderTop:setStrokeColor( 0, 0, 0 ) physics.addBody( borderTop, "static", borderBodyElement ) sceneGroup:insert(borderTop) local borderBottom = display.newRect( 0, 100, 707, 1 ) borderBottom.stroke=100 borderBottom:setFillColor( 0, 0, 0, 0) -- Invisivel physics.addBody( borderBottom, "static", borderBodyElement ) sceneGroup:insert(borderBottom) local borderLeft = display.newRect( 0, 1, 1, 1000 ) borderLeft.stroke=10 borderLeft:setFillColor( 0, 0, 0, 0) -- Invisivel physics.addBody( borderLeft, "static", borderBodyElement ) sceneGroup:insert(borderLeft) local borderRight = display.newRect( 707, 1, 1, 1000 ) borderRight.stroke= 100 borderRight:setFillColor( 0, 0, 0, 0) -- Invisivel physics.addBody( borderRight, "static", borderBodyElement ) sceneGroup:insert(borderRight) -- Inserir BackGround local background = display.newImageRect( "CampoFutsal/Campo1F.tif", 512,300 ) background.x = display.contentCenterX + 0 background.y = display.contentCenterY + 0 sceneGroup:insert( background ) --Botão Voltar local backBtn = display.newImageRect("CampoFutsal/botaoV2.tif",25,25) backBtn.x= display.contentCenterX + 240 backBtn.y= display.contentCenterY + 135 backBtn.destination = "menu" backBtn:addEventListener("tap", buttonHit) composer.removeScene(true) sceneGroup:insert(backBtn) --Botão Jogadas local jogadasBtn = display.newImageRect("CampoFutsal/botaoJ.tif",30,30) jogadasBtn.x= display.contentCenterX + 240p jogadasBtn.y= display.contentCenterY - 133 jogadasBtn.destination = "Jogadas1" jogadasBtn:addEventListener("tap", buttonHit) composer.removeScene(true) sceneGroup:insert(jogadasBtn) -- Inserir a Camisola local camisola1 = display.newImageRect("CampoFutsal/Camisola-F1.tif",33,60) camisola1.x = display.contentCenterX - 240 camisola1.y = display.contentCenterY - 115 sceneGroup:insert(camisola1) physics.addBody( camisola1, "static", { friction=0.5, bounce=10 } ) -- Inserir a Camisola local camisola2 = display.newImageRect("CampoFutsal/Camisola-F2.tif",33,60) camisola2.x=display.contentCenterX - 240 camisola2.y=display.contentCenterY - 60 sceneGroup:insert(camisola2) -- Inserir a Camisola local camisola3 = display.newImageRect("CampoFutsal/Camisola-F3.tif",33,60) camisola3.x=display.contentCenterX - 240 camisola3.y=display.contentCenterY + 0 sceneGroup:insert(camisola3) -- Inserir a Camisola local camisola4 = display.newImageRect("CampoFutsal/Camisola-F4.tif",35,60) camisola4.x=display.contentCenterX - 240 camisola4.y=display.contentCenterY + 60 sceneGroup:insert(camisola4) -- Inserir a Camisola local camisola5 = display.newImageRect("CampoFutsal/Camisola-F5.tif",33,60) camisola5.x=display.contentCenterX - 240 camisola5.y=display.contentCenterY + 115 sceneGroup:insert(camisola5) --Função para dar movimento ás camisolas local function camisolaTouch(event) local touchedObject = event.target if event.phase == "began" then touchedObject.previousX = touchedObject.x touchedObject.previousY = touchedObject.y -- Marca o objeto como alvo local parent = touchedObject.parent parent:insert( touchedObject ) display.getCurrentStage():setFocus( touchedObject ) -- Localiza e seleciona o alvo, assim o utilizador pode mudar de objeto touchedObject.isFocus = true elseif event.phase == "moved" or "cancelled" == event.phase then display.getCurrentStage():setFocus( nil ) touchedObject.isFocus = false touchedObject.x = (event.x - event.xStart) + touchedObject.previousX touchedObject.y = (event.y - event.yStart) + touchedObject.previousY end return true end -- Adiciona a função touch ás camisolas, função que dá movimento as camisolas camisola3:addEventListener( "touch" , camisolaTouch) camisola4:addEventListener( "touch" , camisolaTouch) camisola5:addEventListener( "touch" , camisolaTouch) camisola2:addEventListener( "touch" , camisolaTouch) camisola1:addEventListener( "touch" , camisolaTouch) elseif phase == "did" then end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then physics.stop() elseif phase == "did" then end end function scene:destroy( event ) local sceneGroup = self.view end ----------------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene
This is the application code if I could help find out where the error , thanked :wub:
So, this is the code you use. But what is the problem with it?
Which object should stay inside the borders and when does it fails to do so?
Actually you are using only static bodys, so there is nothing that could collide (only “dynamic” bodys collide with others).
The " Camisola " were to stay in the background …
I changed it to " Dynamic " but now the bodies disappear from the screen
Only set the Camisolas body to dynamic, leave the others static.
But what is " dynamic" falls of the screen , there is no way to stop ?
The static borders should preent the dynamic Camisolas from falling out of the screen.
I thought that was your intend in the first place?
Maybe that is easier to understand, the t - shirts can not leave the field , which is the background , they start with a preset position and then the user changes to where you want …
Ah, I think I understand now.
The user should not be able to place the T-Shrits outside of the field?
If that’s what you want to achieve, physics are not the right way to do it.
Inside your touch function you have to check if the current position is insie the field. If yes, aplly it to the shirt, if not, it should stay where it is.
Yes!!! It is !!!
But how do I do it is that I can not even, I tried a thousand ways and does not give
local function camisolaTouch(event) local touchedObject = event.target if event.phase == "began" then touchedObject.previousX = touchedObject.x touchedObject.previousY = touchedObject.y -- Marca o objeto como alvo local parent = touchedObject.parent parent:insert( touchedObject ) display.getCurrentStage():setFocus( touchedObject ) -- Localiza e seleciona o alvo, assim o utilizador pode mudar de objeto touchedObject.isFocus = true elseif event.phase == "moved" or "cancelled" == event.phase then display.getCurrentStage():setFocus( nil ) touchedObject.isFocus = false touchedObject.x = (event.x - event.xStart) + touchedObject.previousX touchedObject.y = (event.y - event.yStart) + touchedObject.previousY end return true end
In that part of the function I have to get the If’s?
Try something like this:
local fieldWidth = 1000 local fieldHeight = 600 local fieldCenterX = 1200 local fieldCenterY = 700 local fieldBoundLeft = fieldCenterX - fieldWidth\*0.5 local fieldBoundRight = fieldCenterX + fieldWidth\*0.5 local fieldBoundTop = fieldCenterY - fieldHeight\*0.5 local fieldBoundBottom = fieldCenterY + fieldHeight\*0.5 local function camisolaTouch(event) local touchedObject = event.target local eventPhase = event.phase if eventPhase == "began" then touchedObject.startX = touchedObject.x touchedObject.startY = touchedObject.y local parent = touchedObject.parent parent:insert( touchedObject ) display.getCurrentStage():setFocus( touchedObject ) touchedObject.isFocus = true elseif touchedObject.isFocus then if eventPhase == "moved" then touchedObject.x = (event.x - event.xStart) + touchedObject.startX touchedObject.y = (event.y - event.yStart) + touchedObject.startY else display.getCurrentStage():setFocus( nil ) touchedObject.isFocus = nil local newX = (event.x - event.xStart) + touchedObject.startX local newY = (event.y - event.yStart) + touchedObject.startY if newX \>= fieldBoundLeft and newX \<= fieldBoundRight and newY \>= fieldBoundTop and newY \<= fieldBoundBottom then touchedObject.x = newX touchedObject.y = newY else touchedObject.x = touchedObject.startX touchedObject.y = touchedObject.startY end touchedObject.startX = nil touchedObject.startY = nil end end return true end
First I estimented the field bounds from it’s width, height and center position.
Inside the ended event of the touch listener I test if the current position of the object is inside those bounds. If yes, it’s placed there, if not it returns to it’s starting position.