2015-09-25 108 views
1

我正在学习电晕,我正在尝试使用碰撞事件。 以下程序的目标是摇滚正在使用transition.to转向汽车。并打印一行以报告碰撞发生的位置。电晕+碰撞不起作用

但是,它不起作用。而且,我甚至不能收到“enterenter”的消息,程序甚至无法进入输入函数的原因是什么?

我的代码如下。先谢谢你。

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 

回答

0

工作代码与本地和全球(运行)听众:

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 

,你有你的代码的问题是这两个机构是静态的和两个静态物体之间不会发生冲突彼此,并没有什么在作曲家称为scene:enter(所以这就是为什么它不被称为,因此不是onLocalListener)。

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

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

这里是关于作曲家API的一些信息,你会,你应该使用scene:showhttps://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/