有事要出门办,所以先发代码,回来详细说明。
local layerFarm -- 农场层
local menuPopup -- 弹出式菜单
local spriteDog -- 小狗的 sprite
local pointBegin -- 触摸农场层时的起始位置
local winSize = cocos2d.CCDirector:sharedDirector():getWinSize()
-- 农场层的触摸事件处理函数
function btnTouchMove(e)
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()
local x = positionCurrent.x + pointMove.x - pointBegin.x
local y = positionCurrent.y + pointMove.y - pointBegin.y
layerFarm.__CCNode__:setPosition(cocos2d.CCPoint(x, y))
pointBegin = pointMove
end
end
function btnTouchBegin(e)
for k,v in ipairs(e) do
pointBegin = v:locationInView(v:view())
pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin)
cocos2d.CCLuaLog("btnTouchBegin")
end
end
function btnTouchEnd(e)
cocos2d.CCLuaLog("btnTouchEnd")
pointBegin = nil
end
-- 关闭弹出菜单
function menuCallbackClosePopup()
menuPopup:setIsVisible(false)
end
-- 显示弹出菜单
function menuCallbackOpenPopup()
menuPopup:setIsVisible(true)
end
-- 移动小狗位置
function tick()
local point = spriteDog:getPosition();
if point.x > winSize.width then
point.x = 0
spriteDog:setPosition(point)
else
point.x = point.x + 1
spriteDog:setPosition(point)
end
end
-- 创建农场层
local function createLayerFarm()
local layer = cocos2d.CCLayer:node()
layer:setIsTouchEnabled(true)
-- 设置农场背景
local spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg")
spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2))
layer:addChild(spriteFarm)
for i=0,3,1 do
for j=0,1,1 do
local spriteLand = cocos2d.CCSprite:spriteWithFile("land.png")
layer:addChild(spriteLand)
spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2))
end
end
-- add crop
for i=0,3,1 do
for j=0,1,1 do
local textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png")
local rect = cocos2d.CCRectMake(0, 0, 105, 95)
local frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, rect)
local spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop);
layer:addChild(spriteCrop)
spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2))
end
end
-- 注册农场层的触摸事件
layer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin")
layer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove")
layer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd")
-- 添加正在移动的 dog
local FrameWidth = 105
local FrameHeight = 95
local textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png")
local rect = cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight)
local frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, rect)
rect = cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight)
local frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, rect)
spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3))
layer:addChild(spriteDog)
local animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2)
animFrames:addObject(frame0)
animFrames:addObject(frame1)
local animation = cocos2d.CCAnimation:animationWithName("wait", 0.5, animFrames)
local animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))
-- 让小狗移动
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)
return layer
end
-- 创建菜单层
local function createLayerMenu()
local layer = cocos2d.CCLayer:node()
local 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)
layer:addChild(menuPopup)
-- add the left-bottom "tools" menu to invoke menuPopup
local menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png", "menu1.png")
menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) )
menuToolsItem:registerScriptHandler("menuCallbackOpenPopup")
local menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem)
menuTools:setPosition( cocos2d.CCPoint(30, 40) )
layer:addChild(menuTools)
return layer
end
-- 创建场景,并将农场层和菜单层添加到场景中
local function createScene()
layerFarm = createLayerFarm()
scene = cocos2d.CCScene:node()
scene:addChild(layerFarm)
scene:addChild(createLayerMenu())
return scene
end
-- 开始执行
local sceneGame = createScene()
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)
基本思路就是尽量避免全局变量(全局变量如果不仔细清理会导致内存泄漏)和全局函数。