issue #2433:Modify template/multi_platform_lua/Resources/hello.lua

This commit is contained in:
samuele3hu 2013-08-30 17:18:47 +08:00
parent 5ee2a7e58e
commit 564a77edf6
1 changed files with 44 additions and 43 deletions

View File

@ -1,11 +1,15 @@
require "AudioEngine"
require "Cocos2d"
-- cclog
cclog = function(...)
print(string.format(...))
end
-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
print("----------------------------------------")
print("LUA ERROR: " .. tostring(msg) .. "\n")
print(debug.traceback())
print("----------------------------------------")
cclog("----------------------------------------")
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
cclog(debug.traceback())
cclog("----------------------------------------")
end
local function main()
@ -13,17 +17,13 @@ local function main()
collectgarbage("setpause", 100)
collectgarbage("setstepmul", 5000)
local cclog = function(...)
print(string.format(...))
end
require "hello2"
cclog("result is " .. myadd(3, 5))
cclog("result is " .. myadd(1, 1))
---------------
local visibleSize = CCDirector:getInstance():getVisibleSize()
local origin = CCDirector:getInstance():getVisibleOrigin()
local visibleSize = cc.Director:getInstance():getVisibleSize()
local origin = cc.Director:getInstance():getVisibleOrigin()
-- add the moving dog
local function creatDog()
@ -31,24 +31,25 @@ local function main()
local frameHeight = 95
-- create dog animate
local textureDog = CCTextureCache:getInstance():addImage("dog.png")
local rect = CCRect(0, 0, frameWidth, frameHeight)
local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect)
rect = CCRect(frameWidth, 0, frameWidth, frameHeight)
local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect)
local textureDog = cc.TextureCache:getInstance():addImage("dog.png")
local rect = cc.rect(0, 0, frameWidth, frameHeight)
local frame0 = cc.SpriteFrame:createWithTexture(textureDog, rect)
rect = cc.rect(frameWidth, 0, frameWidth, frameHeight)
local frame1 = cc.SpriteFrame:createWithTexture(textureDog, rect)
local spriteDog = CCSprite:createWithSpriteFrame(frame0)
local spriteDog = cc.Sprite:createWithSpriteFrame(frame0)
spriteDog.isPaused = false
spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)
--[[
local animFrames = CCArray:create()
animFrames:addObject(frame0)
animFrames:addObject(frame1)
]]--
local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5)
local animate = CCAnimate:create(animation);
spriteDog:runAction(CCRepeatForever:create(animate))
local animation = cc.Animation:createWithSpriteFrames({frame0,frame1}, 0.5)
local animate = cc.Animate:create(animation);
spriteDog:runAction(cc.RepeatForever:create(animate))
-- moving dog at every frame
local function tick()
@ -63,34 +64,34 @@ local function main()
spriteDog:setPositionX(x)
end
CCDirector:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
return spriteDog
end
-- create farm
local function createLayerFarm()
local layerFarm = CCLayer:create()
local layerFarm = cc.Layer:create()
-- add in farm background
local bg = CCSprite:create("farm.jpg")
local bg = cc.Sprite:create("farm.jpg")
bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2)
layerFarm:addChild(bg)
-- add land sprite
for i = 0, 3 do
for j = 0, 1 do
local spriteLand = CCSprite:create("land.png")
local spriteLand = cc.Sprite:create("land.png")
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
layerFarm:addChild(spriteLand)
end
end
-- add crop
local frameCrop = CCSpriteFrame:create("crop.png", CCRect(0, 0, 105, 95))
local frameCrop = cc.SpriteFrame:create("crop.png", cc.rect(0, 0, 105, 95))
for i = 0, 3 do
for j = 0, 1 do
local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop);
local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop);
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
layerFarm:addChild(spriteCrop)
end
@ -146,37 +147,37 @@ local function main()
-- create menu
local function createLayerMenu()
local layerMenu = CCLayer:create()
local layerMenu = cc.Layer:create()
local menuPopup, menuTools, effectID
local function menuCallbackClosePopup()
-- stop test sound effect
AudioEngine.stopEffect(effectID)
cc.SimpleAudioEngine:getInstance():stopEffect(effectID)
menuPopup:setVisible(false)
end
local function menuCallbackOpenPopup()
-- loop test sound effect
local effectPath = CCFileUtils:getInstance():fullPathForFilename("effect1.wav")
effectID = AudioEngine.playEffect(effectPath)
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
menuPopup:setVisible(true)
end
-- add a popup menu
local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png")
local menuPopupItem = cc.MenuItemImage:create("menu2.png", "menu2.png")
menuPopupItem:setPosition(0, 0)
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
menuPopup = CCMenu:createWithItem(menuPopupItem)
menuPopup = cc.Menu:create(menuPopupItem)
menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
menuPopup:setVisible(false)
layerMenu:addChild(menuPopup)
-- add the left-bottom "tools" menu to invoke menuPopup
local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png")
local menuToolsItem = cc.MenuItemImage:create("menu1.png", "menu1.png")
menuToolsItem:setPosition(0, 0)
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
menuTools = CCMenu:createWithItem(menuToolsItem)
menuTools = cc.Menu:create(menuToolsItem)
local itemWidth = menuToolsItem:getContentSize().width
local itemHeight = menuToolsItem:getContentSize().height
menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)
@ -189,16 +190,16 @@ local function main()
-- uncomment below for the BlackBerry version
-- local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")
local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.mp3")
AudioEngine.playMusic(bgMusicPath, true)
local effectPath = CCFileUtils:getInstance():fullPathForFilename("effect1.wav")
AudioEngine.preloadEffect(effectPath)
local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)
-- run
local sceneGame = CCScene:create()
local sceneGame = cc.Scene:create()
sceneGame:addChild(createLayerFarm())
sceneGame:addChild(createLayerMenu())
CCDirector:getInstance():runWithScene(sceneGame)
cc.Director:getInstance():runWithScene(sceneGame)
end
xpcall(main, __G__TRACKBACK__)