Create a Brick Breaker Game with the Corona SDK: Game Controls

来源:互联网 发布:记账软件onlinedown 编辑:程序博客网 时间:2024/06/06 20:39

原创文章,转载请注明: 转载自All-iPad.net 本文链接地址: Create a Brick Breaker Game with the Corona SDK: Game Controls

这是系列教程的第二部分,完整内容点击下页的链接:

  • Create a Brick Breaker Game with the Corona SDK: Application Setup
  • Create a Brick Breaker Game with the Corona SDK: Game Controls
  • Create a Brick Breaker Game with the Corona SDK: Collision Detection

 

Step 16: Function Declarations

在第15步我们定义了保存关卡数据的多维数组,接下来我们将定义会用到的函数

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
local addMenuScreen = {}
local tweenMS = {}
local hideAbout = {}
local rmvAbout = {}
local addGameScreen = {}
local buildLevel = {}
local movePaddle = {}
local gameListeners = {}
local startGame = {}
local update = {}
local bounce = {}
local removeBrick = {}
local alert = {}
local restart = {}
local changeLevel = {}

 

Step 17: Constructor Code

接下来是主函数Main();该函数在程序启动的时候会被调用

101
102
103
local function Main()
addMenuScreen()
end

 

Step 18: Add Menu Screen

接下来的代码片段把主界面图像添加到画布上,并且将其保存到menuScreen组

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function addMenuScreen()
menuScreen = display.newGroup()
mScreen = display.newImage('mScreen.png')
startB = display.newImage('startB.png')
startB.name = 'startB'
aboutB = display.newImage('aboutB.png')
aboutB.name = 'aboutB'
menuScreen:insert(mScreen)
startB.x = 160
startB.y = 260
menuScreen:insert(startB)
aboutB.x = 160
aboutB.y = 310
menuScreen:insert(aboutB)

 

Step 19: Button Listeners

添加按钮响应函数

121
122
123
startB:addEventListener('tap', tweenMS)
aboutB:addEventListener('tap', tweenMS)
end

 

Step 20: Call About Screen

按钮响应函数的实现代码

125
126
127
128
129
130
131
132
133
function tweenMS:tap(e)
if(e.target.name == 'startB') then
-- Start Game
transition.to(menuScreen, {time = 300, y = -menuScreen.height, transition = easing.outExpo, onComplete = addGameScreen})
else
-- Call AboutScreen
aboutScreen = display.newImage('aboutScreen.png')
transition.from(aboutScreen, {time = 300, x = menuScreen.contentWidth, transition = easing.outExpo})
aboutScreen:addEventListener('tap', hideAbout)

 

Step 21: Hide Menu Buttons

下面几行代码用于隐藏主菜单界面

135
136
137
138
startB.isVisible = false;
aboutB.isVisible = false;
end
end

 

Step 23: Remove About Screen

140
141
142
143
144
145
146
147
148
function hideAbout:tap(e)
transition.to(aboutScreen, {time = 300, x = aboutScreen.width*2, transition = easing.outExpo, onComplete = rmvAbout})
end
function rmvAbout()
aboutScreen:removeSelf()
-- Enable Menu Buttons
startB.isVisible = true;
aboutB.isVisible = true;
end

 

Step 24: Destroy Menu Screen

当玩家点击start按钮的时候,我们首先要做的是销毁菜单界面,然后再创建游戏界面

150
151
152
153
154
155
function addGameScreen()
-- Destroy Menu Screen
menuScreen:removeSelf()
menuScreen = nil

 

Step 25: Add Game Screen

157
158
159
160
161
162
163
164
165
-- Add Game Screen
paddle = display.newImage('paddle.png')
ball = display.newImage('ball.png')
paddle.x = 160
paddle.y = 460
ball.x = 160
ball.y = 446
paddle.name = 'paddle'
ball.name = 'ball'

 

Step 26: Call Build Level Function

接下来我们将构建关卡,该函数在后面解释

167
buildLevel(levels[1])

 

Step 27: Scores & Levels Text

169
170
171
172
173
174
175
176
scoreText = display.newText('Score:', 5, 2, 'akashi', 14)
scoreText:setTextColor(254, 203, 50)
scoreNum = display.newText('0', 54, 2, 'akashi', 14)
scoreNum:setTextColor(254,203,50)
levelText = display.newText('Level:', 260, 2, 'akashi', 14)
levelText:setTextColor(254, 203, 50)
levelNum = display.newText('1', 307, 2, 'akashi', 14)
levelNum:setTextColor(254,203,50)

 

Step 28: Start Listener

我们在背景图上添加一个监听器,当玩家点击背景图后开始游戏

177
178
background:addEventListener('tap', startGame)
end

 

Step 29: Move Paddle

我们使用平衡仪来控制球板

180
181
182
183
function movePaddle:accelerometer(e)
-- Accelerometer Movement
paddle.x = display.contentCenterX + (display.contentCenterX * (e.xGravity*3))

 

Step 30: Paddle Border Collision

我们在屏幕边缘放置不可见的边框来防止球板跑到屏幕外面去

185
186
187
188
189
190
if((paddle.x - paddle.width * 0.5) < 0) then
paddle.x = paddle.width * 0.5
elseif((paddle.x + paddle.width * 0.5) > display.contentWidth) then
paddle.x = display.contentWidth - paddle.width * 0.5
end
end

 

Step 31: Build Level Function

关卡构建函数

该函数使用两层循环来检查关卡数据,在数据为1的地方放置砖块。注意在生成砖块的时候将其物理数据设置为static,这表示其不会主动做碰撞检测,在程序中只有唯一的一个dynamic对象,球,来做碰撞检测。

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function buildLevel(level)
-- Level length, height
local len = table.maxn(level)
bricks:toFront()
for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local brick = display.newImage('brick.png')
brick.name = 'brick'
brick.x = BRICK_W * j - OFFSET
brick.y = BRICK_H * i
physics.addBody(brick, {density = 1, friction = 0, bounce = 0})
brick.bodyType = 'static'
bricks.insert(bricks, brick)
end
end
end
end

 

Step 32: Game Listeners

214
215
216
217
218
219
220
221
222
223
224
225
226
function gameListeners(action)
if(action == 'add') then
Runtime:addEventListener('accelerometer', movePaddle)
--Runtime:addEventListener('enterFrame', update)
paddle:addEventListener('collision', bounce)
--ball:addEventListener('collision', removeBrick)
else
Runtime:removeEventListener('accelerometer', movePaddle)
--Runtime:removeEventListener('enterFrame', update)
paddle:removeEventListener('collision', bounce)
--ball:removeEventListener('collision', removeBrick)
end
end

 

Step 33: Start Game

228
229
230
231
232
233
234
235
function startGame:tap(e)
background:removeEventListener('tap', startGame)
gameListeners('add')
-- Physics
physics.addBody(paddle, {density = 1, friction = 0, bounce = 0})
physics.addBody(ball, {density = 1, friction = 0, bounce = 0})
paddle.bodyType = 'static'
end

 

Step 34: Paddle-Ball Collisions

当球碰到球板的时候,ySpeed被设为负值,这样让其向上移动。我们同时也会检查球碰到了球板的哪一边,以决定它向哪边移动。

237
238
239
240
241
242
243
244
245
246
247
248
function bounce(e)
ySpeed = -5
-- Paddle Collision, check the which side of the paddle the ball hits, left, right
if((ball.x + ball.width * 0.5) < paddle.x) then
xSpeed = -5
elseif((ball.x + ball.width * 0.5) >= paddle.x) then
xSpeed = 5
end
end
-- Run the Code
Main()

 

Next in the Series

接下来我们将会处理砖块和墙的碰撞,以及分数的显示,测试,构建app等等。

 

 

 

原文地址:http://mobile.tutsplus.com/tutorials/corona/create-a-brick-breaker-game-with-the-corona-sdk-game-controls/

原创文章,转载请注明: 转载自All-iPad.net

本文链接地址: Create a Brick Breaker Game with the Corona SDK: Game Controls

原创粉丝点击