require "Cocos2d"
require "Cocos2dConstants"
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
cclog("----------------------------------------")
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
cclog(debug.traceback())
cclog("----------------------------------------")
return msg
end
local function main()
collectgarbage("collect")
--避免内存泄漏
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
--初始化导演
local director = cc.Director:getInstance()
local glview = director:getOpenGLView()
if glview == nil then
glview = cc.GLView:createWithRect("HelloLua", cc.rect(0, 0, 960, 640))
director:setOpenGLView(glview)
end
glview:setDesignResolutionSize(960, 640, cc.ResolutionPolicy.SHOW_ALL)
--显示帧率
director:setDisplayStats(true)
--设置帧率
director:setAnimationInterval(1.0 / 60)
--增加搜索路径
cc.FileUtils:getInstance():addSearchPath("src")
cc.FileUtils:getInstance():addSearchPath("res")
--创建场景
local scene = require("GameScene")
local gameScene = scene.create()
--运行场景
if cc.Director:getInstance():getRunningScene() then
cc.Director:getInstance():replaceScene(gameScene)
else
cc.Director:getInstance():runWithScene(gameScene)
end
end
xpcall(main, __G__TRACKBACK__)
require "Cocos2d"
require "Cocos2dConstants"
local size = cc.Director:getInstance():getWinSize()
local debug = true
cclog = function(...)
if debug then
print(string.format(...))
end
end
local GameScene = class("GameScene",function()
local scene = cc.Scene:createWithPhysics()
scene:getPhysicsWorld():setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)
return scene
end)
function GameScene:ctor()
self.visibleSize = cc.Director:getInstance():getVisibleSize()
self.origin = cc.Director:getInstance():getVisibleOrigin()
end
function GameScene.create()
local scene = GameScene.new()
scene:addChild(scene:createSprite())
return scene
end
function GameScene.createSprite()
local winSize = cc.Director:getInstance():getWinSize()
local layer = cc.Layer:create()
local sprite1 = cc.Sprite:create("background.png")
sprite1:setPosition(cc.p(winSize.width/2.0, winSize.height/2.0))
layer:addChild(sprite1)
local sprite2 = cc.Sprite:create("tree1.png", cc.rect(604, 38, 302, 295))
sprite2:setPosition(cc.p(200, 230))
layer:addChild(sprite2, 1)
local tree = cc.Director:getInstance():getTextureCache():addImage("tree1.png")
local sprite3 = cc.Sprite:createWithTexture(tree, cc.rect(73, 72, 182, 270))
sprite3:setPosition(cc.p(500, 200))
layer:addChild(sprite3, 1)
local mountain = cc.Sprite:create("SpriteSheet.png", cc.rect(2, 391, 924, 388))
mountain:setPosition(cc.p(-200, 80))
mountain:setAnchorPoint(cc.p(0, 0))
layer:addChild(mountain, 0)
local cache = cc.Director:getInstance():getTextureCache():addImage("SpriteSheet.png")
local hero = cc.Sprite:createWithTexture(cache, cc.rect(2, 1706, 391, 327))
hero:setPosition(cc.p(750, 200))
layer:addChild(hero)
local frameCache = cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("SpriteSheet.plist")
local mountain1 = cc.Sprite:createWithSpriteFrameName("mountain1.png")
mountain1:setPosition(cc.p(-200, 80))
mountain1:setAnchorPoint(cc.p(0, 0))
layer:addChild(mountain1)
local spriteFrame = cc.SpriteFrameCache:getInstance():getSpriteFrameByName("hero1.png")
local hero1 = cc.Sprite:createWithSpriteFrame(spriteFrame)
hero1:setPosition(cc.p(800, 200))
hero1:setAnchorPoint(cc.p(0, 0))
layer:addChild(hero1)
return layer
end
return GameScene