I wrote a simpler test to show the bug. It’s a very very very
strange bug.
1、Close the simulator.
2、Open the simulator, and open the test code
3、Click the screen 5 times, the screen will turn red.
4、Command+R to relaunch the code.
5、Click the screen 5 times, nothing happened.
I have reproducted it on:
mac: 10.12.5 simulator: 2017.3086
iPad mini2: 9.3.5
My question is:
1、Where does the magic number 5 come from?
2、Why the screen will not turn red when I use Command+R to relaunch the test code? It seems something not been cleared when I relaunch the code.
test code: main.lua
\_W = display.contentWidth \_H = display.contentHeight \_AW = display.actualContentWidth \_AH = display.actualContentHeight \_T = display.screenOriginY \_B = \_T + \_AH \_L = display.screenOriginX \_R = \_L + \_AW \_CX = display.contentCenterX \_CY = display.contentCenterY display.setDefault("background", 1) local function removeAllChildren(displayGroup) while displayGroup.numChildren \> 0 do displayGroup[displayGroup.numChildren]:removeSelf() end end local function create() local width = \_AW local height = \_AH local group = display.newGroup() local tex = graphics.newTexture({type = "canvas", width = width, height = height}) local tex2 = graphics.newTexture({type = "canvas", width = width, height = height}) local view = display.newImageRect(group, tex.filename, tex.baseDir, tex.width, tex.height) function group:draw() timer.performWithDelay(1, function() local rect = display.newImageRect(tex2.filename, tex2.baseDir, tex2.width, tex2.height) tex:draw(rect) tex:invalidate() local eraser = display.newRect(0, 0, width, height) eraser:setFillColor(1, 0, 0, 1) tex2:draw(eraser) tex2:invalidate() end) end group:translate(\_CX, \_CY) return group end Runtime:addEventListener("touch", function(event) if "began" == event.phase then canvas = create() canvas:draw() end end)
I’ve written a simple test to reproduce this bug.
I have reproducted it on:
mac: 10.12.5 simulator: 2017.3086
iPad mini2: 9.3.5
steps to test:
1、open simulator, and run the test code
2、draw somthing, and the canvas will be cleared when you release mouse.
3、command+r to relaunch the code, draw somthing again, and the canvas is ok now, it’s will not be cleared when you release mouse.
4、close the simulator and run the test code, the bug show again.
Complete test code:
_W = display.contentWidth _H = display.contentHeight _AW = display.actualContentWidth _AH = display.actualContentHeight _T = display.screenOriginY _B = _T + _AH _L = display.screenOriginX _R = _L + _AW _RW = _R - _L _RH = _B - _T _CX = display.contentCenterX _CY = display.contentCenterY display.setDefault(“background”, 1) local function removeAllChildren(displayGroup) while displayGroup.numChildren > 0 do displayGroup[displayGroup.numChildren]:removeSelf() end end function createCanvas() local width = _RW local height = _RH local group = display.newGroup() local tex = graphics.newTexture({type = “canvas”, width = width, height = height}) local view = display.newImageRect(group, tex.filename, tex.baseDir, tex.width, tex.height) local standInTexture = graphics.newTexture({type = “canvas”, width = width, height = height}) local standInView = display.newImageRect(group, standInTexture.filename, standInTexture.baseDir, standInTexture.width, standInTexture.height) group.totalAlpha = 0.7 group.texture = tex group.view = view group.standInTexture = standInTexture group.standInView = standInView standInView.alpha = group.totalAlpha function group:flush() local tex = self.standInTexture local rect = display.newImageRect(self, tex.filename, tex.baseDir, tex.width, tex.height) rect.alpha = self.totalAlpha self.texture:draw(rect) self.texture:invalidate() removeAllChildren(self.standInTexture.cache) self.standInTexture:invalidate(‘cache’) removeAllChildren(self.texture.cache) end function group:paint(displayObj, x, y) local px, py = self.view:contentToLocal(x, y) displayObj:translate(px - displayObj.x, py - displayObj.y) local texture = self.standInTexture texture:draw(displayObj) texture:invalidate() end function group:draw(x, y) local dot = display.newCircle(0, 0, 32) dot:setFillColor(1, 0, 0, 1) self:paint(dot, x, y) end function group:touch(e) if e.phase == “began” then display.currentStage:setFocus(self, e.id) self:draw(e.x, e.y) self.noTracking = true elseif e.phase == “moved” then if not self.isFocused then self.isFocused = true display.currentStage:setFocus( self, e.id ) end self:draw(e.x, e.y) else – if self.noTracking then – self:drawMeshElement(e.x, e.y) – end self:flush() display.currentStage:setFocus(self, nil) self.isFocused = false end return true end function group:clearSelf() removeAllChildren(self.texture.cache) removeAllChildren(self.standInTexture.cache) self.texture:releaseSelf() self.standInTexture:releaseSelf() self:removeSelf() end group:addEventListener(“touch”) group:translate(_CX, _CY) return group end local canvas for i=1, 5 do if canvas ~= nil then canvas:clearSelf() end canvas = createCanvas() end
I also submitted an issue to Corona Issue/Bug system(but It seems I can not track the issue, so I had to post it here)