【问题标题】:Add Listener to an Image in Corona APK在 Corona APK 中向图像添加侦听器
【发布时间】:2020-06-30 10:37:34
【问题描述】:

我在 CoronaLabs Studio 中创建了一个带有Physics-Based Game 启动选项的新项目,并且作为初学者加载了所有内容点击? 我尝试了很多不同的方法来将这行代码放在脚本中以使框可点击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 coronasdk


    【解决方案1】:

    如果我正确理解了您的问题,这里有一个示例,您可以单击一个框并将其销毁。

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

    【讨论】:

      猜你喜欢
      • 2014-03-18
      • 2014-02-08
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2017-04-06
      • 1970-01-01
      相关资源
      最近更新 更多