2013-04-27 09:48:35 +08:00
|
|
|
-- cclog
|
|
|
|
cclog = function(...)
|
|
|
|
print(string.format(...))
|
|
|
|
end
|
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- for CCLuaEngine traceback
|
|
|
|
function __G__TRACKBACK__(msg)
|
2013-04-27 09:48:35 +08:00
|
|
|
cclog("----------------------------------------")
|
|
|
|
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
|
|
|
|
cclog(debug.traceback())
|
|
|
|
cclog("----------------------------------------")
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local function main()
|
|
|
|
-- avoid memory leak
|
|
|
|
collectgarbage("setpause", 100)
|
|
|
|
collectgarbage("setstepmul", 5000)
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
require "hello2"
|
2013-04-27 09:48:35 +08:00
|
|
|
cclog("result is " .. myadd(1, 1))
|
2011-06-24 21:03:33 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
---------------
|
2012-02-02 22:15:56 +08:00
|
|
|
|
2013-07-12 11:50:36 +08:00
|
|
|
local visibleSize = CCDirector:getInstance():getVisibleSize()
|
|
|
|
local origin = CCDirector:getInstance():getVisibleOrigin()
|
2012-02-02 22:15:56 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- add the moving dog
|
|
|
|
local function creatDog()
|
|
|
|
local frameWidth = 105
|
|
|
|
local frameHeight = 95
|
2012-02-02 22:15:56 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- create dog animate
|
2013-07-12 14:44:24 +08:00
|
|
|
local textureDog = CCTextureCache:getInstance():addImage("dog.png")
|
2013-07-12 14:30:26 +08:00
|
|
|
local rect = Rect(0, 0, frameWidth, frameHeight)
|
2012-09-10 12:23:45 +08:00
|
|
|
local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect)
|
2013-07-12 14:30:26 +08:00
|
|
|
rect = Rect(frameWidth, 0, frameWidth, frameHeight)
|
2012-09-10 12:23:45 +08:00
|
|
|
local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect)
|
2012-04-26 09:34:42 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local spriteDog = CCSprite:createWithSpriteFrame(frame0)
|
|
|
|
spriteDog.isPaused = false
|
2012-10-12 16:18:28 +08:00
|
|
|
spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)
|
2012-02-02 22:15:56 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local animFrames = CCArray:create()
|
2012-02-02 22:15:56 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
animFrames:addObject(frame0)
|
|
|
|
animFrames:addObject(frame1)
|
2012-04-26 09:34:42 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5)
|
|
|
|
local animate = CCAnimate:create(animation);
|
|
|
|
spriteDog:runAction(CCRepeatForever:create(animate))
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- moving dog at every frame
|
|
|
|
local function tick()
|
|
|
|
if spriteDog.isPaused then return end
|
|
|
|
local x, y = spriteDog:getPosition()
|
2012-10-12 16:18:28 +08:00
|
|
|
if x > origin.x + visibleSize.width then
|
|
|
|
x = origin.x
|
2012-09-10 12:23:45 +08:00
|
|
|
else
|
|
|
|
x = x + 1
|
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
spriteDog:setPositionX(x)
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2012-09-10 12:23:45 +08:00
|
|
|
|
2013-07-12 11:50:36 +08:00
|
|
|
CCDirector:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
|
2012-09-10 12:23:45 +08:00
|
|
|
|
|
|
|
return spriteDog
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- create farm
|
|
|
|
local function createLayerFarm()
|
|
|
|
local layerFarm = CCLayer:create()
|
|
|
|
|
|
|
|
-- add in farm background
|
|
|
|
local bg = CCSprite:create("farm.jpg")
|
2012-10-12 16:18:28 +08:00
|
|
|
bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2)
|
2012-09-10 12:23:45 +08:00
|
|
|
layerFarm:addChild(bg)
|
|
|
|
|
|
|
|
-- add land sprite
|
|
|
|
for i = 0, 3 do
|
|
|
|
for j = 0, 1 do
|
|
|
|
local spriteLand = CCSprite:create("land.png")
|
|
|
|
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteLand)
|
|
|
|
end
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- add crop
|
2013-07-12 14:30:26 +08:00
|
|
|
local frameCrop = CCSpriteFrame:create("crop.png", Rect(0, 0, 105, 95))
|
2012-09-10 12:23:45 +08:00
|
|
|
for i = 0, 3 do
|
|
|
|
for j = 0, 1 do
|
|
|
|
local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop);
|
|
|
|
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteCrop)
|
|
|
|
end
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- add moving dog
|
|
|
|
local spriteDog = creatDog()
|
|
|
|
layerFarm:addChild(spriteDog)
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- handing touch events
|
|
|
|
local touchBeginPoint = nil
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local function onTouchBegan(x, y)
|
|
|
|
cclog("onTouchBegan: %0.2f, %0.2f", x, y)
|
2012-02-02 22:15:56 +08:00
|
|
|
touchBeginPoint = {x = x, y = y}
|
2012-09-10 12:23:45 +08:00
|
|
|
spriteDog.isPaused = true
|
|
|
|
-- CCTOUCHBEGAN event must return true
|
|
|
|
return true
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local function onTouchMoved(x, y)
|
2013-02-20 12:03:21 +08:00
|
|
|
cclog("onTouchMoved: %0.2f, %0.2f", x, y)
|
2012-09-10 12:23:45 +08:00
|
|
|
if touchBeginPoint then
|
|
|
|
local cx, cy = layerFarm:getPosition()
|
|
|
|
layerFarm:setPosition(cx + x - touchBeginPoint.x,
|
|
|
|
cy + y - touchBeginPoint.y)
|
|
|
|
touchBeginPoint = {x = x, y = y}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function onTouchEnded(x, y)
|
|
|
|
cclog("onTouchEnded: %0.2f, %0.2f", x, y)
|
|
|
|
touchBeginPoint = nil
|
|
|
|
spriteDog.isPaused = false
|
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local function onTouch(eventType, x, y)
|
2013-02-20 12:03:21 +08:00
|
|
|
if eventType == "began" then
|
2012-09-10 12:23:45 +08:00
|
|
|
return onTouchBegan(x, y)
|
2013-02-20 12:03:21 +08:00
|
|
|
elseif eventType == "moved" then
|
2012-09-10 12:23:45 +08:00
|
|
|
return onTouchMoved(x, y)
|
|
|
|
else
|
|
|
|
return onTouchEnded(x, y)
|
|
|
|
end
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2012-09-10 12:23:45 +08:00
|
|
|
|
|
|
|
layerFarm:registerScriptTouchHandler(onTouch)
|
|
|
|
layerFarm:setTouchEnabled(true)
|
|
|
|
|
|
|
|
return layerFarm
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- create menu
|
|
|
|
local function createLayerMenu()
|
|
|
|
local layerMenu = CCLayer:create()
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local menuPopup, menuTools, effectID
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local function menuCallbackClosePopup()
|
|
|
|
-- stop test sound effect
|
|
|
|
SimpleAudioEngine:sharedEngine():stopEffect(effectID)
|
|
|
|
menuPopup:setVisible(false)
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
local function menuCallbackOpenPopup()
|
|
|
|
-- loop test sound effect
|
2013-07-12 12:03:39 +08:00
|
|
|
local effectPath = CCFileUtils:getInstance():fullPathForFilename("effect1.wav")
|
2012-09-10 12:23:45 +08:00
|
|
|
effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)
|
|
|
|
menuPopup:setVisible(true)
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- add a popup menu
|
|
|
|
local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png")
|
|
|
|
menuPopupItem:setPosition(0, 0)
|
|
|
|
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
|
|
|
|
menuPopup = CCMenu:createWithItem(menuPopupItem)
|
2012-10-12 16:18:28 +08:00
|
|
|
menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
|
2012-06-15 17:26:11 +08:00
|
|
|
menuPopup:setVisible(false)
|
2012-09-10 12:23:45 +08:00
|
|
|
layerMenu:addChild(menuPopup)
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
|
|
|
local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png")
|
|
|
|
menuToolsItem:setPosition(0, 0)
|
|
|
|
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
|
|
|
|
menuTools = CCMenu:createWithItem(menuToolsItem)
|
2012-10-12 16:18:28 +08:00
|
|
|
local itemWidth = menuToolsItem:getContentSize().width
|
|
|
|
local itemHeight = menuToolsItem:getContentSize().height
|
|
|
|
menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)
|
2012-09-10 12:23:45 +08:00
|
|
|
layerMenu:addChild(menuTools)
|
|
|
|
|
|
|
|
return layerMenu
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- play background music, preload effect
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- uncomment below for the BlackBerry version
|
2013-07-12 12:03:39 +08:00
|
|
|
-- local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")
|
|
|
|
local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.mp3")
|
2012-09-10 12:23:45 +08:00
|
|
|
SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)
|
2013-07-12 12:03:39 +08:00
|
|
|
local effectPath = CCFileUtils:getInstance():fullPathForFilename("effect1.wav")
|
2012-09-10 12:23:45 +08:00
|
|
|
SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)
|
2012-08-24 14:57:44 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
-- run
|
|
|
|
local sceneGame = CCScene:create()
|
|
|
|
sceneGame:addChild(createLayerFarm())
|
|
|
|
sceneGame:addChild(createLayerMenu())
|
2013-07-12 11:50:36 +08:00
|
|
|
CCDirector:getInstance():runWithScene(sceneGame)
|
2012-09-10 12:23:45 +08:00
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-09-10 12:23:45 +08:00
|
|
|
xpcall(main, __G__TRACKBACK__)
|