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 7818779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:41:43+00:00 2026-06-02T06:41:43+00:00

Here is the code: local physics = require physics physics.start() local lines = {}

  • 0

Here is the code:

local physics = require "physics"
physics.start()

local lines = {}
local lineGroup = display.newGroup()
local prevX,prevY
local isDrawing = false
local i = 0

local function distanceBetween(x1, y1, x2, y2)
    local dist_x = x2 - x1
    local dist_y = y2 - y1
    local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
    return distanceBetween
end

local function drawLine(e)
    if(e.phase == "began") then
        prevX = e.x
        prevY = e.y
        isDrawing = true
        i = i + 1
        print"began"
    elseif(e.phase == "moved") then
        local distance = distanceBetween(prevX, prevY, e.x, e.y)
        if(isDrawing and distance < 100) then
            if(lines[i]) then lineGroup:remove(i) end
            lines[i] = display.newLine(prevX, prevY, e.x, e.y)
            lines[i]:setColor(255, 255, 0)
            lines[i].width = 5

            local dist_x = e.x - prevX
            local dist_y = e.y - prevY
            physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = 1.6, shape = {0, 0, dist_x, dist_y, 0, 0} } )
            lineGroup:insert(lines[i])
        end
    elseif(e.phase == "ended") then
        isDrawing = false
    end
end

Runtime:addEventListener("touch",drawLine)

The questions are:

  1. For example: I draw a line, then I draw the next line and I want the previous line to be deleted. How can I do this?
  2. I am using director 1.4 and when I am trying to replay level, the lines stop to draw correctly and the error appears that is pointing to this line – if(lines[i]) then lineGroup:remove(i) end so: How can I add lines to the localGroup and delete them when replaying level (I am using director:changeScene("level1") or changing scene correctly?
  • 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-02T06:41:44+00:00Added an answer on June 2, 2026 at 6:41 am

    Hmm, I assumed you wanted multiple lines because you was adding them to a lines table. If you don’t need multiple lines, you can just store the 1 line. Something like:

    local physics = require "physics"
    physics.start()
    
    local line
    local lineGroup = display.newGroup()
    local prevX,prevY
    local isDrawing = false
    local i = 0
    
    local function distanceBetween(x1, y1, x2, y2)
        local dist_x = x2 - x1
        local dist_y = y2 - y1
        local distanceBetween = math.sqrt((dist_x*dist_x) + (dist_y*dist_y))
        return distanceBetween
    end
    
    local function drawLine(e)
        if(e.phase == "began") then
            if(line) then
                lineGroup:remove(1)
                line = nil
            end
            prevX = e.x
            prevY = e.y
            isDrawing = true
        elseif(e.phase == "moved") then
            local distance = distanceBetween(prevX, prevY, e.x, e.y)
            if(isDrawing and distance < 100) then
                if(line) then lineGroup:remove(1) end
                line = display.newLine(prevX, prevY, e.x, e.y)
                line:setColor(255, 255, 0)
                line.width = 5
    
                local dist_x = e.x - prevX
                local dist_y = e.y - prevY
                physics.addBody(line, "static", { density = 1, friction = 0.5, bounce = 1.6, shape = {0, 0, dist_x, dist_y, 0, 0} } )
                lineGroup:insert(line)
            end
        elseif(e.phase == "ended") then
            isDrawing = false
        end
    end
    
    Runtime:addEventListener("touch",drawLine)
    

    To add them to the current scene, I believe the director class would be something like this:

    function scene:createScene( event )
        lineGroup = self.view
    end
    

    You just need to set the lineGroup to the scene.view instead of creating a new group with display.newGroup()

    To properly remove the line, you can do it in the exit scene function:

    function scene:exitScene( event )
        if(line) then
            lineGroup:remove(1)
            line = nil
        end
    end
    

    I’d recommend taking a look at the director class tutorials: http://www.youtube.com/watch?v=KudLE8h4kWw or this code sample https://github.com/ansca/Storyboard-Sample

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this bit of code here: local http = require(socket.http) I know it's
Ok the error is showing up somewhere in this here code if($error==false) { $query
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Consider the following Lua code: local var1, var2; Is var2 a local variable here?
Here is code in form.html.erb for partial view. Local var :sid was defined as
Given: function foo(){ var bar = quux; console.log(/*mystery code here*/); } I'm looking for
I tried to save web image to local, I use code here to make
Here is code from a tutorial in A Byte of Python: import sys filename
Here is code from MSDN . I don't understand why the work isn't just
Here a code to demonstrate an annoying problem: class A { public: A(): m_b(1),

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.