--在图片里添加四个端点,通过四个端点来获取图片坐标点。
function GameScene:createLayerFarm() local layerFarm = cc.Layer:create() --图片精灵 width 100 height 200 self.shell_22 = cc.Sprite:create("100-200.png") self.shell_22:setPosition(cc.p(200,200)) self.shell_22:setAnchorPoint(cc.p(0.5, 0)) self:addChild(self.shell_22, 20) --创建ImageView对象 self.up_left = ccui.ImageView:create() self.up_right = ccui.ImageView:create() self.down_left = ccui.ImageView:create() self.down_right = ccui.ImageView:create() --设置四个端点的坐标 [100 图片的宽度 200 图片的高度] self.up_left:setPosition(cc.p(0,200)) --右上角 self.up_right:setPosition(cc.p(100,200)) --右下角 self.down_left:setPosition(cc.p(0,0)) --左下角 self.down_right:setPosition(cc.p(100,0)) --右下角 --添加到旋转的图片中 self.shell_22:addChild(self.up_left) self.shell_22:addChild(self.up_right) self.shell_22:addChild(self.down_left) self.shell_22:addChild(self.down_right) local upLeftPos = self.up_left:getWorldPosition() print("upLeftPos : ", upLeftPos.x, " upLeftPos : ", upLeftPos.y) local up_rightPos = self.up_right:getWorldPosition() print("up_rightPos : ", up_rightPos.x, " up_rightPos : ", up_rightPos.y) local down_leftPos = self.down_left:getWorldPosition() print("down_leftPos : ", down_leftPos.x, " down_leftPos : ", down_leftPos.y) local down_rightPos = self.down_right:getWorldPosition() print("down_rightPos : ", down_rightPos.x, " down_rightPos : ", down_rightPos.y) --触摸处理事件处理方法 local function onTouchBegan(touch, event) local location = touch:getLocation() -- CCTOUCHBEGAN event must return true ------------------------------------------------------------------- local originX, originY = self.shell_22:getPosition() local direction = cc.p(location.x - originX, location.y - originY) local degree = math.atan2(direction.y, direction.x) print("degree : ", degree) self.shell_22:setRotation(90-degree*180/math.acos(-1.0)) --获取图片旋转后的图片端点 local upLeftPos = self.up_left:getWorldPosition() print("upLeftPos : ", upLeftPos.x, " upLeftPos : ", upLeftPos.y) local up_rightPos = self.up_right:getWorldPosition() print("up_rightPos : ", up_rightPos.x, " up_rightPos : ", up_rightPos.y) local down_leftPos = self.down_left:getWorldPosition() print("down_leftPos : ", down_leftPos.x, " down_leftPos : ", down_leftPos.y) local down_rightPos = self.down_right:getWorldPosition() print("down_rightPos : ", down_rightPos.x, " down_rightPos : ", down_rightPos.y) --通过上面获的端点来绘制矩形区域 local drawNode = cc.DrawNode:create() local vertices = { cc.p(down_leftPos.x,down_leftPos.y), --左下角 cc.p(down_rightPos.x,down_rightPos.y), --右下角 cc.p(up_rightPos.x,up_rightPos.y), --右上角 cc.p(upLeftPos.x,upLeftPos.y) --左上角 } --绘制边框 drawNode:drawPoly(vertices, 4, true, cc.c4f(1,0,0,1)) self:addChild(drawNode, 20) return true end return layerFarm end