问题
My first steps in Corona SDK and first troubles. Trying to make two boxes which I can move by following this OOP tutorial, but this doesn't work. I have successfully created two boxes but only one is movable. When I try to move another one it doesn't moved but first one is moving. Guess there's problem with scopes but can't figure where is exactly. Thank you for any help.
tile.lua source code:
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
and I create objects by using the following code in main.lua:
local tileConst = require("tile")
local tile1 = tileConst.new(100,100)
local tile2 = tileConst.new(200,200)
回答1:
In your tile.lua
scene, declare a variable to hold touched sprite just below module (..., package.seeall)
:
local touchedSprite -- my temporary sprite variable
Then assign the target sprite to the above variable inside the function as:
function tile:touch(event)
touchedSprite = event.target -- Assigning target to variable
...
end
Now change the function move
as follows:
function move(direction)
if direction == "left" then
transition.to(touchedSprite, {x = touchedSprite.x+squareSize,y = touchedSprite.y,time = 200})
elseif direction == "right" then
transition.to(touchedSprite, {x = touchedSprite.x-squareSize,y = touchedSprite.y,time = 200})
elseif direction == "up" then
transition.to(touchedSprite, {x = touchedSprite.x,y = touchedSprite.y-squareSize,time = 200})
elseif direction == "down" then
transition.to(touchedSprite, {x = touchedSprite.x,y = touchedSprite.y+squareSize,time = 200})
end
end
Note that I've changed transition.moveTo
to transition.to
and your transition tile
to touchedSprite(target).
Keep Coding.................. :)
来源:https://stackoverflow.com/questions/21028931/oop-and-eventlistener-in-lua-corona-sdk