Create a Brick Breaker Game with the Corona SDK: Collision Detection

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

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

 

Step 35: Detect Brick Collisions

当球碰到砖块的时候,我们将使用与球板碰撞相同的方法来检查向哪边运行。代码如下:

302
303
304
305
306
307
308
309
function removeBrick(e)
-- Check the which side of the brick the ball hits, left, right
if (e.other.name ==  'brick' and (ball.x + ball.width * 0.5) < (e.other.x + e.other.width * 0.5)) then
xSpeed = -5
elseif(e.other.name ==  'brick' and (ball.x + ball.width * 0.5) >= (e.other.x + e.other.width * 0.5)) then
xSpeed = 5
end
end

 

 

Step 37: Remove Brick

当球碰到砖块的时候,砖块会消失

312
313
314
315
316
317
-- Bounce, Remove
if (e.other.name ==  'brick' ) then
ySpeed = ySpeed * -1
e.other:removeSelf()
e.other = nil
bricks.numChildren = bricks.numChildren - 1

 

 

Step 38: Add Score

每碰到一个砖块将获得100分

318
score = score + 1
scoreNum.text = score * SCORE_CONST
scoreNum:setReferencePoint(display.CenterLeftReferencePoint)
scoreNum.x = 54
end

 

Step 39: Check Bricks

当砖块全部被消掉的时候,玩家获胜

326
327
328
329
330
if (bricks.numChildren < 0) then
alert( '  You Win!' '  Next Level ›' )
gameEvent =  'win'
end
end

 

 

Step 40: Ball Movement

球的移动通过xSpeed和ySpeed变量来控制,当update函数执行的时候,球将会开始移动

332
333
334
335
336
function update(e)
-- Ball Movement
ball.x = ball.x + xSpeed
ball.y = ball.y + ySpeed

 

 

Step 41: Wall Collision

下面的代码检查球与墙的碰撞,并且当发生碰撞的时候给球一个反的移动方向

341
342
343
if (ball.x < 0) then ball.x = ball.x + 3 xSpeed = -xSpeed end--Left
if ((ball.x + ball.width) > display.contentWidth) then ball.x = ball.x - 3 xSpeed = -xSpeed end--Right
if (ball.y < 0) then ySpeed = -ySpeed end--Up

 

 

Step 42: Loss Condition

344
if (ball.y + ball.height > paddle.y + paddle.height) then alert( '  You Lose' '  Play Again ›' ) gameEvent =  'lose' end--down/lose

 

 

Step 43: Game Status Message

347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
function alert(t, m)
gameListeners( 'remove' )
alertBg = display.newImage( 'alertBg.png' )
box = display.newImage( 'alertBox.png' , 90, 202)
transition.from(box, {time = 300, xScale = 0.5, yScale = 0.5, transition = easing.outExpo})
titleTF = display.newText(t, 0, 0,  'akashi' , 19)
titleTF:setTextColor(254,203,50)
titleTF:setReferencePoint(display.CenterReferencePoint)
titleTF.x = display.contentCenterX
titleTF.y = display.contentCenterY - 15
msgTF = display.newText(m, 0, 0,  'akashi' , 12)
msgTF:setTextColor(254,203,50)
msgTF:setReferencePoint(display.CenterReferencePoint)
msgTF.x = display.contentCenterX
msgTF.y = display.contentCenterY + 15
box:addEventListener( 'tap' , restart)
alertScreen = display.newGroup()
alertScreen:insert(alertBg)
alertScreen:insert(box)
alertScreen:insert(titleTF)
alertScreen:insert(msgTF)
end

 

 

Step 44: Restart

376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
function restart(e)
if (gameEvent ==  'win' and table.maxn(levels) > currentLevel) then
currentLevel = currentLevel + 1
changeLevel(levels[currentLevel])--next level
levelNum.text = tostring(currentLevel)
elseif(gameEvent ==  'win' and table.maxn(levels) &lt;= currentLevel) then
box:removeEventListener( 'tap' , restart)
alertScreen:removeSelf()
alertScreen = nil
alert( '  Game Over' '  Congratulations!' )
gameEvent =  'finished'
elseif(gameEvent ==  'lose' ) then
changeLevel(levels[currentLevel])--same level
elseif(gameEvent ==  'finished' ) then
addMenuScreen()
transition.from(menuScreen, {time = 300, y = -menuScreen.height, transition = easing.outExpo})
box:removeEventListener( 'tap' , restart)
alertScreen:removeSelf()
alertScreen = nil
currentLevel =
scoreText:removeSelf()
scoreText = nil
scoreNum:removeSelf()
scoreNum = nil
levelText:removeSelf()
levelText = nil
levelNum:removeSelf()
levelNum = nil
ball:removeSelf()
ball = nil
paddle:removeSelf()
paddle = nil
score = 0
end
end

 

 

Step 45: Change Level

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
function changeLevel(level)
-- Clear Level Bricks
bricks:removeSelf()
bricks.numChildren = 0
bricks = display.newGroup()
-- Remove Alert
box:removeEventListener( 'tap' , restart)
alertScreen:removeSelf()
alertScreen = nil
-- Reset Ball and Paddle position
ball.x = (display.contentWidth * 0.5) - (ball.width * 0.5)
ball.y = (paddle.y - paddle.height) - (ball.height * 0.5) -2
paddle.x = display.contentWidth * 0.5
-- Redraw Bricks
buildLevel(level)
-- Start
background:addEventListener( 'tap' , startGame)
end

 

 

Step 46: Call Main Function

调用Main()函数来开始程序

448
Main()

 

 

Step 47: Create the Loading Screen

添加Default.png到工程目录,Corona编译器会自动将其添加为Loading Screen

 

Step 48: Icon

注意不同平台需要的icon大小不一样

 

 

Step 49: Testing in the Simulator

使用Corona模拟器打开项目所在目录即可进行测试

 

Step 50: Build

从Corona模拟器的File菜单中选择Build即可开始编译

 

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

 

 

 

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值