2014-10-11 02:55:04 +08:00
|
|
|
|
|
|
|
cc.FileUtils:getInstance():addSearchPath("src")
|
|
|
|
cc.FileUtils:getInstance():addSearchPath("res")
|
|
|
|
|
|
|
|
-- CC_USE_DEPRECATED_API = true
|
|
|
|
require "cocos.init"
|
2014-03-26 19:42:07 +08:00
|
|
|
|
2014-03-10 19:33:57 +08:00
|
|
|
-- cclog
|
|
|
|
cclog = function(...)
|
|
|
|
print(string.format(...))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- for CCLuaEngine traceback
|
|
|
|
function __G__TRACKBACK__(msg)
|
|
|
|
cclog("----------------------------------------")
|
|
|
|
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
|
|
|
|
cclog(debug.traceback())
|
|
|
|
cclog("----------------------------------------")
|
2014-04-25 19:42:31 +08:00
|
|
|
return msg
|
2014-03-10 19:33:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function main()
|
2014-03-11 16:52:06 +08:00
|
|
|
collectgarbage("collect")
|
2014-03-10 19:33:57 +08:00
|
|
|
-- avoid memory leak
|
|
|
|
collectgarbage("setpause", 100)
|
|
|
|
collectgarbage("setstepmul", 5000)
|
2014-07-11 16:00:02 +08:00
|
|
|
|
|
|
|
-- initialize director
|
|
|
|
local director = cc.Director:getInstance()
|
|
|
|
local glview = director:getOpenGLView()
|
|
|
|
if nil == glview then
|
2014-08-07 13:54:54 +08:00
|
|
|
glview = cc.GLViewImpl:createWithRect("HelloLua", cc.rect(0,0,900,640))
|
2014-07-11 16:00:02 +08:00
|
|
|
director:setOpenGLView(glview)
|
|
|
|
end
|
|
|
|
|
|
|
|
glview:setDesignResolutionSize(480, 320, cc.ResolutionPolicy.NO_BORDER)
|
|
|
|
|
|
|
|
--turn on display FPS
|
|
|
|
director:setDisplayStats(true)
|
|
|
|
|
|
|
|
--set FPS. the default value is 1.0/60 if you don't call this
|
|
|
|
director:setAnimationInterval(1.0 / 60)
|
|
|
|
|
|
|
|
cc.FileUtils:getInstance():addSearchPath("src")
|
|
|
|
cc.FileUtils:getInstance():addSearchPath("res")
|
2014-03-11 16:52:06 +08:00
|
|
|
local schedulerID = 0
|
2014-03-10 19:33:57 +08:00
|
|
|
--support debug
|
|
|
|
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
|
2014-10-11 02:55:04 +08:00
|
|
|
if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or
|
2014-03-10 19:33:57 +08:00
|
|
|
(cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or
|
|
|
|
(cc.PLATFORM_OS_MAC == targetPlatform) then
|
|
|
|
cclog("result is ")
|
|
|
|
--require('debugger')()
|
2014-10-11 02:55:04 +08:00
|
|
|
|
2014-03-10 19:33:57 +08:00
|
|
|
end
|
|
|
|
require "hello2"
|
|
|
|
cclog("result is " .. myadd(1, 1))
|
|
|
|
|
|
|
|
---------------
|
|
|
|
|
|
|
|
local visibleSize = cc.Director:getInstance():getVisibleSize()
|
|
|
|
local origin = cc.Director:getInstance():getVisibleOrigin()
|
|
|
|
|
|
|
|
-- add the moving dog
|
2014-07-28 23:37:55 +08:00
|
|
|
local function createDog()
|
2014-03-10 19:33:57 +08:00
|
|
|
local frameWidth = 105
|
|
|
|
local frameHeight = 95
|
|
|
|
|
|
|
|
-- create dog animate
|
2014-03-26 19:42:07 +08:00
|
|
|
local textureDog = cc.Director:getInstance():getTextureCache():addImage("dog.png")
|
2014-03-10 19:33:57 +08:00
|
|
|
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 = 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 = 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()
|
|
|
|
if spriteDog.isPaused then return end
|
|
|
|
local x, y = spriteDog:getPosition()
|
|
|
|
if x > origin.x + visibleSize.width then
|
|
|
|
x = origin.x
|
|
|
|
else
|
|
|
|
x = x + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
spriteDog:setPositionX(x)
|
|
|
|
end
|
|
|
|
|
2014-03-11 16:52:06 +08:00
|
|
|
schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
|
2014-03-10 19:33:57 +08:00
|
|
|
|
|
|
|
return spriteDog
|
|
|
|
end
|
|
|
|
|
|
|
|
-- create farm
|
|
|
|
local function createLayerFarm()
|
|
|
|
local layerFarm = cc.Layer:create()
|
|
|
|
|
|
|
|
-- add in farm background
|
|
|
|
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 = 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 = cc.SpriteFrame:create("crop.png", cc.rect(0, 0, 105, 95))
|
|
|
|
for i = 0, 3 do
|
|
|
|
for j = 0, 1 do
|
|
|
|
local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop);
|
|
|
|
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteCrop)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add moving dog
|
2014-07-28 23:37:55 +08:00
|
|
|
local spriteDog = createDog()
|
2014-03-10 19:33:57 +08:00
|
|
|
layerFarm:addChild(spriteDog)
|
|
|
|
|
|
|
|
-- handing touch events
|
|
|
|
local touchBeginPoint = nil
|
|
|
|
local function onTouchBegan(touch, event)
|
|
|
|
local location = touch:getLocation()
|
|
|
|
cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
|
|
|
|
touchBeginPoint = {x = location.x, y = location.y}
|
|
|
|
spriteDog.isPaused = true
|
|
|
|
-- CCTOUCHBEGAN event must return true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function onTouchMoved(touch, event)
|
|
|
|
local location = touch:getLocation()
|
|
|
|
cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
|
|
|
|
if touchBeginPoint then
|
|
|
|
local cx, cy = layerFarm:getPosition()
|
|
|
|
layerFarm:setPosition(cx + location.x - touchBeginPoint.x,
|
|
|
|
cy + location.y - touchBeginPoint.y)
|
|
|
|
touchBeginPoint = {x = location.x, y = location.y}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function onTouchEnded(touch, event)
|
|
|
|
local location = touch:getLocation()
|
|
|
|
cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y)
|
|
|
|
touchBeginPoint = nil
|
|
|
|
spriteDog.isPaused = false
|
|
|
|
end
|
|
|
|
|
|
|
|
local listener = cc.EventListenerTouchOneByOne:create()
|
|
|
|
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
|
|
|
|
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
|
|
|
|
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
|
|
|
|
local eventDispatcher = layerFarm:getEventDispatcher()
|
|
|
|
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm)
|
2014-10-11 02:55:04 +08:00
|
|
|
|
2014-03-11 16:52:06 +08:00
|
|
|
local function onNodeEvent(event)
|
|
|
|
if "exit" == event then
|
|
|
|
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerID)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
layerFarm:registerScriptHandler(onNodeEvent)
|
2014-03-10 19:33:57 +08:00
|
|
|
|
|
|
|
return layerFarm
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- create menu
|
|
|
|
local function createLayerMenu()
|
|
|
|
local layerMenu = cc.Layer:create()
|
|
|
|
|
|
|
|
local menuPopup, menuTools, effectID
|
|
|
|
|
|
|
|
local function menuCallbackClosePopup()
|
|
|
|
-- stop test sound effect
|
|
|
|
cc.SimpleAudioEngine:getInstance():stopEffect(effectID)
|
|
|
|
menuPopup:setVisible(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function menuCallbackOpenPopup()
|
|
|
|
-- loop test sound effect
|
|
|
|
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
|
|
|
|
effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
|
|
|
|
menuPopup:setVisible(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add a popup menu
|
|
|
|
local menuPopupItem = cc.MenuItemImage:create("menu2.png", "menu2.png")
|
|
|
|
menuPopupItem:setPosition(0, 0)
|
|
|
|
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
|
|
|
|
menuPopup = cc.Menu:create(menuPopupItem)
|
|
|
|
menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
|
|
|
|
menuPopup:setVisible(false)
|
|
|
|
layerMenu:addChild(menuPopup)
|
2014-10-11 02:55:04 +08:00
|
|
|
|
2014-03-10 19:33:57 +08:00
|
|
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
|
|
|
local menuToolsItem = cc.MenuItemImage:create("menu1.png", "menu1.png")
|
|
|
|
menuToolsItem:setPosition(0, 0)
|
|
|
|
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
|
|
|
|
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)
|
|
|
|
layerMenu:addChild(menuTools)
|
|
|
|
|
|
|
|
return layerMenu
|
|
|
|
end
|
|
|
|
|
|
|
|
-- play background music, preload effect
|
2014-10-11 02:55:04 +08:00
|
|
|
local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
|
2014-05-28 10:27:03 +08:00
|
|
|
cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)
|
2014-03-10 19:33:57 +08:00
|
|
|
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
|
|
|
|
cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)
|
|
|
|
|
|
|
|
-- run
|
|
|
|
local sceneGame = cc.Scene:create()
|
|
|
|
sceneGame:addChild(createLayerFarm())
|
|
|
|
sceneGame:addChild(createLayerMenu())
|
2014-10-11 02:55:04 +08:00
|
|
|
|
2014-03-10 19:33:57 +08:00
|
|
|
if cc.Director:getInstance():getRunningScene() then
|
|
|
|
cc.Director:getInstance():replaceScene(sceneGame)
|
|
|
|
else
|
|
|
|
cc.Director:getInstance():runWithScene(sceneGame)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-04-25 19:42:31 +08:00
|
|
|
local status, msg = xpcall(main, __G__TRACKBACK__)
|
|
|
|
if not status then
|
|
|
|
error(msg)
|
|
|
|
end
|