最是人间留不住,朱颜辞镜花辞树。这篇文章主要讲述试图调用方法'
applyForce'
相关的知识,希望能为你提供帮助。
我正在制作侧卷轴,当我开始游戏时,我可以触摸屏幕以保持我的企鹅在空中,但是当我失败并与冰块碰撞并在重新启动后按下播放。我得到错误尝试调用方法'applyForce'继承我的代码
local function activatePengs(self,event)self:applyForce(0, -45, self.x, self.y)print("run")endlocal function touchScreen(event)print("touch")if event.phase == "began" thenpeng.enterFrame = activatePengs
Runtime:addEventListener("enterFrame", peng)endif event.phase == "ended" thenRuntime:removeEventListener("enterFrame", peng)endendlocal function onCollision(event)if event.phase == "began" thenprint "collide"composer.gotoScene( "restart",{ time=800, effect="crossFade" } )endend-- now comes four required functions for Composer:function scene:create( event )local sceneGroup = self.viewbg1 = display.newImageRect(sceneGroup, "bg.png", 800, 1000)bg2 = display.newImage(sceneGroup, "ice2.png",140,210)bg3 = display.newImage(sceneGroup, "ice2.png",540,210)bg4 = display.newImage(sceneGroup, "ice2.png",940,210)bg5 = display.newImage(sceneGroup, "ice2.png",1340,210)bg6 = display.newImage(sceneGroup, "ice2.png",1740,210)bg7 = display.newImage(sceneGroup, "ice1.png",140,420)bg8 = display.newImage(sceneGroup, "ice1.png",540,420)bg9 = display.newImage(sceneGroup, "ice1.png",940,420)bg10 = display.newImage(sceneGroup, "ice1.png",1340,420)bg11 = display.newImage(sceneGroup, "ice1.png",1740,420)peng = display.newImage(sceneGroup, "peng.png", 80, 201)
physics.addBody(peng, "dynamic", {density=.18, bounce=0.1, friction=.5, radius=55})
...
end
答案【试图调用方法' applyForce' 】可能是因为企鹅在调用函数时不是物理对象而发生错误。我想在去下一个场景之前删除enterFrame监听器应该可以解决你的问题。
我为您的代码添加了一些改进(未经测试):
local applyForceToPenguin = false local function enterFrame( self, event )if applyForceToPenguin thenself:applyForce( 0, -45, self.x, self.y )print("run")endendlocal function touchScreen(event)print("touch")if event.phase == "began" then applyForceToPenguin = true endif event.phase == "ended" then applyForceToPenguin = false endendlocal function onCollision(event)if event.phase == "began" thenprint "collide"applyForceToPenguin = falsecomposer.gotoScene( "restart",{ time=800, effect="crossFade" } )endend-- now comes four required functions for Composer:function scene:create( event )local sceneGroup = self.viewbg1= display.newImageRect( sceneGroup, "bg.png", 800, 1000 )
bg2= display.newImage( sceneGroup, "ice2.png", 140, 210 )
bg3= display.newImage( sceneGroup, "ice2.png", 540, 210 )
bg4= display.newImage( sceneGroup, "ice2.png", 940, 210 )
bg5= display.newImage( sceneGroup, "ice2.png", 1340, 210 )
bg6= display.newImage( sceneGroup, "ice2.png", 1740, 210 )
bg7= display.newImage( sceneGroup, "ice1.png", 140, 420 )
bg8= display.newImage( sceneGroup, "ice1.png", 540, 420 )
bg9= display.newImage( sceneGroup, "ice1.png", 940, 420 )
bg10 = display.newImage( sceneGroup, "ice1.png", 1340, 420 )
bg11 = display.newImage( sceneGroup, "ice1.png", 1740, 420 )peng = display.newImage( sceneGroup, "peng.png", 80, 201)
physics.addBody( peng, "dynamic", { density=.18, bounce=0.1, friction=.5, radius=55 } )
Runtime:addEventListener( "enterFrame", enterFrame )
...
endfunction scene:destroy()Runtime:removeEventListener( "enterFrame", enterFrame )endfunction scene:hide( event )
local sceneGroup = self.view
local phase = event.phaseif phase == "will" then
-- Code here runs when the scene is still off screen (but is about to come on screen)elseif phase == "did" then
-- Code here runs when the scene is entirely on screen
applyForceToPenguin = false
endendscene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
祝你今天愉快:)
推荐阅读
- 如何解决android studio的编译错误()
- 如何只用月份和年份字段显示android的日期选择器()
- Android Studio(SDK目录不存在)
- Android设备监视器无法在Ubuntu 17.10中打开
- 如何用android studio 2.3.2下载旧版本的android sdk平台()
- 如何使用Ionic 3为Android API 21构建apk(它继续为API 23构建,即使我在config.xml中将目标sdk更改为21)
- 无法在Android Studio上更改API级别
- 整合Applovin Sdk在统一android
- 在Android Studio中更新Android SDK API 28时出现问题