CoronaLabs物理游戏中为Box添加addEventListener的位置疑问
在CoronaLabs Studio中给物理Box添加点击响应的正确方式
嘿,我完全懂你作为新手的困惑——刚把物理游戏的基础内容搭好,却不知道该把点击事件监听器放在哪儿才能让Box有反应。别担心,我来一步步帮你搞定这个问题:
核心思路
在Corona里,要让物理对象响应点击,你需要把**触摸/点击监听器直接绑定到目标对象(也就是你的Box)**上,而且要确保在对象创建完成后再添加监听器,同时要贴合场景的生命周期逻辑。
具体修改步骤
- 编写点击事件处理函数:定义点击Box后要执行的操作(比如施加冲量、销毁对象或者更新点击计数)。
- 给Box绑定监听器:在Box对象创建并添加物理属性后,把监听器挂载到它上面。
- (可选但重要)规范tapText的管理:你原来的tapText在场景函数外,场景切换时可能无法被正确清理,最好把它移到create函数里并加入场景组。
修改后的完整代码
我已经给你的level1.lua做了针对性修改,关键改动都加了注释:
require("toast") local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require("physics") -------------------------------------------- local tapCount = 0 local tapText -- 先声明变量,后续在create中初始化 -------------------------------------------- local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX function scene:create( event ) local sceneGroup = self.view physics.start() physics.pause() local background = display.newImageRect("game-background.png", 1170, 658) background.x = display.contentCenterX background.y = display.contentCenterY -- 将tapText移到create内创建,方便加入场景组统一管理 tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 ) -- OFFSCREEN BOX, position it, and rotate slightly local box = display.newImageRect( "box.png", 40, 40 ) box.x, box.y = 160, -100 box.rotation = 33 -- add physics physics.addBody( box, { density=1.0, friction=0.3, bounce=0.2 } ) -- 1. 定义Box的点击处理函数 local function onBoxTap( event ) -- 给Box施加向上的冲量,event.target就是被点击的Box对象 event.target:applyLinearImpulse( 0, -0.25, event.target.x, event.target.y ) -- 如果需要点击后销毁Box,取消下面这行注释即可 -- display.remove(event.target) -- 更新点击计数文本 tapCount = tapCount + 1 tapText.text = tapCount return true -- 阻止事件向下层对象传递 end -- 2. 给Box绑定tap事件监听器 box:addEventListener( "tap", onBoxTap ) -- create a grass object and add physics (with custom shape) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 -- draw the grass at the very bottom of the screen grass.x, grass.y = display.screenOriginX, display.actualContentHeight + display.screenOriginY local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) -- 3. 将所有显示对象加入场景组,包括tapText sceneGroup:insert( background ) sceneGroup:insert( grass ) sceneGroup:insert( box ) sceneGroup:insert( tapText ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off-screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on-screen physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on-screen and is about to move off-screen physics.stop() elseif phase == "did" then -- Called when the scene is now off-screen end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) local sceneGroup = self.view package.loaded[physics] = nil physics = nil end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene
为什么之前的尝试没成功?
大概率是因为你没有把监听器直接绑定到Box对象上,而是误加到了场景或其他无关对象上;或者是监听器添加的时机不对(比如在Box还没创建完成时就绑定了)。上面的代码里,我们在Box创建并添加物理属性后立刻绑定监听器,确保对象已经存在,这样点击就能正常触发响应了。
内容的提问来源于stack exchange,提问作者Ecodor
相关产品推荐
相关产品推荐

