Lua中的OOP和eventListener(Corona SDK) [英] OOP and eventListener in Lua (Corona SDK)

查看:96
本文介绍了Lua中的OOP和eventListener(Corona SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Corona SDK中的第一步和遇到的麻烦.尝试制作两个可以按照以下

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源代码:

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

,然后在main.lua中使用以下代码创建对象:

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)

推荐答案

在您的tile.lua场景中,声明一个变量以将触摸的精灵保持在module (..., package.seeall)以下:

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

现在如下更改功能move:

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

请注意,我已将transition.moveTo更改为transition.to,将您的transition tile更改为touchedSprite(target).

Note that I've changed transition.moveTo to transition.to and your transition tile to touchedSprite(target).

保持编码.........:)

Keep Coding.................. :)

这篇关于Lua中的OOP和eventListener(Corona SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆