is there a logical error in this code? [lua] _H = display.contentHeight _W = display.contentWidth local composer = require “composer”; local scene = composer.newScene(); local bg, message, house, ball, can, shadowHouse, shadowBall, shadowCan local function hasCollided(obj1, obj2) if (obj1 == nil) then return false end if (obj2 == nil) then return false end local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax return (left or right) and (up or down) end local function dragHouse(event) local target = event.target local phase = event.phase if (event.phase == “began”) then local parent = target.parent display.getCurrentStage(): setFocus(target) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif(target.isFocus) then if (phase == “moved”) then target.x = event.x - target.x0 target.y = event.y - target.y0 if (hasCollided(event.target, shadowHouse)) then transition.to(event.target, {time = 250, x = shadowHouse.x, y = shadowHouse.y}) audio.play(sound2) phase = “cancelled” else end elseif(phase == “cancelled” or phase == “cancelled”) then display.getCurrentStage():setFocus(nil) target.isFocus = false end end return true end local function dragBall(event) local target = event.target local phase = event.phase if (event.phase == “began”) then local parent = target.parent display.getCurrentStage(): setFocus(target) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif(target.isFocus) then if (phase == “moved”) then target.x = event.x - target.x0 target.y = event.y - target.y0 if (hasCollided(event.target, shadowBall)) then transition.to(event.target, {time = 250, x = shadowBall.x, y = shadowBall.y}) audio.play(sound2) phase = “cancelled” else end elseif(phase == “cancelled” or phase == “cancelled”) then display.getCurrentStage():setFocus(nil) target.isFocus = false end end return true end local function dragCan(event) local target = event.target local phase = event.phase if (event.phase == “began”) then local parent = target.parent display.getCurrentStage(): setFocus(target) target.isFocus = true target.x0 = event.x - target.x target.y0 = event.y - target.y target.xStart = target.x target.yStart = target.y target:toFront() elseif(target.isFocus) then if (phase == “moved”) then target.x = event.x - target.x0 target.y = event.y - target.y0 if (hasCollided(event.target, shadowCan)) then transition.to(event.target, {time = 250, x = shadowCan.x, y = shadowCan.y}) audio.play(sound2) phase = “cancelled” else end elseif(phase == “cancelled” or phase == “cancelled”) then display.getCurrentStage():setFocus(nil) target.isFocus = false end end return true end function scene:create(event) local sceneGroup = self.view bg = display.newImage(“sceneBG.png”) bg.x = 50 bg.y = 50 sceneGroup:insert(bg) message = display.newText(“Game na ba?”, 250, 250, nil, 16) message:setFillColor (0, 0, 0) message.x = 50 message.y = 75 sceneGroup:insert(message) house = display.newImage(“house_red.png”) house.x = 50 house.y = 150 sceneGroup:insert(house) house.myName = “house” shadowHouse = display.newImage(“house_red.png”) shadowHouse.x = 160 shadowHouse.y = 150 shadowHouse:setFillColor(50, 50, 50) shadowHouse.alpha = 0.75 sceneGroup:insert(shadowHouse) shadowHouse.myName = “house” shadowHouse.collision = onLocalCollision ball = display.newImage(“ball_blue.png”) ball.x = 50 ball.y = 250 sceneGroup:insert(ball) ball.myName = “ball” shadowBall = display.newImage(“ball_blue.png”) shadowBall.x = 160 shadowBall.y = 250 shadowBall:setFillColor(50, 50, 50) shadowBall.alpha = 0.75 sceneGroup:insert(shadowBall) shadowBall.myName = “ball” shadowBall.collision = onLocalCollision can = display.newImage(“soda_can.png”) can.x = 50 can.y = 350 sceneGroup:insert(can) can.myName = “can” shadowCan = display.newImage(“soda_can.png”) shadowCan.x = 160 shadowCan.y = 350 shadowCan:setFillColor(50, 50, 50) shadowCan.alpha = 0.75 sceneGroup:insert(shadowCan) shadowCan.myName = “can” shadowCan.collision = onLocalCollision end function scene:show(event) local sceneGroup = self.view sound2 = audio.loadSound(“boing_wav.wav”) house:addEventListener(“touch”, dragHouse) shadowHouse:addEventListener(“collision”, shadowHouse) ball:addEventListener(“touch”, dragBall) shadowBall:addEventListener(“collision”, shadowBall) can:addEventListener(“touch”, dragCan) shadowCan:addEventListener(“collision”, shadowCan) end function scene:hide(event) local sceneGroup = self.view house:removeEventListener(“touch”, dragHouse) shadowHouse:removeEventListener(“collision”, shadowHouse) ball:removeEventListener(“touch”, dragBall) shadowBall:removeEventListener(“collision”, shadowBall) can:removeEventListener(“touch”, dragCan) shadowCan:removeEventListener(“collision”, shadowCan) 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 [/lua]
You probably need to remove focus on an ‘ended’ phase. At the moment you have ‘cancelled’ or ‘cancelled’.
thank you. . . , is there a way to check if the object is in the right place or not? then is it possible when it is correct it moved to another level?
You probably need to remove focus on an ‘ended’ phase. At the moment you have ‘cancelled’ or ‘cancelled’.
thank you. . . , is there a way to check if the object is in the right place or not? then is it possible when it is correct it moved to another level?