quick cocos collion

local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

function MainScene:ctor()

    --add Plist
    display.addSpriteFrames("bandit.plist","bandit.png")
    display.addSpriteFrames("gun/guns.plist","gun/guns.png")
    display.addSpriteFrames("fx/fx.plist","fx/fx.png")
    display.addSpriteFrames("projectiles/projectiles.plist","projectiles/projectiles.png")
    display.addSpriteFrames("pickups/pickups.plist","pickups/pickups.png")

    local hero=display.newSprite("#player_idle1.png",display.cx,display.cy):addTo(self)
    local heroPosX,heroPosY=hero:getPosition()
    hero.CollionRect={x=-15,y=-28,width=30,height=38}
    local heroBox={
        x=heroPosX+hero.CollionRect.x,
        y=heroPosY+hero.CollionRect.y,
        width=hero.CollionRect.width,
        height=hero.CollionRect.height
    }   
    
    display.newSprite("res/test/mark.png",heroBox.x,heroBox.y):addTo(self)
    display.newSprite("res/test/mark.png",heroBox.x,heroBox.y+heroBox.height):addTo(self)
    display.newSprite("res/test/mark.png",heroBox.x+heroBox.width,heroBox.y):addTo(self)
    display.newSprite("res/test/mark.png",heroBox.x+heroBox.width,heroBox.y+heroBox.height):addTo(self)
    
 
    local coin=display.newSprite("#silver_coin1.png",display.cx-23,display.cy+20):addTo(self)
    local coinPosX,coinPosY=coin:getPosition()
    coin.CollionRect={x=-8,y=-12,width=16,height=15}
    local coinBox={
          x=coinPosX+coin.CollionRect.x,
          y=coinPosY+coin.CollionRect.y,
          width=coin.CollionRect.width,
          height=coin.CollionRect.height
    }   
    

    display.newSprite("res/test/mark.png",coinBox.x,coinBox.y):addTo(self)
    display.newSprite("res/test/mark.png",coinBox.x,coinBox.y+coinBox.height):addTo(self)
    display.newSprite("res/test/mark.png",coinBox.x+coinBox.width,coinBox.y):addTo(self)
    display.newSprite("res/test/mark.png",coinBox.x+coinBox.width,coinBox.y+coinBox.height):addTo(self)

 
    print(cc.rectIntersectsRect(heroBox,coinBox))

end

return MainScene
原文地址:https://www.cnblogs.com/yufenghou/p/4323354.html