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

来源:4-4 蛇骑士!控制蛇身一

huimou

2016-08-29 18:45

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


写回答 关注

1回答

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

    坐标判断的有点问题

Cocos2d-x游戏开发之贪吃蛇(上)

贪吃蛇都玩过~有没有想过自己实现呢,一起学习来吧

32438 学习 · 122 问题

查看课程

相似问题