2012-04-16 229 views
0

下面是代碼:如何刪除以前繪製的線?

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) 

的問題是:

  1. 例如:我畫一條線,然後我畫下一條線,我想前行被刪除。我怎樣才能做到這一點?
  2. 我正在使用導演1.4,當我試圖重播級別時,線條停止繪製正確,並且出現指向此線條的錯誤 - if(lines[i]) then lineGroup:remove(i) end so:如何將線條添加到localGroup並在播放時刪除它們水平(我用director:changeScene("level1")或正常變化的場景?

回答

0

嗯,我以爲你想多行,因爲你是把它們添加到一個行表。如果你不需要多行,你可以存儲1行:類似於:

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) 

將它們添加到c urrent場面,我相信導演類將是這樣的:

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

你只需要設置lineGroup到scene.view而不是創建新組display.newGroup()

向正確刪除行,你可以做到這一點在出口場景功能:

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

我建議考慮看看導演類教程:http://www.youtube.com/watch?v=KudLE8h4kWw或此代碼示例https://github.com/ansca/Storyboard-Sample

+0

萬萬感謝男人!你只是鞭打! – barmyman 2012-04-16 21:12:53