问答详情
源自:4-3 蛇类二

报错了,错误信息看不懂

[LUA-print] [string "src/app/Body.lua"]:27: attempt to index local 'node' (a number value)

stack traceback:

        [string "src/framework/functions.lua"]:336: in function 'new'

        [string "src/app/Snake.lua"]:28: in function 'Grow'

        [string "src/app/Snake.lua"]:12: in function 'ctor'

        [string "src/framework/functions.lua"]:336: in function 'new'

        [string "src/app/scenes/MainScene.lua"]:10: in function 'onEnter'

        [string "src/framework/cocos2dx/NodeEx.lua"]:143: in function 'listener_'      

        [string "src/framework/cocos2dx/NodeEx.lua"]:428: in function 'EventDispatcher'

        [string "src/framework/cocos2dx/NodeEx.lua"]:271: in function <[string "src/framework/cocos2dx/NodeEx.lua"]:270>


提问者:慕无忌4252426 2016-05-07 10:13

个回答

  • 一滴清殇
    2016-08-02 09:50:09
    已采纳

    问题解决没有,我也和你出现了差不多的错误

  • FLY8
    2017-01-25 19:57:45

    求问如何解决的。。跟着老师代码一模一样。但是没有出现body

  • 水里有条鱼
    2016-05-07 10:46:13

    其实我也是新手,但是我看了一下,它好像是说你的node给了一个数值,你可以去MainScene那看一下snake的创建是不是self.snake = Snake.new(self),大概是这个原因吧

  • 慕无忌4252426
    2016-05-07 10:15:08

    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 0,0

    end


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

    local body = self.BodyArray[i]


    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


  • 慕无忌4252426
    2016-05-07 10:14:41

    local Body = class("Body")


    local cGridSize = 33

    local scaleRate = 1/display.contentScaleFactor


    function Grid2Pos( x,y )

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

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


    local finalX = origin.x+visibleSize.width/2+x*cGridSize*scaleRate

    local finalY = origin.y+visibleSize.height/2+y*cGridSize*scaleRate


    return finalX,finalY

    end


    function Body:ctor( snake,x,y,node,isHead )

    self.snake = snake

    self.X = x

    self.Y = y


    if ishead then

    self.sp = cc.Sprite:create("head_l.png")

    else

    self.sp = cc.Sprite:create("body.png")

    end


    node:addChild(self.sp)


    self:Update()

    end


    function Body:Update()

    local posx,posy = Grid2Pos(self.X,self.Y)


    self.sp:setPosition(posx,posy)

    end


    return Body