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