为了账号安全,请及时绑定邮箱和手机立即绑定

cocos2d-lua贪吃蛇头和身体重叠停在正中央不动?

初学cocos2d-lua,慕课网照着教程打的,代码对过应该没问题啊,为什么教程上run了贪吃蛇就往左走了,我的蛇头蛇身重叠在正中央不动?
命令行窗口显示:
[LUA-print] [string "src/app/Snake.lua"]:61: attempt to call field 'BodyArray' (
a table value)
stack traceback:
[string "src/app/scenes/MainScene.lua"]:13: in function <[string "src/ap
p/scenes/MainScene.lua"]:12>

MainScene.lua代码如下
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)

local tick = function()--第12行代码
self.snake:Update()--第13行代码
end

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

function MainScene:onExit()
end

return MainScene

Snake.lua代码如下
local Snake = class("Snake")
local Body = require("app.Body")

local cInitLen = 3

function Snake:ctor(node)

self.BodyArray = {}
self.node = node
self.MoveDir = "left"

for i = 1,cInitLen do
self:Grow(i == 1)
end
end
function Snake:GetTailGrid()
if #self.BodyArray == 0 then
return 0,0
end

local tail = self.BodyArray[#self.BodyArray]

return tail.X,tail.Y

end


function Snake:Grow(isHead)
local tailX,tailY = self:GetTailGrid()
local body = Body.new(self,tailX,tailY,self.node,isHead)

table.insert(self.BodyArray,body)

end

local function OffsetGridByDir(x,y,dir)

if dir == "left" then
return x -1,y
elseif dir == "right" then
return x +1,y
elseif dir == "up" then
return x,y -1
elseif dir == "down" then
return x,y +1
end

print("unknown dir",dir)

return x,y
end

function Snake:Update()

if #self.BodyArray == 0 then
return
end

for i = #self.BodyArray,1,-1 do

local body = self.BodyArray{i}--第61行代码

if i == 1 then
body.X,body.Y = OffsetGridByDir(body.X,body.Y,self.MoveDir)
else
local front = self.BodyArray[i-1]
body.X,body.Y = front.X,front.Y

end

body:Update()

end

end


return Snake

正在回答

2 回答

local body = self.BodyArray{i}--第61行代码

-- carefully look......   [i]

0 回复 有任何疑惑可以回复我~
#1

糖醋肉 提问者

非常感谢!
2015-09-21 回复 有任何疑惑可以回复我~

大神来给我解答一下吧,谢谢啦

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

cocos2d-lua贪吃蛇头和身体重叠停在正中央不动?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信