在Corona SDK中,如何在触摸背景屏幕时让对象保持移动?

时间:2016-04-20 03:03:58

标签: lua corona

以下是这种情况。只要我触摸屏幕,我想要一个方形物体在y方向上连续移动。基于代码,我有它应该给我一个方形对象的连续移动,但是,只有当我用手指在触摸屏上滑动时,对象才会移动。当我在同一个地方触摸屏幕时,我希望方形物体不断移动。

我尝试使用timer和runtime事件监听器都无济于事。这里的解决方法是什么?

var paidRef=new Firebase("https://app.firebaseio.com/paidUsers");
var ref1=new Firebase("https://app.firebaseio.com/tempUser");

function checkPaidUsers(res,callback){

 ref1.orderByChild('userId').equalTo(jsonData.userId).once('child_added', function(snap) {

 registeredUser=true;
  paidRef.child(jsonData.userId).once('value', function(snapshot) {
    var paidFlag = false;
    if (snapshot.val() !== null) {
      paidFlag = true;
    }
    callback(registeredUser, paidFlag, res)
  })
})
}
checkPaidUsers( res,function(registeredUser,paidFlag) {

  if(registeredUser!=true)
  {
     newUser=true;


  }
return res.send({paidFlag:paidFlag,registeredUser:registeredUser,newUser:newUser});})

2 个答案:

答案 0 :(得分:0)

这只是用你的对象替换square:

_W = display.contentWidth 
_H = display.contentHeight 


local background = display.newRect(0,0,_W,_H)
background.anchorX = 0
background.anchorY = 0

local square = display.newRect(200,200,_W/3,_H/3)
square:setFillColor( 1,1,0 )

local squareTimer
local touchStarted = false


local holding = 'false'
local function startMove(event)

     if event.phase == 'began' then
        --Runtime:addEventListener('enterFrame',squareMoveUp)  
        squareTimer = timer.performWithDelay(200,squareMoveUp,0)
        touchStarted = true
            local function squareMoveUp( )
                square.y = square.y - 0.02
            end

        for i = 1,1000 do
          if touchStarted then 
                    squareTimer = timer.performWithDelay(200,squareMoveUp,1)
        end
    end

    elseif ( event.phase == "ended" ) then
            touchStarted = false
     end
     return true
end 

background:addEventListener('touch',startMove)

答案 1 :(得分:0)

您需要更改代码,如下所示。

_W = display.contentWidth 
_H = display.contentHeight 


local background = display.newRect(0,0,_W,_H)
background.anchorX = 0
background.anchorY = 0

local square = display.newRect(200,200,_W/3,_H/3)
square:setFillColor( 1,1,0 )

local touchStarted = false

local function startMove(event)

        local function squareMoveUp( )
          if touchStarted then 
            square.y = square.y - 10
          end
        end
 if event.phase == 'began' then
       touchStarted = true
 elseif ( event.phase == "ended" ) then
        touchStarted = false
 end

timer.performWithDelay(200,squareMoveUp,0)
end 

background:addEventListener('touch',startMove)

或者您可以更改squareMoveUp中的代码,例如:transition.moveTo( square, { y=square.y-10, 1 } )而不是square.y = square.y - 10 它会更顺利。