Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:42:57+00:00 2026-06-09T18:42:57+00:00

I’m new to Corona SDK. I’m implementing a test using drag-and-drop for an application.

  • 0

I’m new to Corona SDK.

I’m implementing a test using drag-and-drop for an application.

I implemented the code bellow for a container that “attract” the dragging object that is near it.

display.setStatusBar( display.HiddenStatusBar )

local posX, posY = 100, 200
local sizeX, sizeY = 150, 100

local container = display.newRect( posX, posY, sizeX, sizeY )
container:setFillColor( 255,0,0 )
container.strokeWidth = 3
container:setStrokeColor(100, 100, 100)

local myObject = display.newRect( 0, 0, sizeX, sizeY )
myObject.alpha = 0.6
myObject:setFillColor( 0,0,255 )


-- dcenter, d1, d2, d3, d4 are test points: IF THERE ARE AT LEAST 2 POINTS INSIDE THE TARGET CONTAINER, THAN THE OBJECT WILL BE ATRACTED INTO THE CONTAINER
-- uncomment the code bellow to see the test points

-- local dcenter = display.newCircle( (sizeX/2) - 1, (sizeY/2) - 1, 2 )
-- dcenter:setFillColor(120,120,120)
-- 
-- local d1 = display.newCircle( (sizeX/2) - (((1/3) * sizeX)/2) - 1, (sizeY/2) - (((1/3) * sizeY)/2) - 1, 2 )
-- d1:setFillColor(120,120,120)
-- 
-- local d2 = display.newCircle( (sizeX/2) - (((1/3) * sizeX)/2) - 1, (sizeY/2) + (((1/3) * sizeY)/2) - 1, 2 )
-- d2:setFillColor(120,120,120)
-- 
-- local d3 = display.newCircle( (sizeX/2) + (((1/3) * sizeX)/2) - 1, (sizeY/2) - (((1/3) * sizeY)/2) - 1, 2 )
-- d3:setFillColor(120,120,120)
-- 
-- local d4 = display.newCircle( (sizeX/2) + (((1/3) * sizeX)/2) - 1, (sizeY/2) + (((1/3) * sizeY)/2) - 1, 2 )
-- d4:setFillColor(120,120,120)


-- touch listener function
function myObject: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

        -- uncomment the code bellow to see the test points (following myObject)
        -- dcenter.x, dcenter.y = x, y
        -- d1.x, d1.y = x - (((1/3) * sizeX)/2), y - (((1/3) * sizeY)/2)
        -- d2.x, d2.y = x - (((1/3) * sizeX)/2), y + (((1/3) * sizeY)/2)
        -- d3.x, d3.y = x + (((1/3) * sizeX)/2), y - (((1/3) * sizeY)/2)
        -- d4.x, d4.y = x + (((1/3) * sizeX)/2), y + (((1/3) * sizeY)/2)

        if (((x >= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
            container:setFillColor( 100,0,0 )
        else
            container:setFillColor( 255,0,0 )
        end

    elseif event.phase == "ended" then

        local x = (event.x - event.xStart) + self.markX
        local y = (event.y - event.yStart) + self.markY

        -- main condition: I calculated 3 areas to atract the object to the target container, 2 areas that atract it when it's 1/3 in the target and 1 area that atract it when it's 1/4 in the target
        if (((x >= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y >= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y <= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x >= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y >= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x <= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y <= ((sizeY/2) + posY + ((1/2) * sizeY))))) then
            self.x, self.y = posX + (sizeX/2), posY + (sizeY/2);

            -- uncomment the code bellow to see the test points (following myObject)
            -- dcenter.x, dcenter.y = posX + (sizeX/2), posY + (sizeY/2)
            -- d1.x, d1.y = posX - (((1/3) * sizeX)/2) + (sizeX/2), posY - (((1/3) * sizeY)/2) + (sizeY/2)
            -- d2.x, d2.y = posX - (((1/3) * sizeX)/2) + (sizeX/2), posY + (((1/3) * sizeY)/2) + (sizeY/2)
            -- d3.x, d3.y = posX + (((1/3) * sizeX)/2) + (sizeX/2), posY - (((1/3) * sizeY)/2) + (sizeY/2)
            -- d4.x, d4.y = posX + (((1/3) * sizeX)/2) + (sizeX/2), posY + (((1/3) * sizeY)/2) + (sizeY/2)
        end

    end

    return true
end


myObject:addEventListener( "touch", myObject )

I just want to know if there is some api for it already.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:42:59+00:00Added an answer on June 9, 2026 at 6:42 pm

    Nope, there are no API for it.

    Also I suggest you use the focus system, so you do not interact with “touch” events of other objects while dragging yours…

    Example:

    local function myTouchListener(event)
        if event.phase == "began" then
            display.getCurrentStage( ):setFocus( event.target );
        elseif event.phase == "ended" then
            display.getCurrentStage( ):setFocus( nil );
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.