【问题标题】:corona + collision not working电晕+碰撞不起作用
【发布时间】:2015-12-23 12:15:52
【问题描述】:

我正在学习 Corona,我正在尝试使用碰撞事件。 以下程序的目标是使用 transition.to 将岩石移向汽车。并打印一行报告发生碰撞的地方。

但是,它不起作用。而且我连enterenter都收不到,请问程序连enter都进不去是什么原因?

我的代码如下。提前谢谢你。

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


local physics = require "physics"
physics.start(); physics.pause()

local function onLocalCollision( self, event )

if ( event.phase == "began" ) then
    print( self.myName .. ": collision began with " .. event.other.myName )

elseif ( event.phase == "ended" ) then
    print( self.myName .. ": collision ended with " .. event.other.myName )
end
end


local widget = require "widget"

function scene:create( event )
print("entercreate")
local sceneGroup = self.view

local backgrd = display.newImage("background2.png",0,260)
backgrd:scale(3,3)
local car = display.newImage("car2.png",80,270)
physics.addBody(car,"static")
car.myName="Car"

local rock = display.newImage("rock.jpg",520,280)
rock:scale(0.05,0.05)
physics.addBody(rock,"static")
rock.myName="rock"


sceneGroup:insert(backgrd)
sceneGroup:insert(car)
sceneGroup:insert(rock)
transition.to(backgrd,{time=24000, x=-1800,onComplete=endScroll})
transition.to(rock,{time=4000, delay=2500,x=-40})
end

function scene:enter( event )
print("enterenter")
physics.start()
local sceneGroup = self.view
Runtime:addEventListener( "collision", onLocalCollision )
end



scene:addEventListener( "create", scene )
scene:addEventListener( "enter", scene )


return scene

【问题讨论】:

    标签: lua collision-detection coronasdk


    【解决方案1】:

    具有本地和全局(运行时)侦听器的工作代码:

    local widget = require "widget"
    local composer = require( "composer" )
    local scene = composer.newScene()
    
    local physics = require "physics"
    physics.start()
    physics.setGravity( 0,0 )
    
    local function onGlobalCollision( event )
            local target = event.object1
            local other = event.object2
    
            if ( event.phase == "began" ) then
                print( "GLOBAL: " .. target.name .. ": collision began with " .. other.name )
            elseif ( event.phase == "ended" ) then
                print( "GLOBAL: " .. target.name .. ": collision ended  with " .. other.name )      
            end
        end
    
    local function onLocalCollision( event )
        local target = event.target
        local other = event.other
    
        if ( event.phase == "began" ) then
            print( "LOCAL: " .. other.name .. ": collision began with " .. target.name )
        elseif ( event.phase == "ended" ) then
            print( "LOCAL: " .. other.name .. ": collision ended  with " .. target.name )       
        end
    end
    
    function scene:create( event )
        print("scene:create")
    
    
        local sceneGroup = self.view
    
        local car = display.newRect( 100, 100, 100, 100 )
        car.name = "car"
        physics.addBody( car )
    
        local rock = display.newRect( 250, 250, 100, 100 )
        rock.name = "rock"
        rock:setFillColor( 0.5, 0.5, 0.5 )
        physics.addBody( rock )
        rock:addEventListener( "collision", onLocalCollision )
    
        sceneGroup:insert(car)
        sceneGroup:insert(rock)
    
        transition.to(rock,{time=4000, x = -40, y = -100})
    
        Runtime:addEventListener( "collision", onGlobalCollision )
    end
    
    function scene:enter( event )
        print("scene:enter")    
        local sceneGroup = self.view
    
    end
    
    scene:addEventListener( "create", scene )
    scene:addEventListener( "enter", scene )
    
    
    return scene
    

    您的代码存在的问题是这两个主体是静态的,两个静态主体不能相互碰撞,并且在 composer 中没有任何名为 scene:enter 的东西(这就是它没有被调用的原因因此不是onLocalListener)。

    某些体型会(或不会)与其他体型发生碰撞。在 两个物理对象之间的碰撞,至少其中一个对象 必须是动态的,因为这是唯一与 任何其他类型。有关身体的详细信息,请参阅物理身体指南 类型。

    https://docs.coronalabs.com/daily/guide/physics/collisionDetection/index.html

    以下是有关 Composer API 的一些信息,您可能应该使用 scene:showhttps://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多