贴上代码
Body.lua
local Body = class(“Body”,cc.load(“mvc”).ViewBase)
local cGridSize=33
local scaleRate = 1/display.contentScaleFactor
function Grid2Pos(x,y)
local visibleSize = cc.Diirector: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.png")
else
self.sp = cc.Sprite:create("body.png")
end
node:addChild(self.sp)
self:Update()
end
function Body:Update()
local posx,posy = cc.exports.Grid2Pos(self.X,self.Y)
self.sp:setPosition(posx,posy)
end
return Body
TitleScene.lua
local Body = import("…Body")
local TitleScene = class(“TitleScene”,cc.load(“mvc”).ViewBase,function()
return display.newScene(“TitleScene”)
end)
function TitleScene:onEnter()
print(Body)
display.newSprite(“bg.png”)
:move(display.center)
:addTo(self)
local playButton = cc.MenuItemImage:create("btn_start.png", "btn_start.png")
:onClicked(function()
self.snake = Body.new(nil,5,0,self,false)
-- self:getApp():enterScene("PlayScene")
end)
cc.Menu:create(playButton)
:move(display.cx,display.cy)
:addTo(self)
end
return TitleScene
报错
load view error: USE " cc.exports.Grid2Pos = value " INSTEAD OF SET GLOBAL VARIABLE
[LUA-print] AppBase:createView() - not found view “./TitleScene” in search paths “app.views”
stack traceback:
[string “packages/mvc/AppBase.lua”]:61: in function ‘createView’
[string “packages/mvc/AppBase.lua”]:41: in function ‘enterScene’
[string “packages/mvc/AppBase.lua”]:37: in function ‘run’
在TitleScene.lua去掉local Body = import("…Body")就可以运行