Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 将侦听器添加到Corona APK中的图像_Lua_Apk_Coronasdk - Fatal编程技术网

Lua 将侦听器添加到Corona APK中的图像

Lua 将侦听器添加到Corona APK中的图像,lua,apk,coronasdk,Lua,Apk,Coronasdk,我在CoronaLabs Studio中创建了一个新项目,其中包含了基于物理的游戏开始选项,以及作为初学者加载的所有内容。我不知道应该将addEventListener()放在哪里,这样我就可以使该框可点击或点击后被销毁? 我尝试了一系列不同的方法,将这行代码放在脚本中,以使框可单击virus:applylinearpulse(0,-0.25,virus.x,virus.y) 下面是level1.lua脚本 在这种情况下,我有 require("toast") local composer =

我在CoronaLabs Studio中创建了一个新项目,其中包含了
基于物理的游戏
开始选项,以及作为初学者加载的所有内容。我不知道应该将
addEventListener()
放在哪里,这样我就可以使该框可点击或点击后被销毁? 我尝试了一系列不同的方法,将这行代码放在脚本中,以使框可单击
virus:applylinearpulse(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

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

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,那么
event
将分为三个阶段。因此要小心