向Corona APK中的图像添加侦听器

问题描述 投票:0回答:1

[我在CoronaLabs Studio中创建了一个具有Physics-Based Game开始选项的新项目,所有内容都作为初学者加载,我不知道该放在addEventListener()的位置,以便可以使该框可单击或在单击时被破坏?我尝试了多种方法将这行代码放在脚本中,以使框可单击virus:applyLinearImpulse( 0, -0.25, virus.x, virus.y )

这里是level1.lua脚本。

在这种情况下,我有

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

-- include Corona's "physics" library
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 )

    -- Called when the scene's view does not exist.
    -- INSERT code here to initialize the scene e.g. add display objects to 'sceneGroup', add touch listeners, etc.


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

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

    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
        -- 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
lua apk corona
1个回答
0
投票

如果我正确理解了您的问题,下面的示例可以单击一个框并销毁它。

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
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.