/
hello.lua
146 lines (107 loc) · 4.44 KB
/
hello.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- 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.__CCNode__:getPosition()
layerFarm.__CCNode__:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y))
pointBegin = pointMove
end
end
function btnTouchBegin(e)
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:animationWithName("wait", 0.5, animFrames)
animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
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)
function tick()
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
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)
-- run
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)