为啥我的上下左右行,page up 和down 没反应

来源:4-3 控制光标

慕无忌4252426

2016-05-08 20:38

require "app.Common"

local Fence = require "app.Fence"

require "app.CollideManager"

local Block = require "app.Block"

 

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

  return display.newScene("EditorScene")

end)


 function EditorScene:onEnter()


  self.fence = Fence.new(cBound,self)

  self.curX = 0

  self.curY = 0

  self.curIndex = 0

  self:SwitchCursor(1)

  self:ProcessInput()

end


local cMaxBlock = 3


function EditorScene:SwitchCursor( delta )

if self.cur == nil then

self.cur = Block.new(self)

end

local newIndex = self.curIndex + delta

newIndex = math.max(newIndex,1)

newIndex = math.min(newIndex,cMaxBlock)


self.curIndex = newIndex


self.cur:Set(newIndex)

self.cur:SetPos(self.curX,self.curY)


end


function EditorScene:MoveCursor( deltaX,deltaY )

self.cur:SetPos(self.curX+deltaX,self.curY+deltaY)

self.curX = self.cur.x

self.curY = self.cur.y

end


function EditorScene:ProcessInput(  )

local function keyboardPressed(keyCode,event )


--up

if keyCode == 28 then

self:MoveCursor(0,1)

--down

elseif keyCode == 29 then

self:MoveCursor(0,-1)

--left

elseif keyCode == 26 then

self:MoveCursor(-1,0)

--right

elseif keyCode == 27 then

self:MoveCursor(1,0)

--page up

elseif keyCode == 38 then

self:SwitchCursor(-1)

--page down

elseif keyCode == 44 then

self:SwitchCursor(1)

end

end


local listener = cc.EventListenerKeyboard:create()

listener:registerScriptHandler(keyboardPressed,cc.Handler.EVENT_KEYBOARD_PRESSED)

local eventDispatcher = self:getEventDispatcher()

eventDispatcher:addEventListenerWithSceneGraphPriority(listener,self)

end


return EditorScene


写回答 关注

2回答

Cocos2d-x游戏开发入门-贪吃蛇(下)

本课程将对上一门课进行改造,加入新的元素,符合游戏特性

15572 学习 · 15 问题

查看课程

相似问题