问答详情
源自:4-4 蛇骑士!控制蛇身一

为什么我的蛇只能左右 ,而不能上下移动,运行没有报错,求大神解答

local Snake = require "app.Snake"

local MainScene = class("MainScene", function()

    return display.newScene("MainScene")

end)


local cMoveSpeed = 0.3


function MainScene:onEnter()

self.Snake = Snake.new(self)


self:ProcessInput()



local tick = function()

self.Snake:Update()

end


cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,cMoveSpeed,false)


end

-------------------------------------------------------------

local function vector2Dir(x,y)


if math.abs(x) > math.abs(y) then

if x < 0 then

return "left"

else

return "right"

end


else


if y < 0 then

return "up"

else

return "down"

end

end


end

-------------------------------------------------------

function MainScene:ProcessInput()


local function onTouchBegan(touch,event)


local location = touch:getLocation()

local visibleSize = cc.Director:getInstance():getVisibleSize()

local origin = cc.Director:getInstance():getVisibleOrigin()


local finalX = location.x - (origin.x + visibleSize.width/2)

local finalY = location.y - (origin.y + visibleSize.height/2)


local dir = vector2Dir(finalX,finalY)

self.Snake:SetDir(dir)

end

local listener = cc.EventListenerTouchOneByOne:create()

listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)


local eventDispatcher = self:getEventDispatcher()

eventDispatcher:addEventListenerWithSceneGraphPriority(listener,self)



end


提问者:huimou 2016-08-29 18:45

个回答

  • 慕粉114840189
    2017-01-14 15:01:52

    坐标判断的有点问题