2013-03-17 40 views
2

我正在研究这个联盟过滤器代码已经超过一个星期了,现在无法实现它。 发生碰撞时它的每一次打印全部4个碰撞过滤器,如:碰撞过滤器不像预期的那样运行

你好,我是CollisionFilter1,
你好,我是CollisionFilter2,
你好,我是CollisionFilter3,
你好,我是墙,

我不明白如何使这个工作,所以任何帮助是值得欢迎的。

代码:

badc1CollisionFilter = { categoryBits = 1, maskBits = 16 } 
badc2CollisionFilter = { categoryBits = 2, maskBits = 16 } 
badc3CollisionFilter = { categoryBits = 4, maskBits = 16 } 
wallCollisionFilter = { categoryBits = 8, maskBits = 16 } 
playerCollisionFilter = { categoryBits = 16, maskBits = 15 } 

function getRandomStar() 
    local temp = starTable[math.random(1, #starTable)] 
    local randomStar = display.newImage(temp.imgpath) 
    if (temp.imgpath == "BCloud1.png") then 
    physics.addBody(randomStar, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc1CollisionFilter }) 
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png"; 
    end 

    if (temp.imgpath == "BCloud2.png") then 
    physics.addBody(randomStar, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc2CollisionFilter }) 
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png"; 
    end 

    if (temp.imgpath == "BCloud3.png") then 
    physics.addBody(randomStar, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc3CollisionFilter }) 
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png"; 
    end   
    randomStar.myName = "star" -- Set the name of the object to star 
    randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move 
    randomStar.x = math.random(-30, _W);  
    randomStar.y = -35; 
    randomStar.rotation = math.random(0,20) -- Rotate the object 

    starMove = transition.to(randomStar, { 
     time=randomStar.movementSpeed, 
     y=500, 
     onComplete = function(self) self.parent:remove(self); self = nil; 
     end 
     }) -- Move the star 
end 

    ceiling = display.newImage("invisibleTile.png") 
    ceiling:setReferencePoint(display.BottomLeftReferencePoint) 
    ceiling.x = 0 
    ceiling.y = 0 
    physics.addBody(ceiling, "static", {density=.1, bounce=0.1, friction=.2, filter=wallCollisionFilter }) 

    screenGroup:insert(ceiling) 

    theFloor = display.newImage("invisibleTile.png") 
    theFloor:setReferencePoint(display.BottomLeftReferencePoint) 
    theFloor.x = 0 
    theFloor.y = 510 
    physics.addBody(theFloor, "static", {density=.1, bounce=0.1, friction=.2, filter=wallCollisionFilter })   
    screenGroup:insert(theFloor) 

    playerSpriteSheet = sprite.newSpriteSheet("player.png", 113, 55) 
    playerSprites = sprite.newSpriteSet(playerSpriteSheet, 1, 4) 
    sprite.add(playerSprites, "players", 1, 4, 1000, 0) 
    player = sprite.newSprite(playerSprites) 
    player.x = -80 
    player.y = 350 
    player:prepare("players") 
    player:play() 
    player.collided = false 
    player:setReferencePoint(display.CenterReferencePoint);  
    physics.addBody(player, "static", {density=.1, bounce=0.1, friction=.2, radius=10, filter=playerCollisionFilter })  
    player.gravityScale = 0 
    screenGroup:insert(player) 
    playerIntro = transition.to(player,{time=2000, x=150, onComplete=playerReady}) 

local badc1collision = function(event) 
if event.phase == 'began' then 
    print("Hello i am CollisionFilter1") 
end 
end 

local badc2collision = function(event) 
if event.phase == 'began' then 
    print("Hello i am CollisionFilter2") 
end 
end 

local badc3collision = function(event) 
if event.phase == 'began' then 
    print("Hello i am CollisionFilter3") 
end 
end 

local wallcollision = function(event) 
if event.phase == 'began' then 
    print("Hello i am the wall") 
end 
end 

    Runtime:addEventListener("collision", badc1collision) 
    Runtime:addEventListener("collision", badc2collision) 
    Runtime:addEventListener("collision", badc3collision) 
    Runtime:addEventListener("collision", wallcollision)  

更新代码后:

我仍然得到这个错误?

enter image description here

祺凯文


OKY所以从你的意见我已经做到了这一点:

badc1CollisionFilter = { categoryBits = 1, maskBits = 16 } 
badc2CollisionFilter = { categoryBits = 2, maskBits = 16 } 
badc3CollisionFilter = { categoryBits = 4, maskBits = 16 } 
wallCollisionFilter = { categoryBits = 8, maskBits = 16 } 
playerCollisionFilter = { categoryBits = 16, maskBits = 15 } 

function getRandomStar() 
    local temp = starTable[math.random(1, #starTable)] -- Get a random star from starTable 
    local randomStar = display.newImage(temp.imgpath) -- Set image path for object 

    if (temp.imgpath == "BCloud1.png") then 
    physics.addBody(randomStar, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc1CollisionFilter }) 
    randomStar.myName = "star1" 
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png"; 
    end 

    if (temp.imgpath == "BCloud2.png") then 
    physics.addBody(randomStar, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc2CollisionFilter }) 
    randomStar.myName = "star2" 
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png"; 
    end 

    if (temp.imgpath == "BCloud3.png") then 
    physics.addBody(randomStar, { density=.1, bounce=0.1, friction=.2, radius=45, filter=badc3CollisionFilter }) 
    randomStar.myName = "star3" 
    temp.imgpath = "BCloud"..tostring(math.random(1, 3))..".png"; 
    end 

    randomStar.myName = "star" -- Set the name of the object to star 
    randomStar.movementSpeed = temp.movementSpeed; -- Set how fast the object will move 
    randomStar.x = math.random(-30, _W);  
    randomStar.y = -35; 
    randomStar.rotation = math.random(0,20) -- Rotate the object 

    starMove = transition.to(randomStar, { 
     time=randomStar.movementSpeed, 
     y=500, 
     onComplete = function(self) self.parent:remove(self); self = nil; 
     end 
     }) -- Move the star 
end--END getRandomStar() 


    playerSpriteSheet = sprite.newSpriteSheet("player.png", 113, 55) 
    playerSprites = sprite.newSpriteSet(playerSpriteSheet, 1, 4) 
    sprite.add(playerSprites, "players", 1, 4, 1000, 0) 
    player = sprite.newSprite(playerSprites) 
    player.x = -80 
    player.y = 350 
    player:prepare("players") 
    player:play() 
    player.collided = false 
    player:setReferencePoint(display.CenterReferencePoint); 
    physics.addBody(player, "static", {density=.1, bounce=0.1, friction=.2, radius=10, filter=playerCollisionFilter })  
    player.gravityScale = 0  
    screenGroup:insert(player) 
    playerIntro = transition.to(player,{time=2000, x=150, onComplete=playerReady}) 


function newCollisionHandler(self, event) 
    if event.other.myName == "star1" then 
     print("Collided with star1") 
    elseif event.other.myName == "star2" then 
     print("Collided with star2") 
    elseif event.other.myName == "star3" then 
     print("Collided with star3") 
    end 
end 


function scene:enterScene(event) 
player.collision = newCollisionHandler 
player:addEventListener("collision", player) 
end 

终端打印: '噢,你好,我有一个碰撞',但不'与star1/2/3相撞'?

-kevin

+1

'badc1CollisionFilter'和'badc1collision()'之间没有关系。您只需为同一对象的同一事件设置4个事件处理程序。当然,他们都是同时被解雇的。 – 2013-03-17 20:51:03

+0

我如何建立链接? – 2013-03-17 22:45:55

回答

2

你可以尝试一个主要冲突功能添加到播放器对象:

local newCollisionHandler  
player:addEventListener("collision", newCollisionHandler) 

function newCollisionHandler(self, event) 
-- self object is player object 
-- event.other object is the other object in collision 

-- by giving id's or object type to all objects you can find collision type 
    if event.other.myName == "star1" then 
     print("Collided with star1") 
    elseif event.other.myName == "star2" then 
     print("Collided with star2") 
    elseif event.other.myName == "star3" then 
     print("Collided with star3") 
    elseif event.other.myName == "star4" then 
     print("Collided with star4") 
    elseif event.other.myName == "star5" then 
     print("Collided with star5") 
    end 
end 

我只是做了对象MYNAME值。你当然应该修改它。但我想解释的是,所有添加到物理的对象都应该具有myName或myType属性。所以从这个属性,你可以理解,什么是与玩家对象相撞。

+0

嗨dogancan这个我扔了一个错误, – 2013-04-18 18:19:34

相关问题