2011-06-24 17:00:14 +08:00
|
|
|
-- create scene & layer
|
|
|
|
layerFarm = cocos2d.CCLayer:node()
|
|
|
|
layerFarm:setIsTouchEnabled(true)
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
layerMenu = cocos2d.CCLayer:node()
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
sceneGame = cocos2d.CCScene:node()
|
|
|
|
sceneGame:addChild(layerFarm)
|
|
|
|
sceneGame:addChild(layerMenu)
|
|
|
|
|
|
|
|
winSize = cocos2d.CCDirector:sharedDirector():getWinSize()
|
|
|
|
|
|
|
|
-- add in farm background
|
|
|
|
spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg")
|
|
|
|
spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2))
|
|
|
|
layerFarm:addChild(spriteFarm)
|
|
|
|
|
|
|
|
-- touch handers
|
2011-06-24 21:03:33 +08:00
|
|
|
pointBegin = nil
|
|
|
|
|
2011-06-21 11:47:57 +08:00
|
|
|
function btnTouchMove(e)
|
2011-06-24 21:03:33 +08:00
|
|
|
cocos2d.CCLuaLog("btnTouchMove")
|
|
|
|
if pointBegin ~= nil then
|
|
|
|
local v = e[1]
|
|
|
|
local pointMove = v:locationInView(v:view())
|
|
|
|
pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove)
|
|
|
|
local positionCurrent = layerFarm.__CCNode__:getPosition()
|
|
|
|
layerFarm.__CCNode__:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y))
|
|
|
|
pointBegin = pointMove
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2011-06-21 11:47:57 +08:00
|
|
|
function btnTouchBegin(e)
|
2011-07-04 19:04:21 +08:00
|
|
|
cocos2d.CCLuaLog("btnTouchBegin")
|
2011-06-24 21:03:33 +08:00
|
|
|
for k,v in ipairs(e) do
|
|
|
|
pointBegin = v:locationInView(v:view())
|
|
|
|
pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin)
|
|
|
|
end
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2011-06-21 11:47:57 +08:00
|
|
|
function btnTouchEnd(e)
|
|
|
|
cocos2d.CCLuaLog("btnTouchEnd")
|
2011-06-24 21:03:33 +08:00
|
|
|
touchStart = nil
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
-- regiester touch handlers
|
|
|
|
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin")
|
|
|
|
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove")
|
|
|
|
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd")
|
2011-06-21 11:47:57 +08:00
|
|
|
|
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
-- add land sprite
|
2011-06-21 11:47:57 +08:00
|
|
|
for i=0,3,1 do
|
|
|
|
for j=0,1,1 do
|
2011-06-24 17:00:14 +08:00
|
|
|
spriteLand = cocos2d.CCSprite:spriteWithFile("land.png")
|
|
|
|
layerFarm:addChild(spriteLand)
|
|
|
|
spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2))
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
-- add crop
|
2011-06-21 11:47:57 +08:00
|
|
|
|
|
|
|
for i=0,3,1 do
|
|
|
|
for j=0,1,1 do
|
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png")
|
|
|
|
frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95))
|
|
|
|
spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop);
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
layerFarm:addChild(spriteCrop)
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2))
|
2011-06-21 11:47:57 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
-- add the moving dog
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
FrameWidth = 105
|
|
|
|
FrameHeight = 95
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png")
|
|
|
|
frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight))
|
|
|
|
frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight))
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
|
|
|
|
spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3))
|
2011-06-24 21:03:33 +08:00
|
|
|
layerFarm:addChild(spriteDog)
|
2011-06-21 11:47:57 +08:00
|
|
|
|
|
|
|
animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2)
|
|
|
|
animFrames:addObject(frame0)
|
|
|
|
animFrames:addObject(frame1)
|
|
|
|
|
|
|
|
animation = cocos2d.CCAnimation:animationWithName("wait", 0.5, animFrames)
|
|
|
|
|
|
|
|
animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
|
2011-06-24 17:00:14 +08:00
|
|
|
spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))
|
|
|
|
|
|
|
|
|
|
|
|
-- add a popup menu
|
|
|
|
|
|
|
|
function menuCallbackClosePopup()
|
|
|
|
menuPopup:setIsVisible(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
menuPopupItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png")
|
|
|
|
menuPopupItem:setPosition( cocos2d.CCPoint(0, 0) )
|
|
|
|
menuPopupItem:registerScriptHandler("menuCallbackClosePopup")
|
|
|
|
menuPopup = cocos2d.CCMenu:menuWithItem(menuPopupItem)
|
|
|
|
menuPopup:setPosition( cocos2d.CCPoint(winSize.width/2, winSize.height/2) )
|
|
|
|
menuPopup:setIsVisible(false)
|
|
|
|
layerMenu:addChild(menuPopup)
|
|
|
|
|
|
|
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
|
|
|
|
|
|
|
function menuCallbackOpenPopup()
|
|
|
|
menuPopup:setIsVisible(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png")
|
|
|
|
menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) )
|
|
|
|
menuToolsItem:registerScriptHandler("menuCallbackOpenPopup")
|
|
|
|
menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem)
|
|
|
|
menuTools:setPosition( cocos2d.CCPoint(30, 40) )
|
|
|
|
layerMenu:addChild(menuTools)
|
2011-06-21 11:47:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
function tick()
|
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
point = spriteDog:getPosition();
|
2011-06-21 11:47:57 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
if point.x > winSize.width then
|
2011-06-21 11:47:57 +08:00
|
|
|
point.x = 0
|
2011-06-24 17:00:14 +08:00
|
|
|
spriteDog:setPosition(point)
|
2011-06-21 11:47:57 +08:00
|
|
|
else
|
|
|
|
point.x = point.x + 1
|
2011-06-24 17:00:14 +08:00
|
|
|
spriteDog:setPosition(point)
|
2011-06-21 11:47:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)
|
2011-06-15 11:11:13 +08:00
|
|
|
|
2011-06-24 17:00:14 +08:00
|
|
|
-- run
|
|
|
|
|
|
|
|
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)
|