quick-cocos2d-x physics物理世界使用实例

--定义类
local MainScene = class("MainScene", function()
    return display.newPhysicsScene("MainScene")
    -- return display.newScene("MainScene")
end)

local GRAVITY         = -200   --重力
local COIN_MASS       = 100   --物体质量
local COIN_RADIUS     = 46     --半径
local COIN_FRICTION   = 0.8    --摩擦力
local COIN_ELASTICITY = 0.8   --弹性系数
local WALL_THICKNESS  = 64     --墙的厚度
local WALL_FRICTION   = 1.0   --墙的摩擦力
local WALL_ELASTICITY = 0.5    --墙的弹性系数

function MainScene:ctor()
    --创建触摸层,添加触摸监听
    self.layer = display.newLayer()
    self.layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
        return self:onTouch(event.name, event.x, event.y)
    end)
    self:addChild(self.layer)

    --只创建一个label
    cc.ui.UILabel.new({text = "TAP SCREEN", size = 32, color = display.COLOR_WHITE})
        :align(display.CENTER, display.cx, display.cy)
        :addTo(self)

    --创建物理世界
    self.world = self:getPhysicsWorld()
    self.world:setGravity(cc.p(0, GRAVITY))--设置x,y方向上的重力

    display.addSpriteFrames("AllSprites.plist","AllSprites.png")     --添加精灵帧
    --用图像帧创建精灵   墙
    local leftWallSprite = display.newSprite("#Wall.png")
    leftWallSprite:setScaleY(display.height / WALL_THICKNESS)
    leftWallSprite:setPosition(display.left + WALL_THICKNESS / 2, display.cy + WALL_THICKNESS)
    self:addChild(leftWallSprite)

    local rightWallSprite = display.newSprite("#Wall.png")
    rightWallSprite:setScaleY(display.height / WALL_THICKNESS)
    rightWallSprite:setPosition(display.right - WALL_THICKNESS / 2, display.cy + WALL_THICKNESS)
    self:addChild(rightWallSprite)

    local bottomWallSprite = display.newSprite("#Wall.png")
    bottomWallSprite:setScaleX(display.width / WALL_THICKNESS)
    bottomWallSprite:setPosition(display.cx, display.bottom + WALL_THICKNESS / 2)
    self:addChild(bottomWallSprite)

    --创建物理世界,定义物理世界的形状
    local wallBox = display.newNode()
    wallBox:setAnchorPoint(cc.p(0.5, 0.5))
    wallBox:setPhysicsBody(
        cc.PhysicsBody:createEdgeBox(cc.size(display.width - WALL_THICKNESS*2, display.height - WALL_THICKNESS)))
    wallBox:setPosition(display.cx, display.height/2 + WALL_THICKNESS/2)
    self:addChild(wallBox)

    -- add debug node
    -- self:getPhysicsWorld():setDebugDrawMask(
    --     true and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE)
end

--创建刚体
function MainScene:createCoin(x, y)
    --创建精灵
    local coinSprite = display.newSprite("#Coin.png")
    self:addChild(coinSprite)
    --创建刚体
    local coinBody = cc.PhysicsBody:createCircle(COIN_RADIUS,
        cc.PhysicsMaterial(COIN_RADIUS, COIN_FRICTION, COIN_ELASTICITY))
    coinBody:setMass(COIN_MASS)   --设置刚体的质量
    coinSprite:setPhysicsBody(coinBody)    --把刚体添加到精灵上
    coinSprite:setPosition(x, y)    --设置精灵的位置
end

--设置点击事件
function MainScene:onTouch(event, x, y)
    if event == "began" then
        self:createCoin(x, y)  --每次点击创建一个对象
    end
end

--
function MainScene:onEnter()
    self.layer:setTouchEnabled(true)
    --对cc.NODE_ENTER_FRAME_EVENT 帧事件  设置监听
    self:addNodeEventListener(cc.NODE_ENTER_FRAME_EVENT, handler(self, self.onEnterFrame))
    self:scheduleUpdate()
end

function MainScene:onEnterFrame(dt)
    -- body
end

return MainScene


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值