【问题标题】:In Corona SDK, How do I get an object to keep moving while touching the background screen?在 Corona SDK 中,如何让对象在触摸背景屏幕时保持移动?
【发布时间】:2016-04-24 19:34:03
【问题描述】:

情况是这样的。只要我触摸屏幕,我就希望一个方形物体在 y 方向上不断移动。根据代码,我应该给我一个正方形对象的连续移动,但是,只有当我用手指在触摸屏上滑动时,对象才会移动。当我在同一个位置触摸屏幕时,我希望方形对象不断移动。

我尝试同时使用计时器和运行时事件侦听器,但均无济于事。这里有什么解决方法?

_W = display.contentWidth 
_H = display.contentHeight 


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


local squareTimer

local function squareMoveUp()
     square.y = square.y - 5
end    

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

     if event.phase == 'began' then
        --Runtime:addEventListener('enterFrame',squareMoveUp)  
        squareTimer = timer.performWithDelay(200,squareMoveUp,0)

     elseif event.phase == 'ended' then
        --Runtime:removeEventListener('enterFrame',squareMoveUp)

     end
     return true
end 

background:addEventListener('touch',squareMoveUp)

【问题讨论】:

  • background 应该全屏显示。
  • 是的。我只是没有包含该代码。

标签: lua coronasdk


【解决方案1】:

这只是用你的对象替换正方形:

_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)

【讨论】:

    【解决方案2】:

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

    _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 会更流畅。

    【讨论】:

      猜你喜欢
      • 2014-06-16
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多