[Lua] fix: HelloLua

This commit is contained in:
YuLei 2012-09-10 12:23:45 +08:00
parent d4128975f6
commit 1254d08d5c
1 changed files with 172 additions and 160 deletions

View File

@ -1,21 +1,30 @@
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
local cclog = function(...)
print(string.format(...))
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
print("----------------------------------------")
print("LUA ERROR: " .. tostring(msg) .. "\n")
print(debug.traceback())
print("----------------------------------------")
end
require "hello2"
cclog("result is " .. myadd(3, 5))
local function main()
-- avoid memory leak
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
---------------
local cclog = function(...)
print(string.format(...))
end
local winSize = CCDirector:sharedDirector():getWinSize()
require "hello2"
cclog("result is " .. myadd(3, 5))
-- add the moving dog
local function creatDog()
---------------
local winSize = CCDirector:sharedDirector():getWinSize()
-- add the moving dog
local function creatDog()
local frameWidth = 105
local frameHeight = 95
@ -30,12 +39,12 @@ local function creatDog()
spriteDog.isPaused = false
spriteDog:setPosition(0, winSize.height / 4 * 3)
local animFrames = CCArray:create(2)
local animFrames = CCArray:create()
animFrames:addObject(frame0)
animFrames:addObject(frame1)
local animation = CCAnimation:create(animFrames, 0.5)
local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5)
local animate = CCAnimate:create(animation);
spriteDog:runAction(CCRepeatForever:create(animate))
@ -55,10 +64,10 @@ local function creatDog()
CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)
return spriteDog
end
end
-- create farm
local function createLayerFarm()
-- create farm
local function createLayerFarm()
local layerFarm = CCLayer:create()
-- add in farm background
@ -101,7 +110,7 @@ local function createLayerFarm()
end
local function onTouchMoved(x, y)
cclog("onTouchMoved: %0.2f, %0.2f", x, y)
-- cclog("onTouchMoved: %0.2f, %0.2f", x, y)
if touchBeginPoint then
local cx, cy = layerFarm:getPosition()
layerFarm:setPosition(cx + x - touchBeginPoint.x,
@ -130,11 +139,11 @@ local function createLayerFarm()
layerFarm:setTouchEnabled(true)
return layerFarm
end
end
-- create menu
local function createLayerMenu()
-- create menu
local function createLayerMenu()
local layerMenu = CCLayer:create()
local menuPopup, menuTools, effectID
@ -155,7 +164,7 @@ local function createLayerMenu()
-- add a popup menu
local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png")
menuPopupItem:setPosition(0, 0)
menuPopupItem:registerScriptHandler(menuCallbackClosePopup)
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
menuPopup = CCMenu:createWithItem(menuPopupItem)
menuPopup:setPosition(winSize.width / 2, winSize.height / 2)
menuPopup:setVisible(false)
@ -164,25 +173,28 @@ local function createLayerMenu()
-- add the left-bottom "tools" menu to invoke menuPopup
local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png")
menuToolsItem:setPosition(0, 0)
menuToolsItem:registerScriptHandler(menuCallbackOpenPopup)
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
menuTools = CCMenu:createWithItem(menuToolsItem)
menuTools:setPosition(30, 40)
layerMenu:addChild(menuTools)
return layerMenu
end
-- play background music, preload effect
-- uncomment below for the BlackBerry version
-- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.ogg")
local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.mp3")
SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)
local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("effect1.wav")
SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)
-- run
local sceneGame = CCScene:create()
sceneGame:addChild(createLayerFarm())
sceneGame:addChild(createLayerMenu())
CCDirector:sharedDirector():runWithScene(sceneGame)
end
-- play background music, preload effect
-- uncomment below for the BlackBerry version
-- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.ogg")
local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.mp3")
SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)
local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("effect1.wav")
SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)
-- run
local sceneGame = CCScene:create()
sceneGame:addChild(createLayerFarm())
sceneGame:addChild(createLayerMenu())
CCDirector:sharedDirector():runWithScene(sceneGame)
xpcall(main, __G__TRACKBACK__)