在 Corona APK 中为图像添加监听器

我在 CoronaLabs Studio 中创建了一个新项目,并选择了“基于物理的游戏”选项,所有内容都作为初学者加载,但我不知道应该在哪里使用 addEventListener() 来让方块可以点击或在点击时被销毁。 我尝试了很多不同的方式,只是把这一行代码放在脚本中让盒子可点击 virus:applyLinearImpulse( 0, -0.25, virus.x, virus.y )

以下是 level1.lua 脚本。

在这种情况下,我有

require("toast")
local composer = require( "composer" )
local scene = composer.newScene()

-- 加入 Corona 的“物理”库
local physics = require("physics")

--------------------------------------------
tapCount = 0
tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )

--------------------------------------------

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function scene:create( event )

    -- 当场景的视图不存在时调用。
    -- 在此处插入代码以初始化场景,例如将显示对象添加到 'sceneGroup'、添加触摸监听器等。

    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

    -- OFFSCREEN BOX,设置位置并略微旋转
    local box = display.newImageRect( "box.png", 40, 40 )
    box.x, box.y = 160, -100
    box.rotation = 33
    -- 添加物理效果
    physics.addBody( box, { density=1.0, friction=0.3, bounce=0.2 } )

    -- 创建一个草地对象并添加物理(使用自定义形状)
    local grass = display.newImageRect( "grass.png", screenW, 82 )
    grass.anchorX = 0
    grass.anchorY = 1
    -- 在屏幕底部绘制草地
    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 } )

    sceneGroup:insert( background )
    sceneGroup:insert( grass )
    sceneGroup:insert( box )
end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if phase == "will" then
        -- 当场景仍未在屏幕上时调用
    elseif phase == "did" then
        -- 当场景现在在屏幕上时调用
        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

---------------------------------------------------------------------------------

-- 监听器设置
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene
点赞
用户9217577
用户9217577

如果我正确理解了你的问题,这里是一个例子,你可以点击一个方框并摧毁它。

function scene:create(event)

 local function destroyMe(event)
    display.remove(event.target)
    event.target=nil
  end

 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 } )

  box:addEventListener("tap",destroyMe)

end

如果你正在使用 touch 事件,那么那个 event 将会有三个阶段。因此要小心。

2020-03-26 08:46:49