教程说明技术工具:CoronaSDK执行难度:正常运行时间:30-60分钟平衡球类游戏的基本概念和基本用户的创建界面。第二部分,我们将共同完成基础物理效果的创建,游戏交互的编写,最终制作出一个能给用户带来乐趣的成品应用。第29步:添加物理效果。为游戏中的每个图形元素分配物理属性。请注意,不能移动静态元素。此外,检查玩家控制的球和陷阱孔的半径。这些元素必须声明使用圆形的物理属性,而不是普通的正方形,这将提高物理碰撞效果并提高游戏性。--AddPhysicstoGFXphysics.addBody(left,'static')physics.addBody(right,'static')physics.addBody(top,'static')physics.addBody(bottom,'static')physics.addBody(b1,'static')')physics.addBody(b2,'static')physics.addBody(b3,'static')physics.addBody(b4,'static')physics.addBody(h1,'static',{radius=15})物理。addBody(h2,'static',{radius=15})physics.addBody(h3,'static',{radius=15})physics.addBody(h4,'static',{radius=15})physics.addBody(h5,'static',{radius=15})physics.addBody(player,{radius=14})physics.addBody(goal,'static',{radius=15})Step30:设置traphole为SensorSince作为陷阱的小孔本身不会产生物理碰撞效果,我们只需要为其设置一个接触传感器即可。--SetHolesasSensorsh1.isSensor=trueh2.isSensor=trueh3.isSensor=trueh4.isSensor=trueh5.isSensor=true--gameListeners('add')endStep31:CodeReview下面列出的都是本教程提到的Codeoutline,你可以从宏观的角度检查工作,确保所有元素都包含在完成的程序中:--TeeterlikeGame--DevelopedbyCarlosYanez--HideStatusBardisplay.setStatusBar(display.HiddenStatusBar)--Physicslocalphysics=require('physics')physics。start()physics.setGravity(0,0)--Graphics--[Background]localbg=display.newImage('bg.png')--[TitleView]localtitleBglocalplayBtnlocalcreditsBtnlocaltitleView--[Credits]localcreditsView--[Player]localplayer--[BarsTable]localbars={}--[HolesTable]localholes={}--[Goal]localgoal--Soundslocalbell=audio.loadSound('bell.caf')localbuzz=audio.loadSound('buzz.caf')--FunctionslocalMain={}localstartButtonListeners={}localshowCredits={}localhideCredits={}localshowGameView={}localgameListeners={}localmovePlayer={}localonCollision={}localalert={}localdragPaddle={}--MainFunctionfunctionMain()titleBg=display.newImage('titleBg.png')playBtn=display.newImage('playBtn.png',display.contentCenterX-35.5,display.contentCenterY+10)creditsBtn=display.newImage('creditsBtn.png',display.contentCenterX-50.5,display.contentCenterY+65)titleView=display.newGroup(titleBg,playBtn,creditsBtn)startButtonListeners('add')endfunctionstartButtonListeners(action)if(action=='add')thenplayBtn:addEventListener('tap',showGameView)creditsBtn:addEventListener('tap',showCredits)elseplayBtn:removeEventListener('tap',showGameView)creditsBtn:removeEventListener('tap',showCredits)endendfunctionshowCredits:tap(e)playBtn.isVisible=falsecreditsBtn.isVisible=falsecreditsView=display.newImage('credits.png',0,display.contentHeight+40)transition.to(creditsView,{time=300,y=display.contentHeight-20,onComplete=function()creditsView:addEventListener('tap',hideCredits)end})endfunctionhideCredits:tap(e)playBtn.isVisible=truecreditsBtn.isVisible=truetransition.to(creditsView,{time=300,y=display.contentHeight+creditsView.height,onComplete=function()creditsView:removeEventListener('tap',hideCredits)display.remove(creditsView)creditsView=nilend})endfunctionshowGameView:tap(e)transition.to(titleView,{time=300,x=-titleView.height,onComplete=function()startButtonListeners('rmv')display.remove(titleView)titleView=nilend})--[AddGFX]--Goalgoal=display.newImage('goal.png')goal.x=439goal.y=31goal.name='g'--Wallslocalleft=display.newLine(-1,0,-1,display.contentHeight)localright=display.newLine(display.contentWidth+1,0,display.contentWidth+1,display.contentHeight)localtop=display.newLine(0,-3,display.contentWidth,-3)localbottom=display.newLine(0,display.contentHeight,display.contentWidth,display.contentHeight)--Barslocalb1=display.newImage('bar.png',92,67)localb2=display.newImage('bar.png',192,-2)localb3=display.newImage('bar.png',287,67)localb4=display.newImage('bar.png',387,-2)--Holeslocalh1=display.newImage('hole.png',62,76)localh2=display.newImage('hole.png',124,284)localh3=display.newImage('hole.png',223,224)localh4=display.newImage('hole.png',356,114)localh5=display.newImage('hole.png',380,256)h1.name='h'h2.name='h'h3.name='h'h4.name='h'h5.name='h'--Playerplayer=display.newImage('player.png')player.x=49player.y=288player:setReferencePoint(display.CenterReferencePoint)--AddPhysicstoGFXphysics.addBody(left,'static')physics.addBody(right,'static')physics.addBody(top,'static')physics.addBody(bottom,'static')physics.addBody(b1,'static')physics.addBody(b2,'static')physics.addBody(b3,'static')physics.addBody(b4,'static')physics.addBody(h1,'static',{radius=15})physics.addBody(h2,'static',{radius=15})physics.addBody(h3,'static',{radius=15})physics.addBody(h4,'static',{radius=15})physics.addBody(h5,'static',{radius=15})physics.addBody(player,{radius=14})physics.addBody(goal,'static',{radius=15})--SetHolesasSensorsh1.isSensor=trueh2.isSensor=trueh3.isSensor=trueh4.isSensor=trueh5.isSensor=truegameListeners('add')end步骤三十二:游戏监听器下栏目代码的作用是为应用添加重力加速度和物理碰撞监听器代码,通过提交参数去除这些效果。functiongameListeners(action)if(action=='add')thenRuntime:addEventListener('accelerometer',movePlayer)player:addEventListener('collision',onCollision)player:addEventListener('touch',dragPaddle)elseRuntime:removeEventListener('accelerometer',movePlayer)player:removeEventListener('collision',onCollision)player:removeEventListener('touch',dragPaddle)endend第33步:移动小球下面的函数用来捕捉物理加速度值,并给小球X和Y属性赋值.functionmovePlayer:accelerometer(e)player.x=player.x+(e.yGravity*-15)player.y=player.y+(e.xGravity*-15)end第34步:当小球与其他物体发生物理碰撞时发生碰撞时,将其名称与接触对象进行比较。根据物体的类型(陷阱洞和目的地),游戏会给出不同的提示。functiononCollision(e)if(e.other.name=='h')thenalert()elseif(e.other.name=='g')thenalert('win')endendStep35:Promptmessage提示信息是Whentriggered,游戏中的所有听众都被移除,并在播放声音时显示正确的文本内容。functionalert(action)localalertgameListeners('rmv')if(action=='win')thenalert=display.newImage('complete.png')alert.x=display.contentCenterXalert.y=display.contentCenterYtransition.from(alert,{time=300,xScale=0.3,yScale=0.3})audio.play(bell)elsealert=display.newImage('gameOver.png')alert.x=display.contentCenterXalert.y=display.contentCenterYtransition.from(alert,{time=300,xScale=0.3,yScale=0.3})audio.play(buzz)endend第36步:模拟运动这一步纯属建议,可以在模拟环境中加入以下代表来拖动小球,观察是否运动模式符合预期。functiondragPaddle(e)if(e.phase=='began')thenlastY=e.y-player.ylastX=e.x-player.xelseif(e.phase=='moved')thenplayer.y=e.y-lastYplayer.x=e.x-lastXendend第37步:调用Main函数为了在应用程序启动时对其进行初始化,我们需要调用Main函数。上面的代码写完后,我们只需要编辑如下内容就可以实现初始化需求:Main()步骤38:加载界面当我们启动指南针应用时,iOS系统会逐项加载基础数据。此时,默认。png文件将作为背景图案显示在主屏幕上。将此图像保存到我们的项目资源文件夹中,以便它自动添加到Corona的编译器中。第39步:图标现在是每个人的绘图技巧派上用场的时候了,让我们为您的应用程序创建一个漂亮且令人印象深刻的图标。在非视网膜iPhone设备上,图标文件大小应为57x57像素,而视网膜屏幕需要114x114像素,我们还需要为iTunes软件商店创建一个512x512图形的大版本。我建议您从512x512像素开始作为基准设计,然后缩小到其他两个尺寸。您无需在图标创建上投入太多精力,制作圆角或添加半透明效果完全是多余的——因为iTunes和iPhone会自动为您实现这些效果。第四十步:在模拟环境中进行测试到了最后测试的时候了。打开Corona模拟器,选择我们的项目文件夹并单击“打开”。如果一切都按预期顺利运行,那么我们可以继续执行最后一项工作。Step41:在Corona模拟器中创建,点击文件选项下的创建项,选择目标设备平台。在对话框中输入项目数据,然后单击“创建”按钮。等待几秒钟,我们的应用程序就完成了!然后您可以在设备上对其进行测试,或者直接将应用程序发布到软件商店。综上所述,后测越多越好。当我们对自己的应用作品进行细节打磨时,让我们发布用户版本——这可能是走向辉煌成功的第一步!希望这篇攻略文章能帮助大家在移动开发的道路上越走越好,感谢大家的支持!原文链接:http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-a-teeter-like-game-physics-and-interaction/
