自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Corona SDK游戲開發(fā)實例(二):物理效果與互動操作

譯文
移動開發(fā) Android iOS 移動應(yīng)用 游戲開發(fā)
在本系列指導(dǎo)教程中,我們共同來學(xué)習(xí)如何創(chuàng)建一款平衡球類游戲。這款游戲的目標(biāo)是保持小球平衡、避開各種障礙物并最終到達(dá)終點。簡單的小游戲,咱們馬上進(jìn)入正題!

[[90110]]教程說明

  • 技術(shù)工具: Corona SDK
  • 執(zhí)行難度: 普通
  • 操作時間: 30 到60分鐘

書接上文

在本系列文章的前編中,我們了解了平衡球小游戲的基本概念并創(chuàng)建出基礎(chǔ)用戶界面。在后編中,我們將共同完成基礎(chǔ)物理效果創(chuàng)建、游戲互動性編寫等工作,并最終制作出能夠給用戶帶來樂趣的應(yīng)用成品。

步驟二十九: 添加物理效果

為游戲中的各個圖形元素分配物理屬性,要注意靜態(tài)元素是不能移動的。另外還要檢查玩家操控的小球與陷阱洞的半徑,這些元素必須聲明使用圓形物理屬性而不能用一般的方形,這樣會提高物理碰撞效果、提高游戲性。

  1. -- Add Physics to GFX  
  2.  
  3. physics.addBody(left, 'static'
  4. physics.addBody(right, 'static'
  5. physics.addBody(top, 'static'
  6. physics.addBody(bottom, 'static')  
  7.  
  8. physics.addBody(b1, 'static'
  9. physics.addBody(b2, 'static'
  10. physics.addBody(b3, 'static'
  11. physics.addBody(b4, 'static')  
  12.  
  13. physics.addBody(h1, 'static', {radius = 15}) 
  14. physics.addBody(h2, 'static', {radius = 15}) 
  15. physics.addBody(h3, 'static', {radius = 15}) 
  16. physics.addBody(h4, 'static', {radius = 15}) 
  17. physics.addBody(h5, 'static', {radius = 15}) 
  18.   
  19. physics.addBody(player, {radius = 14}) 
  20. physics.addBody(goal, 'static', {radius = 15}) 

步驟三十: 將陷阱小洞設(shè)置為感應(yīng)器

由于作為陷阱的小洞本身不會產(chǎn)生物理碰撞效果,因此我們只要為其設(shè)置接觸感應(yīng)器即可。

  1. -- Set Holes as Sensors      
  2.  
  3.     h1.isSensor = true 
  4.     h2.isSensor = true 
  5.     h3.isSensor = true 
  6.     h4.isSensor = true 
  7.     h5.isSensor = true      
  8.  
  9.     --gameListeners('add'
  10. end 

步驟三十一: 代碼審查

以下列出的是本教程所提到全部代碼綱要,大家可以從宏觀角度對作品進(jìn)行核查,確定所有要素都已經(jīng)包含在程序成品當(dāng)中:

  1. -- Teeter like Game 
  2.  
  3. -- Developed by Carlos Yanez 
  4.  
  5.   
  6.  
  7. -- Hide Status Bar 
  8.  
  9.   
  10.  
  11. display.setStatusBar(display.HiddenStatusBar) 
  12.  
  13.   
  14.  
  15. -- Physics 
  16.  
  17.   
  18.  
  19. local physics = require('physics'
  20.  
  21. physics.start() 
  22.  
  23. physics.setGravity(0, 0) 
  24.  
  25.   
  26.  
  27. -- Graphics 
  28.  
  29.   
  30.  
  31. -- [Background] 
  32.  
  33.   
  34.  
  35. local bg = display.newImage('bg.png'
  36.  
  37.   
  38.  
  39. -- [Title View] 
  40.  
  41.   
  42.  
  43. local titleBg 
  44.  
  45. local playBtn 
  46.  
  47. local creditsBtn 
  48.  
  49. local titleView 
  50.  
  51.   
  52.  
  53. -- [Credits] 
  54.  
  55.   
  56.  
  57. local creditsView 
  58.  
  59.   
  60.  
  61. -- [Player] 
  62.  
  63.   
  64.  
  65. local player 
  66.  
  67.   
  68.  
  69. -- [Bars Table] 
  70.  
  71.   
  72.  
  73. local bars = {} 
  74.  
  75.   
  76.  
  77. -- [Holes Table] 
  78.  
  79.   
  80.  
  81. local holes = {} 
  82.  
  83.   
  84.  
  85. -- [Goal] 
  86.  
  87.   
  88.  
  89. local goal 
  90.  
  91.   
  92.  
  93. -- Sounds 
  94.  
  95.   
  96.  
  97. local bell = audio.loadSound('bell.caf'
  98.  
  99. local buzz = audio.loadSound('buzz.caf'
  100.  
  101.   
  102.  
  103. -- Functions 
  104.  
  105.   
  106.  
  107. local Main = {} 
  108.  
  109. local startButtonListeners = {} 
  110.  
  111. local showCredits = {} 
  112.  
  113. local hideCredits = {} 
  114.  
  115. local showGameView = {} 
  116.  
  117. local gameListeners = {} 
  118.  
  119. local movePlayer = {} 
  120.  
  121. local onCollision = {} 
  122.  
  123. local alert = {} 
  124.  
  125. local dragPaddle = {} 
  126.  
  127.   
  128.  
  129. -- Main Function 
  130.  
  131.   
  132.  
  133. function Main() 
  134.  
  135.     titleBg = display.newImage('titleBg.png'
  136.  
  137.     playBtn = display.newImage('playBtn.png', display.contentCenterX - 35.5, display.contentCenterY + 10) 
  138.  
  139.     creditsBtn = display.newImage('creditsBtn.png', display.contentCenterX - 50.5, display.contentCenterY + 65) 
  140.  
  141.     titleView = display.newGroup(titleBg, playBtn, creditsBtn) 
  142.  
  143.       
  144.  
  145.     startButtonListeners('add'
  146.  
  147. end 
  148.  
  149.   
  150.  
  151. function startButtonListeners(action) 
  152.  
  153.     if(action == 'add') then 
  154.  
  155.         playBtn:addEventListener('tap', showGameView) 
  156.  
  157.         creditsBtn:addEventListener('tap', showCredits) 
  158.  
  159.     else 
  160.  
  161.         playBtn:removeEventListener('tap', showGameView) 
  162.  
  163.         creditsBtn:removeEventListener('tap', showCredits) 
  164.  
  165.     end 
  166.  
  167. end 
  168.  
  169.   
  170.  
  171. function showCredits:tap(e) 
  172.  
  173.     playBtn.isVisible = false 
  174.  
  175.     creditsBtn.isVisible = false 
  176.  
  177.     creditsView = display.newImage('credits.png', 0, display.contentHeight+40) 
  178.  
  179.     transition.to(creditsView, {time = 300, y = display.contentHeight-20, onComplete = function() creditsView:addEventListener('tap', hideCredits) end}) 
  180.  
  181. end 
  182.  
  183.   
  184.  
  185. function hideCredits:tap(e) 
  186.  
  187.     playBtn.isVisible = true 
  188.  
  189.     creditsBtn.isVisible = true 
  190.  
  191.     transition.to(creditsView, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end}) 
  192.  
  193. end 
  194.  
  195.   
  196.  
  197. function showGameView:tap(e) 
  198.  
  199.     transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end}) 
  200.  
  201.       
  202.  
  203.     -- [Add GFX] 
  204.  
  205.       
  206.  
  207.     -- Goal 
  208.  
  209.       
  210.  
  211.     goal = display.newImage('goal.png'
  212.  
  213.     goal.x = 439 
  214.  
  215.     goal.y = 31 
  216.  
  217.     goal.name = 'g' 
  218.  
  219.       
  220.  
  221.     -- Walls 
  222.  
  223.       
  224.  
  225.     local left = display.newLine(-1, 0, -1, display.contentHeight) 
  226.  
  227.     local right = display.newLine(display.contentWidth+1, 0, display.contentWidth+1, display.contentHeight) 
  228.  
  229.     local top = display.newLine(0, -3, display.contentWidth, -3) 
  230.  
  231.     local bottom = display.newLine(0, display.contentHeight, display.contentWidth, display.contentHeight) 
  232.  
  233.       
  234.  
  235.     -- Bars 
  236.  
  237.       
  238.  
  239.     local b1 = display.newImage('bar.png', 92, 67) 
  240.  
  241.     local b2 = display.newImage('bar.png', 192, -2) 
  242.  
  243.     local b3 = display.newImage('bar.png', 287, 67) 
  244.  
  245.     local b4 = display.newImage('bar.png', 387, -2) 
  246.  
  247.       
  248.  
  249.     -- Holes 
  250.  
  251.       
  252.  
  253.     local h1 = display.newImage('hole.png', 62, 76) 
  254.  
  255.     local h2 = display.newImage('hole.png', 124, 284) 
  256.  
  257.     local h3 = display.newImage('hole.png', 223, 224) 
  258.  
  259.     local h4 = display.newImage('hole.png', 356, 114) 
  260.  
  261.     local h5 = display.newImage('hole.png', 380, 256) 
  262.  
  263.       
  264.  
  265.     h1.name = 'h' 
  266.  
  267.     h2.name = 'h' 
  268.  
  269.     h3.name = 'h' 
  270.  
  271.     h4.name = 'h' 
  272.  
  273.     h5.name = 'h' 
  274.  
  275.       
  276.  
  277.     -- Player 
  278.  
  279.       
  280.  
  281.     player = display.newImage('player.png'
  282.  
  283.     player.x = 49 
  284.  
  285.     player.y = 288 
  286.  
  287.     player:setReferencePoint(display.CenterReferencePoint) 
  288.  
  289.       
  290.  
  291.     -- Add Physics to GFX 
  292.  
  293.       
  294.  
  295.     physics.addBody(left, 'static'
  296.  
  297.     physics.addBody(right, 'static'
  298.  
  299.     physics.addBody(top, 'static'
  300.  
  301.     physics.addBody(bottom, 'static'
  302.  
  303.       
  304.  
  305.     physics.addBody(b1, 'static'
  306.  
  307.     physics.addBody(b2, 'static'
  308.  
  309.     physics.addBody(b3, 'static'
  310.  
  311.     physics.addBody(b4, 'static'
  312.  
  313.       
  314.  
  315.     physics.addBody(h1, 'static', {radius = 15}) 
  316.  
  317.     physics.addBody(h2, 'static', {radius = 15}) 
  318.  
  319.     physics.addBody(h3, 'static', {radius = 15}) 
  320.  
  321.     physics.addBody(h4, 'static', {radius = 15}) 
  322.  
  323.     physics.addBody(h5, 'static', {radius = 15}) 
  324.  
  325.       
  326.  
  327.     physics.addBody(player, {radius = 14}) 
  328.  
  329.     physics.addBody(goal, 'static', {radius = 15}) 
  330.  
  331.       
  332.  
  333.     -- Set Holes as Sensors 
  334.  
  335.       
  336.  
  337.     h1.isSensor = true 
  338.  
  339.     h2.isSensor = true 
  340.  
  341.     h3.isSensor = true 
  342.  
  343.     h4.isSensor = true 
  344.  
  345.     h5.isSensor = true 
  346.  
  347.       
  348.  
  349.     gameListeners('add'
  350.  
  351. end 

步驟三十二: 游戲監(jiān)聽器

下列代碼的作用是為應(yīng)用程序添加重力加速及物理碰撞監(jiān)聽器。代碼還能通過遞交參數(shù)來移除這些效果。

  1. function gameListeners(action) 
  2.  
  3.     if(action == 'add') then 
  4.  
  5.         Runtime:addEventListener('accelerometer', movePlayer) 
  6.  
  7.         player:addEventListener('collision', onCollision) 
  8.  
  9.         player:addEventListener('touch', dragPaddle) 
  10.  
  11.     else 
  12.  
  13.         Runtime:removeEventListener('accelerometer', movePlayer) 
  14.  
  15.         player:removeEventListener('collision', onCollision) 
  16.  
  17.         player:removeEventListener('touch', dragPaddle) 
  18.  
  19.     end 
  20.  
  21. end 

步驟三十三: 移動小球

以下函數(shù)用來捕捉物理加速值,并根據(jù)結(jié)果給小球的X及Y屬性賦值。

  1. function movePlayer:accelerometer(e) 
  2.  
  3.     player.x = player.x + (e.yGravity*-15) 
  4.  
  5.     player.y = player.y + (e.xGravity*-15) 
  6.  
  7. end 

步驟三十四: 物理碰撞

當(dāng)小球與其它對象發(fā)生碰撞時,其名稱會與觸碰對象相比照。根據(jù)對象類型的不同(陷阱小洞及目的地),游戲會給出不同的提示信息。

  1. function onCollision(e) 
  2.  
  3.     if(e.other.name == 'h') then 
  4.  
  5.         alert() 
  6.  
  7.     elseif(e.other.name == 'g') then 
  8.  
  9.         alert('win'
  10.  
  11.     end 
  12.  
  13. end 

步驟三十五: 提示信息

提示信息被觸發(fā)時,游戲中的所有監(jiān)聽器都會被移除,并在播放音效的同時顯示正確的文本內(nèi)容。

  1. function alert(action) 
  2.  
  3.     local alert 
  4.  
  5.       
  6.  
  7.     gameListeners('rmv'
  8.  
  9.       
  10.  
  11.     if(action == 'win') then 
  12.  
  13.         alert = display.newImage('complete.png'
  14.  
  15.         alert.x = display.contentCenterX 
  16.  
  17.         alert.y = display.contentCenterY 
  18.  
  19.         transition.from(alert, {time = 300, xScale = 0.3, yScale = 0.3}) 
  20.  
  21.         audio.play(bell) 
  22.  
  23.     else 
  24.  
  25.         alert = display.newImage('gameOver.png'
  26.  
  27.         alert.x = display.contentCenterX 
  28.  
  29.         alert.y = display.contentCenterY 
  30.  
  31.         transition.from(alert, {time = 300, xScale = 0.3, yScale = 0.3}) 
  32.  
  33.         audio.play(buzz) 
  34.  
  35.     end 
  36.  
  37. end 

步驟三十六: 模擬移動

這一步純屬建議,大家可以將下列代表添加進(jìn)來,借以在模擬環(huán)境下拖動小球,觀察移動方式是否與預(yù)期相符。

  1. function dragPaddle(e) 
  2.  
  3.     if(e.phase == 'began') then 
  4.  
  5.         lastY = e.y - player.y 
  6.  
  7.         lastX = e.x - player.x 
  8.  
  9.     elseif(e.phase == 'moved') then 
  10.  
  11.         player.y = e.y - lastY 
  12.  
  13.         player.x = e.x- lastX 
  14.  
  15.     end 
  16.  
  17. end 

步驟三十七: 調(diào)用Main函數(shù)

為了在應(yīng)用啟動時進(jìn)行初始化,我們需要調(diào)用Main函數(shù)。上述代碼編寫完成之后,我們只需編輯以下內(nèi)容即可實現(xiàn)初始化需求:

  1. Main() 

步驟三十八: 載入界面

[[90111]]

當(dāng)我們啟動指南針應(yīng)用時,iOS系統(tǒng)會逐項載入基本數(shù)據(jù),這時Default.png文件將作為背景圖案顯示在主屏幕當(dāng)中。將這張圖片保存到我們的項目資源文件夾中,這樣它就會被自動添加到Corona的編譯器中。

步驟三十九: 圖標(biāo)

[[90110]]

現(xiàn)在大家的做圖功力就該派上用場了,快為自己的應(yīng)用打造一款美觀又令人印象深刻的圖標(biāo)吧。在非視網(wǎng)膜屏的iPhone設(shè)備上,圖標(biāo)文件的尺寸應(yīng)為57x57像素,而視網(wǎng)膜屏則需要114x114像素,另外我們還需要為iTunes軟件商店打造一個512x512的大版圖形。我建議大家先以512x512像素為基準(zhǔn)設(shè)計,然后再縮小成其它兩種尺寸。

大家沒必要在圖標(biāo)制作方面過分投入精力,制作圓角或者添加半透明特效完全是種花蛇添足——因為iTunes與iPhone會自動為你實現(xiàn)這些效果。

步驟四十: 在模擬環(huán)境下進(jìn)行測試

是時候進(jìn)行最終測試了。打開Corona模擬器,選擇我們的項目文件夾并點擊“打開”。如果一切都依照預(yù)期效果順利運行,那么我們就可以著手做***一項工作了。

步驟四十一: 創(chuàng)建

在Corona模擬器中,點選文件選項下的創(chuàng)建項并選擇目標(biāo)設(shè)備平臺。在對話框中輸入項目數(shù)據(jù)并點擊創(chuàng)建按鈕。等上幾秒,我們的應(yīng)用作品就大功告成啦!接下來大家可以在設(shè)備上進(jìn)行實機(jī)測試,或者直接將應(yīng)用發(fā)布到軟件商店中。

總結(jié)

后期測試總是越多越好,當(dāng)我們對自己的應(yīng)用作品詳加打磨后,發(fā)行用戶版吧——這也許會成為輝煌成功的***步!

希望這篇指南文章能夠幫助大家在移動開發(fā)的道路上越走越好,感謝朋友們的支持!

 

原文鏈接:

http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-teeter-like-game-physics-and-interaction/

責(zé)任編輯:佚名 來源: 51CTO.com
相關(guān)推薦

2012-08-09 08:49:30

CoronaCorona SDKCorona SDK游

2012-12-13 13:27:29

Corona SDK

2012-12-13 09:20:55

Corona 2.0Corona SDK下

2013-04-27 16:14:33

Corona

2012-12-13 10:55:25

CoronaCorona SDK

2011-07-08 14:04:40

LuaCorona

2013-08-01 14:03:49

JavaScript

2012-03-06 08:47:40

Corona

2012-07-12 17:10:40

頑石互動吳剛迭代開發(fā)

2013-05-20 15:42:22

2011-04-25 14:45:38

2011-07-27 17:07:06

iPhone 游戲 Cocos2d

2011-07-22 18:20:04

IOS View 動畫

2009-06-05 09:24:53

struts標(biāo)簽jsp

2020-12-28 06:29:31

Bash互動游戲Linux

2011-12-31 21:28:33

2012-08-07 09:20:48

CoronaCorona SDKCorona SDK指

2011-07-11 09:58:52

2013-05-21 11:26:49

Android游戲開發(fā)Sensor感應(yīng)

2011-08-08 15:56:18

iPhone 震動 NSUserDefa
點贊
收藏

51CTO技術(shù)棧公眾號