2011-06-15 11:11:13 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- avoid memory leak
|
|
|
|
collectgarbage("setpause", 100)
|
|
|
|
collectgarbage("setstepmul", 5000)
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local cclog = function(...)
|
|
|
|
print(string.format(...))
|
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
require "hello2"
|
|
|
|
cclog("result is " .. myadd(3, 5))
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
---------------
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local winSize = CCDirector:sharedDirector():getWinSize()
|
2011-06-24 21:03:33 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- add the moving dog
|
|
|
|
local function creatDog()
|
|
|
|
local frameWidth = 105
|
|
|
|
local frameHeight = 95
|
|
|
|
|
|
|
|
-- create dog animate
|
|
|
|
local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")
|
|
|
|
local rect = CCRectMake(0, 0, frameWidth, frameHeight)
|
2012-08-10 18:14:04 +08:00
|
|
|
local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect)
|
2012-02-02 22:15:56 +08:00
|
|
|
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)
|
2012-08-10 18:14:04 +08:00
|
|
|
local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect)
|
2012-02-02 22:15:56 +08:00
|
|
|
|
2012-08-10 18:14:04 +08:00
|
|
|
local spriteDog = CCSprite:createWithSpriteFrame(frame0)
|
2012-02-02 22:15:56 +08:00
|
|
|
spriteDog.isPaused = false
|
|
|
|
spriteDog:setPosition(0, winSize.height / 4 * 3)
|
|
|
|
|
2012-06-15 11:46:51 +08:00
|
|
|
local animFrames = CCArray:create(2)
|
2012-04-26 09:34:42 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
animFrames:addObject(frame0)
|
|
|
|
animFrames:addObject(frame1)
|
|
|
|
|
2012-06-27 14:21:29 +08:00
|
|
|
local animation = CCAnimation:create(animFrames, 0.5)
|
2012-06-15 11:46:51 +08:00
|
|
|
local animate = CCAnimate:create(animation);
|
|
|
|
spriteDog:runAction(CCRepeatForever:create(animate))
|
2012-02-02 22:15:56 +08:00
|
|
|
|
|
|
|
-- moving dog at every frame
|
|
|
|
local function tick()
|
|
|
|
if spriteDog.isPaused then return end
|
|
|
|
local x, y = spriteDog:getPosition()
|
|
|
|
if x > winSize.width then
|
|
|
|
x = 0
|
|
|
|
else
|
|
|
|
x = x + 1
|
|
|
|
end
|
2012-04-26 09:34:42 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
spriteDog:setPositionX(x)
|
2011-06-24 21:03:33 +08:00
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-04-26 09:34:42 +08:00
|
|
|
CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
return spriteDog
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- create farm
|
|
|
|
local function createLayerFram()
|
2012-06-15 11:46:51 +08:00
|
|
|
local layerFarm = CCLayer:create()
|
2012-02-02 22:15:56 +08:00
|
|
|
|
|
|
|
-- add in farm background
|
2012-06-15 11:46:51 +08:00
|
|
|
local bg = CCSprite:create("farm.jpg")
|
2012-02-02 22:15:56 +08:00
|
|
|
bg:setPosition(winSize.width / 2 + 80, winSize.height / 2)
|
|
|
|
layerFarm:addChild(bg)
|
|
|
|
|
|
|
|
-- add land sprite
|
|
|
|
for i = 0, 3 do
|
|
|
|
for j = 0, 1 do
|
2012-06-15 11:46:51 +08:00
|
|
|
local spriteLand = CCSprite:create("land.png")
|
2012-02-02 22:15:56 +08:00
|
|
|
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteLand)
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- add crop
|
2012-06-27 14:21:29 +08:00
|
|
|
local frameCrop = CCSpriteFrame:create("crop.png", CCRectMake(0, 0, 105, 95))
|
2012-02-02 22:15:56 +08:00
|
|
|
for i = 0, 3 do
|
|
|
|
for j = 0, 1 do
|
2012-06-27 14:21:29 +08:00
|
|
|
local spriteCrop = CCSprite:create(frameCrop);
|
2012-02-02 22:15:56 +08:00
|
|
|
spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)
|
|
|
|
layerFarm:addChild(spriteCrop)
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- add moving dog
|
|
|
|
local spriteDog = creatDog()
|
|
|
|
layerFarm:addChild(spriteDog)
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- handing touch events
|
|
|
|
local touchBeginPoint = nil
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
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
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local function onTouchMoved(x, y)
|
|
|
|
cclog("onTouchMoved: %0.2f, %0.2f", x, y)
|
|
|
|
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
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local function onTouchEnded(x, y)
|
2012-04-26 09:34:42 +08:00
|
|
|
cclog("onTouchEnded: %0.2f, %0.2f", x, y)
|
2012-02-02 22:15:56 +08:00
|
|
|
touchBeginPoint = nil
|
|
|
|
spriteDog.isPaused = false
|
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local function onTouch(eventType, x, y)
|
|
|
|
if eventType == CCTOUCHBEGAN then
|
|
|
|
return onTouchBegan(x, y)
|
|
|
|
elseif eventType == CCTOUCHMOVED then
|
|
|
|
return onTouchMoved(x, y)
|
|
|
|
else
|
|
|
|
return onTouchEnded(x, y)
|
|
|
|
end
|
|
|
|
end
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
layerFarm:registerScriptTouchHandler(onTouch)
|
2012-06-15 17:26:11 +08:00
|
|
|
layerFarm:setTouchEnabled(true)
|
2011-06-24 17:00:14 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
return layerFarm
|
2011-06-24 17:00:14 +08:00
|
|
|
end
|
|
|
|
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- create menu
|
|
|
|
local function createLayerMenu()
|
2012-06-15 11:46:51 +08:00
|
|
|
local layerMenu = CCLayer:create()
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local menuPopup, menuTools, effectID
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local function menuCallbackClosePopup()
|
|
|
|
-- stop test sound effect
|
|
|
|
SimpleAudioEngine:sharedEngine():stopEffect(effectID)
|
2012-06-15 17:26:11 +08:00
|
|
|
menuPopup:setVisible(false)
|
2012-02-02 22:15:56 +08:00
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
local function menuCallbackOpenPopup()
|
|
|
|
-- loop test sound effect
|
|
|
|
effectID = SimpleAudioEngine:sharedEngine():playEffect("effect1.wav")
|
2012-06-15 17:26:11 +08:00
|
|
|
menuPopup:setVisible(true)
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- add a popup menu
|
2012-06-15 11:46:51 +08:00
|
|
|
local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png")
|
2012-02-02 22:15:56 +08:00
|
|
|
menuPopupItem:setPosition(0, 0)
|
|
|
|
menuPopupItem:registerScriptHandler(menuCallbackClosePopup)
|
2012-06-15 11:46:51 +08:00
|
|
|
menuPopup = CCMenu:createWithItem(menuPopupItem)
|
2012-02-02 22:15:56 +08:00
|
|
|
menuPopup:setPosition(winSize.width / 2, winSize.height / 2)
|
2012-06-15 17:26:11 +08:00
|
|
|
menuPopup:setVisible(false)
|
2012-02-02 22:15:56 +08:00
|
|
|
layerMenu:addChild(menuPopup)
|
|
|
|
|
|
|
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
2012-06-15 11:46:51 +08:00
|
|
|
local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png")
|
2012-02-02 22:15:56 +08:00
|
|
|
menuToolsItem:setPosition(0, 0)
|
|
|
|
menuToolsItem:registerScriptHandler(menuCallbackOpenPopup)
|
2012-06-15 11:46:51 +08:00
|
|
|
menuTools = CCMenu:createWithItem(menuToolsItem)
|
2012-02-02 22:15:56 +08:00
|
|
|
menuTools:setPosition(30, 40)
|
|
|
|
layerMenu:addChild(menuTools)
|
|
|
|
|
|
|
|
return layerMenu
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- play background music, preload effect
|
|
|
|
SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
|
|
|
|
SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav");
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2012-02-02 22:15:56 +08:00
|
|
|
-- run
|
2012-06-15 11:46:51 +08:00
|
|
|
local sceneGame = CCScene:create()
|
2012-02-02 22:15:56 +08:00
|
|
|
sceneGame:addChild(createLayerFram())
|
|
|
|
sceneGame:addChild(createLayerMenu())
|
|
|
|
CCDirector:sharedDirector():runWithScene(sceneGame)
|