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
坐标判断的有点问题