我在 Corona SDK 中的第一步和第一次遇到的麻烦。尝试制作两个盒子,我可以通过以下方式移动它们this http://www.ludicroussoftware.com/blog/2011/07/06/simple-oop-with-inheritance-in-corona/OOP教程,但这不起作用。我已经成功创建了两个盒子,但只有一个是可移动的。当我尝试移动另一个时,它没有移动,但第一个正在移动。猜测范围有问题,但无法确定具体位置。
感谢您的任何帮助。
tile.lua源代码:
module (..., package.seeall)
function new(initX, initY)
local scrnWidth = display.stageWidth
local scrnHeight = display.stageHeight
local squareSize = (scrnWidth*0.9)/4
local tile = display.newRect( initY, initY, squareSize, squareSize )
function move(direction)
if direction == "left" then
transition.moveTo(tile, {x = tile.x+squareSize,y = tile.y,time = 200})
elseif direction == "right" then
transition.moveTo(tile, {x = tile.x-squareSize,y = tile.y,time = 200})
elseif direction == "up" then
transition.moveTo(tile, {x = tile.x,y = tile.y-squareSize,time = 200})
elseif direction == "down" then
transition.moveTo(tile, {x = tile.x,y = tile.y+squareSize,time = 200})
end
end
function tile:touch(event)
if event.phase == "began" then
display.getCurrentStage():setFocus( tile )
beginX = event.x
beginY = event.y
end
if event.phase == "ended" then
endX = event.x
endY = event.y
display.getCurrentStage():setFocus(nil)
checkSwipeDirection();
end
end
function checkSwipeDirection()
xDistance = math.abs(endX - beginX) -- math.abs will return the absolute, or non-negative value, of a given value.
yDistance = math.abs(endY - beginY)
if xDistance > yDistance then
if beginX > endX then
move("right",tile)
else
move("left",tile)
end
else
if beginY > endY then
move("up",tile)
else
move("down",tile)
end
end
end
tile:addEventListener("touch", tile)
return tile
end
我在 main.lua 中使用以下代码创建对象:
local tileConst = require("tile")
local tile1 = tileConst.new(100,100)
local tile2 = tileConst.new(200,200)