mirror of https://github.com/axmolengine/axmol.git
* update lua application template files
This commit is contained in:
parent
4dbc5997f0
commit
7ef0373c8b
|
@ -1,166 +1,180 @@
|
||||||
require "hello2"
|
|
||||||
|
|
||||||
cocos2d.CCLuaLog("result is " .. myadd(3, 5))
|
|
||||||
|
|
||||||
-- create scene & layer
|
|
||||||
layerFarm = cocos2d.CCLayer:node()
|
|
||||||
layerFarm:setIsTouchEnabled(true)
|
|
||||||
|
|
||||||
layerMenu = cocos2d.CCLayer:node()
|
|
||||||
|
|
||||||
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
|
|
||||||
pointBegin = nil
|
|
||||||
|
|
||||||
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:getPosition()
|
|
||||||
layerFarm:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y))
|
|
||||||
pointBegin = pointMove
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function btnTouchBegin(e)
|
|
||||||
cocos2d.CCScheduler:sharedScheduler():unscheduleScriptFunc("tick")
|
|
||||||
cocos2d.CCLuaLog("btnTouchBegin")
|
|
||||||
for k,v in ipairs(e) do
|
|
||||||
pointBegin = v:locationInView(v:view())
|
|
||||||
pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function btnTouchEnd(e)
|
|
||||||
cocos2d.CCLuaLog("btnTouchEnd")
|
|
||||||
touchStart = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
-- regiester touch handlers
|
|
||||||
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin")
|
|
||||||
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove")
|
|
||||||
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd")
|
|
||||||
|
|
||||||
|
|
||||||
-- add land sprite
|
|
||||||
for i=0,3,1 do
|
|
||||||
for j=0,1,1 do
|
|
||||||
spriteLand = cocos2d.CCSprite:spriteWithFile("land.png")
|
|
||||||
layerFarm: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
|
|
||||||
|
|
||||||
textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png")
|
|
||||||
frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95))
|
|
||||||
spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop);
|
|
||||||
|
|
||||||
layerFarm:addChild(spriteCrop)
|
|
||||||
|
|
||||||
spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2))
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- add the moving dog
|
|
||||||
|
|
||||||
FrameWidth = 105
|
|
||||||
FrameHeight = 95
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
|
|
||||||
spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3))
|
|
||||||
layerFarm:addChild(spriteDog)
|
|
||||||
|
|
||||||
animation = cocos2d.CCAnimation:animation()
|
|
||||||
|
|
||||||
animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2)
|
|
||||||
animFrames:addObject(frame0)
|
|
||||||
animFrames:addObject(frame1)
|
|
||||||
|
|
||||||
animation = cocos2d.CCAnimation:animationWithFrames(animFrames,0.5)
|
|
||||||
|
|
||||||
animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
|
|
||||||
spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))
|
|
||||||
|
|
||||||
|
|
||||||
-- add a popup menu
|
|
||||||
|
|
||||||
function menuCallbackClosePopup()
|
|
||||||
-- stop test sound effect
|
|
||||||
CocosDenshion.SimpleAudioEngine:sharedEngine():stopEffect(effectID)
|
|
||||||
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()
|
|
||||||
-- loop test sound effect
|
|
||||||
-- NOTE: effectID is global, so it can be used to stop
|
|
||||||
effectID = CocosDenshion.SimpleAudioEngine:sharedEngine():playEffect("effect1.wav")
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
-- avoid memory leak
|
-- avoid memory leak
|
||||||
collectgarbage("setpause", 100)
|
collectgarbage("setpause", 100)
|
||||||
collectgarbage("setstepmul", 5000)
|
collectgarbage("setstepmul", 5000)
|
||||||
|
|
||||||
|
local cclog = function(...)
|
||||||
|
print(string.format(...))
|
||||||
|
end
|
||||||
|
|
||||||
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)
|
require "hello2"
|
||||||
|
cclog("result is " .. myadd(3, 5))
|
||||||
|
|
||||||
|
---------------
|
||||||
|
|
||||||
|
local winSize = CCDirector:sharedDirector():getWinSize()
|
||||||
|
|
||||||
|
-- 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)
|
||||||
|
local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect)
|
||||||
|
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)
|
||||||
|
local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect)
|
||||||
|
|
||||||
|
local spriteDog = CCSprite:spriteWithSpriteFrame(frame0)
|
||||||
|
spriteDog.isPaused = false
|
||||||
|
spriteDog:setPosition(0, winSize.height / 4 * 3)
|
||||||
|
|
||||||
|
local animFrames = CCMutableArray_CCSpriteFrame__:new(2)
|
||||||
|
animFrames:addObject(frame0)
|
||||||
|
animFrames:addObject(frame1)
|
||||||
|
|
||||||
|
local animation = CCAnimation:animationWithFrames(animFrames, 0.5)
|
||||||
|
local animate = CCAnimate:actionWithAnimation(animation, false);
|
||||||
|
spriteDog:runAction(CCRepeatForever:actionWithAction(animate))
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
spriteDog:setPositionX(x)
|
||||||
|
end
|
||||||
|
|
||||||
|
CCScheduler:sharedScheduler():scheduleScriptFunc(tick, 0, false)
|
||||||
|
|
||||||
|
return spriteDog
|
||||||
|
end
|
||||||
|
|
||||||
|
-- create farm
|
||||||
|
local function createLayerFram()
|
||||||
|
local layerFarm = CCLayer:node()
|
||||||
|
|
||||||
|
-- add in farm background
|
||||||
|
local bg = CCSprite:spriteWithFile("farm.jpg")
|
||||||
|
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
|
||||||
|
local spriteLand = CCSprite:spriteWithFile("land.png")
|
||||||
|
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
|
||||||
|
layerFarm:addChild(spriteLand)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- add crop
|
||||||
|
local textureCrop = CCTextureCache:sharedTextureCache():addImage("crop.png")
|
||||||
|
local frameCrop = CCSpriteFrame:frameWithTexture(textureCrop, CCRectMake(0, 0, 105, 95))
|
||||||
|
for i = 0, 3 do
|
||||||
|
for j = 0, 1 do
|
||||||
|
local spriteCrop = CCSprite:spriteWithSpriteFrame(frameCrop);
|
||||||
|
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)
|
||||||
|
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
|
||||||
|
|
||||||
|
local function onTouchEnded(x, y)
|
||||||
|
cclog("onTouchEnded")
|
||||||
|
touchBeginPoint = nil
|
||||||
|
spriteDog.isPaused = false
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
layerFarm:registerScriptTouchHandler(onTouch)
|
||||||
|
|
||||||
|
return layerFarm
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- create menu
|
||||||
|
local function createLayerMenu()
|
||||||
|
local layerMenu = CCLayer:node()
|
||||||
|
|
||||||
|
local menuPopup, menuTools, effectID
|
||||||
|
|
||||||
|
local function menuCallbackClosePopup()
|
||||||
|
-- stop test sound effect
|
||||||
|
SimpleAudioEngine:sharedEngine():stopEffect(effectID)
|
||||||
|
menuPopup:setIsVisible(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function menuCallbackOpenPopup()
|
||||||
|
-- loop test sound effect
|
||||||
|
effectID = SimpleAudioEngine:sharedEngine():playEffect("effect1.wav")
|
||||||
|
menuPopup:setIsVisible(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- add a popup menu
|
||||||
|
local menuPopupItem = CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png")
|
||||||
|
menuPopupItem:setPosition(0, 0)
|
||||||
|
menuPopupItem:registerScriptHandler(menuCallbackClosePopup)
|
||||||
|
menuPopup = CCMenu:menuWithItem(menuPopupItem)
|
||||||
|
menuPopup:setPosition(winSize.width / 2, winSize.height / 2)
|
||||||
|
menuPopup:setIsVisible(false)
|
||||||
|
layerMenu:addChild(menuPopup)
|
||||||
|
|
||||||
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
||||||
|
local menuToolsItem = CCMenuItemImage:itemFromNormalImage("menu1.png", "menu1.png")
|
||||||
|
menuToolsItem:setPosition(0, 0)
|
||||||
|
menuToolsItem:registerScriptHandler(menuCallbackOpenPopup)
|
||||||
|
menuTools = CCMenu:menuWithItem(menuToolsItem)
|
||||||
|
menuTools:setPosition(30, 40)
|
||||||
|
layerMenu:addChild(menuTools)
|
||||||
|
|
||||||
|
return layerMenu
|
||||||
|
end
|
||||||
|
|
||||||
|
-- play background music, preload effect
|
||||||
|
SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
|
||||||
|
SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav");
|
||||||
|
|
||||||
-- play background music
|
|
||||||
CocosDenshion.SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
|
|
||||||
-- preload effect
|
|
||||||
CocosDenshion.SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav");
|
|
||||||
-- run
|
-- run
|
||||||
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)
|
local sceneGame = CCScene:node()
|
||||||
|
sceneGame:addChild(createLayerFram())
|
||||||
|
sceneGame:addChild(createLayerMenu())
|
||||||
|
CCDirector:sharedDirector():runWithScene(sceneGame)
|
||||||
|
|
|
@ -1,164 +1,180 @@
|
||||||
require "hello2"
|
|
||||||
|
|
||||||
cocos2d.CCLuaLog("result is " .. myadd(3, 5))
|
|
||||||
|
|
||||||
-- create scene & layer
|
|
||||||
layerFarm = cocos2d.CCLayer:node()
|
|
||||||
layerFarm:setIsTouchEnabled(true)
|
|
||||||
|
|
||||||
layerMenu = cocos2d.CCLayer:node()
|
|
||||||
|
|
||||||
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
|
|
||||||
pointBegin = nil
|
|
||||||
|
|
||||||
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:getPosition()
|
|
||||||
layerFarm:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y))
|
|
||||||
pointBegin = pointMove
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function btnTouchBegin(e)
|
|
||||||
cocos2d.CCScheduler:sharedScheduler():unscheduleScriptFunc("tick")
|
|
||||||
cocos2d.CCLuaLog("btnTouchBegin")
|
|
||||||
for k,v in ipairs(e) do
|
|
||||||
pointBegin = v:locationInView(v:view())
|
|
||||||
pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function btnTouchEnd(e)
|
|
||||||
cocos2d.CCLuaLog("btnTouchEnd")
|
|
||||||
touchStart = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
-- regiester touch handlers
|
|
||||||
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin")
|
|
||||||
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove")
|
|
||||||
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd")
|
|
||||||
|
|
||||||
|
|
||||||
-- add land sprite
|
|
||||||
for i=0,3,1 do
|
|
||||||
for j=0,1,1 do
|
|
||||||
spriteLand = cocos2d.CCSprite:spriteWithFile("land.png")
|
|
||||||
layerFarm: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
|
|
||||||
|
|
||||||
textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png")
|
|
||||||
frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95))
|
|
||||||
spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop);
|
|
||||||
|
|
||||||
layerFarm:addChild(spriteCrop)
|
|
||||||
|
|
||||||
spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2))
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- add the moving dog
|
|
||||||
|
|
||||||
FrameWidth = 105
|
|
||||||
FrameHeight = 95
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
|
|
||||||
spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3))
|
|
||||||
layerFarm:addChild(spriteDog)
|
|
||||||
|
|
||||||
animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2)
|
|
||||||
animFrames:addObject(frame0)
|
|
||||||
animFrames:addObject(frame1)
|
|
||||||
|
|
||||||
animation = cocos2d.CCAnimation:animationWithFrames(animFrames, 0.5)
|
|
||||||
|
|
||||||
animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
|
|
||||||
spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))
|
|
||||||
|
|
||||||
|
|
||||||
-- add a popup menu
|
|
||||||
|
|
||||||
function menuCallbackClosePopup()
|
|
||||||
-- stop test sound effect
|
|
||||||
CocosDenshion.SimpleAudioEngine:sharedEngine():stopEffect(effectID)
|
|
||||||
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()
|
|
||||||
-- loop test sound effect
|
|
||||||
-- NOTE: effectID is global, so it can be used to stop
|
|
||||||
effectID = CocosDenshion.SimpleAudioEngine:sharedEngine():playEffect("effect1.wav")
|
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
-- avoid memory leak
|
-- avoid memory leak
|
||||||
collectgarbage("setpause", 100)
|
collectgarbage("setpause", 100)
|
||||||
collectgarbage("setstepmul", 5000)
|
collectgarbage("setstepmul", 5000)
|
||||||
|
|
||||||
|
local cclog = function(...)
|
||||||
|
print(string.format(...))
|
||||||
|
end
|
||||||
|
|
||||||
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)
|
require "hello2"
|
||||||
|
cclog("result is " .. myadd(3, 5))
|
||||||
|
|
||||||
|
---------------
|
||||||
|
|
||||||
|
local winSize = CCDirector:sharedDirector():getWinSize()
|
||||||
|
|
||||||
|
-- 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)
|
||||||
|
local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect)
|
||||||
|
rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)
|
||||||
|
local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect)
|
||||||
|
|
||||||
|
local spriteDog = CCSprite:spriteWithSpriteFrame(frame0)
|
||||||
|
spriteDog.isPaused = false
|
||||||
|
spriteDog:setPosition(0, winSize.height / 4 * 3)
|
||||||
|
|
||||||
|
local animFrames = CCMutableArray_CCSpriteFrame__:new(2)
|
||||||
|
animFrames:addObject(frame0)
|
||||||
|
animFrames:addObject(frame1)
|
||||||
|
|
||||||
|
local animation = CCAnimation:animationWithFrames(animFrames, 0.5)
|
||||||
|
local animate = CCAnimate:actionWithAnimation(animation, false);
|
||||||
|
spriteDog:runAction(CCRepeatForever:actionWithAction(animate))
|
||||||
|
|
||||||
|
-- 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
|
||||||
|
spriteDog:setPositionX(x)
|
||||||
|
end
|
||||||
|
|
||||||
|
CCScheduler:sharedScheduler():scheduleScriptFunc(tick, 0, false)
|
||||||
|
|
||||||
|
return spriteDog
|
||||||
|
end
|
||||||
|
|
||||||
|
-- create farm
|
||||||
|
local function createLayerFram()
|
||||||
|
local layerFarm = CCLayer:node()
|
||||||
|
|
||||||
|
-- add in farm background
|
||||||
|
local bg = CCSprite:spriteWithFile("farm.jpg")
|
||||||
|
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
|
||||||
|
local spriteLand = CCSprite:spriteWithFile("land.png")
|
||||||
|
spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)
|
||||||
|
layerFarm:addChild(spriteLand)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- add crop
|
||||||
|
local textureCrop = CCTextureCache:sharedTextureCache():addImage("crop.png")
|
||||||
|
local frameCrop = CCSpriteFrame:frameWithTexture(textureCrop, CCRectMake(0, 0, 105, 95))
|
||||||
|
for i = 0, 3 do
|
||||||
|
for j = 0, 1 do
|
||||||
|
local spriteCrop = CCSprite:spriteWithSpriteFrame(frameCrop);
|
||||||
|
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)
|
||||||
|
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
|
||||||
|
|
||||||
|
local function onTouchEnded(x, y)
|
||||||
|
cclog("onTouchEnded")
|
||||||
|
touchBeginPoint = nil
|
||||||
|
spriteDog.isPaused = false
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
layerFarm:registerScriptTouchHandler(onTouch)
|
||||||
|
|
||||||
|
return layerFarm
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- create menu
|
||||||
|
local function createLayerMenu()
|
||||||
|
local layerMenu = CCLayer:node()
|
||||||
|
|
||||||
|
local menuPopup, menuTools, effectID
|
||||||
|
|
||||||
|
local function menuCallbackClosePopup()
|
||||||
|
-- stop test sound effect
|
||||||
|
SimpleAudioEngine:sharedEngine():stopEffect(effectID)
|
||||||
|
menuPopup:setIsVisible(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function menuCallbackOpenPopup()
|
||||||
|
-- loop test sound effect
|
||||||
|
effectID = SimpleAudioEngine:sharedEngine():playEffect("effect1.wav")
|
||||||
|
menuPopup:setIsVisible(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- add a popup menu
|
||||||
|
local menuPopupItem = CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png")
|
||||||
|
menuPopupItem:setPosition(0, 0)
|
||||||
|
menuPopupItem:registerScriptHandler(menuCallbackClosePopup)
|
||||||
|
menuPopup = CCMenu:menuWithItem(menuPopupItem)
|
||||||
|
menuPopup:setPosition(winSize.width / 2, winSize.height / 2)
|
||||||
|
menuPopup:setIsVisible(false)
|
||||||
|
layerMenu:addChild(menuPopup)
|
||||||
|
|
||||||
|
-- add the left-bottom "tools" menu to invoke menuPopup
|
||||||
|
local menuToolsItem = CCMenuItemImage:itemFromNormalImage("menu1.png", "menu1.png")
|
||||||
|
menuToolsItem:setPosition(0, 0)
|
||||||
|
menuToolsItem:registerScriptHandler(menuCallbackOpenPopup)
|
||||||
|
menuTools = CCMenu:menuWithItem(menuToolsItem)
|
||||||
|
menuTools:setPosition(30, 40)
|
||||||
|
layerMenu:addChild(menuTools)
|
||||||
|
|
||||||
|
return layerMenu
|
||||||
|
end
|
||||||
|
|
||||||
|
-- play background music, preload effect
|
||||||
|
SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
|
||||||
|
SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav");
|
||||||
|
|
||||||
-- play background music
|
|
||||||
CocosDenshion.SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
|
|
||||||
-- preload effect
|
|
||||||
CocosDenshion.SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav");
|
|
||||||
-- run
|
-- run
|
||||||
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)
|
local sceneGame = CCScene:node()
|
||||||
|
sceneGame:addChild(createLayerFram())
|
||||||
|
sceneGame:addChild(createLayerMenu())
|
||||||
|
CCDirector:sharedDirector():runWithScene(sceneGame)
|
||||||
|
|
Loading…
Reference in New Issue