2013-08-30 17:18:47 +08:00
|
|
|
require "Cocos2d"
|
|
|
|
-- cclog
|
|
|
|
cclog = function(...)
|
|
|
|
print(string.format(...))
|
|
|
|
end
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
-- for CCLuaEngine traceback
|
|
|
|
function __G__TRACKBACK__(msg)
|
2013-08-30 17:18:47 +08:00
|
|
|
cclog("----------------------------------------")
|
|
|
|
cclog("LUA ERROR: " .. tostring(msg) .. "\n")
|
|
|
|
cclog(debug.traceback())
|
|
|
|
cclog("----------------------------------------")
|
2013-02-19 15:38:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local function main()
|
|
|
|
-- avoid memory leak
|
|
|
|
collectgarbage("setpause", 100)
|
|
|
|
collectgarbage("setstepmul", 5000)
|
|
|
|
|
|
|
|
require "hello2"
|
2013-08-30 17:18:47 +08:00
|
|
|
cclog("result is " .. myadd(1, 1))
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
---------------
|
|
|
|
|
2013-08-30 17:18:47 +08:00
|
|
|
local visibleSize = cc.Director:getInstance():getVisibleSize()
|
|
|
|
local origin = cc.Director:getInstance():getVisibleOrigin()
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
-- add the moving dog
|
|
|
|
local function creatDog()
|
|
|
|
local frameWidth = 105
|
|
|
|
local frameHeight = 95
|
|
|
|
|
|
|
|
-- create dog animate
|
2013-08-30 17:18:47 +08:00
|
|
|
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)
|
2013-02-19 15:38:30 +08:00
|
|
|
|
2013-08-30 17:18:47 +08:00
|
|
|
local spriteDog = cc.Sprite:createWithSpriteFrame(frame0)
|
2013-02-19 15:38:30 +08:00
|
|
|
spriteDog.isPaused = false
|
|
|
|
spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)
|
2013-08-30 17:18:47 +08:00
|
|
|
--[[
|
2013-02-19 15:38:30 +08:00
|
|
|
local animFrames = CCArray:create()
|
|
|
|
|
|
|
|
animFrames:addObject(frame0)
|
|
|
|
animFrames:addObject(frame1)
|
2013-08-30 17:18:47 +08:00
|
|
|
]]--
|
2013-02-19 15:38:30 +08:00
|
|
|
|
2013-08-30 17:18:47 +08:00
|
|
|
local animation = cc.Animation:createWithSpriteFrames({frame0,frame1}, 0.5)
|
|
|
|
local animate = cc.Animate:create(animation);
|
|
|
|
spriteDog:runAction(cc.RepeatForever:create(animate))
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
-- 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
|
|
|
|
|
2013-08-30 17:18:47 +08:00
|
|
|
cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
return spriteDog
|
|
|
|
end
|
|
|
|
|
|
|
|
-- create farm
|
|
|
|
local function createLayerFarm()
|
2013-08-30 17:18:47 +08:00
|
|
|
local layerFarm = cc.Layer:create()
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
-- add in farm background
|
2013-08-30 17:18:47 +08:00
|
|
|
local bg = cc.Sprite:create("farm.jpg")
|
2013-02-19 15:38:30 +08:00
|
|
|
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
|
2013-08-30 17:18:47 +08:00
|
|
|
local spriteLand = cc.Sprite:create("land.png")
|
2013-02-19 15:38:30 +08:00
|
|
|
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteLand)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add crop
|
2013-08-30 17:18:47 +08:00
|
|
|
local frameCrop = cc.SpriteFrame:create("crop.png", cc.rect(0, 0, 105, 95))
|
2013-02-19 15:38:30 +08:00
|
|
|
for i = 0, 3 do
|
|
|
|
for j = 0, 1 do
|
2013-08-30 17:18:47 +08:00
|
|
|
local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop);
|
2013-02-19 15:38:30 +08:00
|
|
|
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteCrop)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add moving dog
|
|
|
|
local spriteDog = creatDog()
|
|
|
|
layerFarm:addChild(spriteDog)
|
|
|
|
|
|
|
|
-- handing touch events
|
|
|
|
local touchBeginPoint = nil
|
|
|
|
|
|
|
|
local function onTouchBegan(x, y)
|
|
|
|
cclog("onTouchBegan: %0.2f, %0.2f", x, y)
|
|
|
|
touchBeginPoint = {x = x, y = y}
|
|
|
|
spriteDog.isPaused = true
|
|
|
|
-- CCTOUCHBEGAN event must return true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local function onTouchMoved(x, y)
|
2013-02-20 12:03:21 +08:00
|
|
|
cclog("onTouchMoved: %0.2f, %0.2f", x, y)
|
2013-02-19 15:38:30 +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
|
|
|
|
|
|
|
|
local function onTouch(eventType, x, y)
|
2013-02-20 12:03:21 +08:00
|
|
|
if eventType == "began" then
|
2013-02-19 15:38:30 +08:00
|
|
|
return onTouchBegan(x, y)
|
2013-02-20 12:03:21 +08:00
|
|
|
elseif eventType == "moved" then
|
2013-02-19 15:38:30 +08:00
|
|
|
return onTouchMoved(x, y)
|
|
|
|
else
|
|
|
|
return onTouchEnded(x, y)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
layerFarm:registerScriptTouchHandler(onTouch)
|
|
|
|
layerFarm:setTouchEnabled(true)
|
|
|
|
|
|
|
|
return layerFarm
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- create menu
|
|
|
|
local function createLayerMenu()
|
2013-08-30 17:18:47 +08:00
|
|
|
local layerMenu = cc.Layer:create()
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
local menuPopup, menuTools, effectID
|
|
|
|
|
|
|
|
local function menuCallbackClosePopup()
|
|
|
|
-- stop test sound effect
|
2013-08-30 17:18:47 +08:00
|
|
|
cc.SimpleAudioEngine:getInstance():stopEffect(effectID)
|
2013-02-19 15:38:30 +08:00
|
|
|
menuPopup:setVisible(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function menuCallbackOpenPopup()
|
|
|
|
-- loop test sound effect
|
2013-08-30 17:18:47 +08:00
|
|
|
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
|
|
|
|
effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
|
2013-02-19 15:38:30 +08:00
|
|
|
menuPopup:setVisible(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- add a popup menu
|
2013-08-30 17:18:47 +08:00
|
|
|
local menuPopupItem = cc.MenuItemImage:create("menu2.png", "menu2.png")
|
2013-02-19 15:38:30 +08:00
|
|
|
menuPopupItem:setPosition(0, 0)
|
|
|
|
menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup)
|
2013-08-30 17:18:47 +08:00
|
|
|
menuPopup = cc.Menu:create(menuPopupItem)
|
2013-02-19 15:38:30 +08:00
|
|
|
menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)
|
|
|
|
menuPopup:setVisible(false)
|
|
|
|
layerMenu:addChild(menuPopup)
|
2013-08-30 17:18:47 +08:00
|
|
|
|
2013-02-19 15:38:30 +08:00
|
|
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
2013-08-30 17:18:47 +08:00
|
|
|
local menuToolsItem = cc.MenuItemImage:create("menu1.png", "menu1.png")
|
2013-02-19 15:38:30 +08:00
|
|
|
menuToolsItem:setPosition(0, 0)
|
|
|
|
menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup)
|
2013-08-30 17:18:47 +08:00
|
|
|
menuTools = cc.Menu:create(menuToolsItem)
|
2013-02-19 15:38:30 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
-- uncomment below for the BlackBerry version
|
2013-07-12 12:03:39 +08:00
|
|
|
-- local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")
|
2013-08-30 17:18:47 +08:00
|
|
|
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)
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
-- run
|
2013-08-30 17:18:47 +08:00
|
|
|
local sceneGame = cc.Scene:create()
|
2013-02-19 15:38:30 +08:00
|
|
|
sceneGame:addChild(createLayerFarm())
|
|
|
|
sceneGame:addChild(createLayerMenu())
|
2013-08-30 17:18:47 +08:00
|
|
|
cc.Director:getInstance():runWithScene(sceneGame)
|
2013-02-19 15:38:30 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
xpcall(main, __G__TRACKBACK__)
|