该例使用物理引擎+重力加速器事件完成,代码如下:
-
-- Author: Your Name
-- Date: 2019-11-19 13:47:00
-- 星星玻璃瓶
local GlassBottle = class("glass_bottle")
local STAR_WH = 50;
local STAR_RADIUS = 25;
function GlassBottle:onCreate()
--设置物理场景
local physics_scene = self:create_physics_scene()
physics_scene:getPhysicsWorld():setGravity(cc.p(0, 0))
--设置物理边界
local edge_size = cc.size(1000, 100)
self.edge_layer = cc.LayerColor:create(cc.c4f(0,0,0,0))
--启用加速计事件 加速计:用于检测设备是否在运动。有x,y,z三个运动方向
self.edge_layer:setAccelerometerEnabled(true)
self.edge_layer:setContentSize(edge_size)
self:add_child_to_physics(self.edge_layer)
local csb_path = "scenes/common/glasstube/pic_practice_glasstube.csb"
local view = utils:load_csb(csb_path, self, {
})
self:addChildToPhysics(view)
view:setPosition(display.cx,display.cy)
local half_layer_width = self.edge_layer:getContentSize().width * 0.5
local half_layer_height = self.edge_layer:getContentSize().height * 0.5
self.edge_layer:setPosition(view:getPositionX() - half_layer_width, view:getPositionY() - half_layer_height)
--创建多边形刚体
local points = {
cc.p(STAR_RADIUS - half_layer_width, -half_layer_height),
cc.p(-half_layer_width, -half_layer_height + STAR_RADIUS),
cc.p(-half_layer_width, half_layer_height - STAR_RADIUS),
cc.p(STAR_RADIUS - half_layer_width, half_layer_height),
cc.p(half_layer_width - STAR_RADIUS, half_layer_height),
cc.p(half_layer_width, half_layer_height - STAR_RADIUS),
cc.p(half_layer_width, -half_layer_height + STAR_RADIUS),
cc.p(half_layer_width - STAR_RADIUS, -half_layer_height)
}
--cc.PhysicsMaterial类型,表示物理材质的属性,即密度设为1,弹性系数设为1,摩擦系数设为1.
local edge_body = cc.PhysicsBody:createEdgePolygon(points,cc.PhysicsMaterial(1,1,0))
self.edge_layer:setPhysicsBody(edge_body)
--注册加速度事件监听器,与其他事件监听器不同的是,它没有事件响应属性,事件响应是通过静态create指定的
local listener = cc.EventListenerAcceleration:create(handler(self, self.accelerometerListener))
self.edge_layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener, self.edge_layer)
self:create_star_with_count(20)
end
function GlassBottle:accelerometerListener(event,x,y,z,timestamp)
local vNowX = x * 9.81 * 240
local vNowY = y * 9.81 * 240
self.physic_scene:getPhysicsWorld():setGravity(cc.p(vNowX, vNowY))
end
function GlassBottle:create_physics_scene()
if self.physic_scene == nil then
--创建有物理空间的场景
local physic_scene = cc.Scene:createWithPhysics()
physic_scene:setRotation(0)
self:addChild(physic_scene)
physic_scene:setName("physic_scene")
--设置Debug模式,你会看到物体的表面被线条包围,主要为了在调试中更容易地观察
--physic_scene:getPhysicsWorld():setDebugDrawMask(debug and cc.PhysicsWorld.DEBUGDRAW_ALL or cc.PhysicsWorld.DEBUGDRAW_NONE)
self.physic_scene = physic_scene
self:begin_timer()
end
return self.physic_scene
end
function GlassBottle:add_child_to_physics( VIEW )
if self.physic_scene then
self.physic_scene:addChild(VIEW)
else
self:addChild(VIEW)
end
end
function GlassBottle:begin_timer( ... )
self.update_hander = self.update_hander or 0
if self.update_hander == 0 then
local scheduler = cc.Director:getInstance():getScheduler()
self.update_hander = scheduler:scheduleScriptFunc(handler(self,self.update),1/60.0,false)
end
end
function GlassBottle:update( dt )
if self.physic_scene and not tolua.isnull(self.physic_scene) and self.physic_scene.stepPhysicsAndNavigation then
self.physic_scene:stepPhysicsAndNavigation(dt)
end
end
function GlassBottle:stop_timer()
self.update_hander = self.update_hander or 0
if self.update_hander > 0 then
local scheduler = cc.Director:getInstance():getScheduler()
scheduler:unschedulerSciptEntry(self.update_hander)
self.update_hander = 0
end
end
function GlassBottle:dealloc()
self:stop_timer()
self.edge_layer:setAccelerometerEnabled(false)
end
function GlassBottle:create_star_with_count( count )
for i=1,count do
self:create_physic_star( i * 10)
end
end
function GlassBottle:create_physic_star( deltaX )
local star = cc.Sprite:create("pic_practice_glassstar.png")
if deltaX then
star:setPosition(self.edge_layer:getContentSize().width * 0.5 + deltaX, self.edge_layer:getContentSize().height - STAR_RADIUS - 5)
else
star:setPosition(self.edge_layer:getContentSize().width * 0.5, self.edge_layer:getContentSize().height - STAR_RADIUS - 5)
end
local points = {
cc.p(0, 25),
cc.p(24, 7.5),
cc.p(15, -20),
cc.p(-15, -20),
cc.p(-24, 7.5)
}
local star_body = cc.PhysicsBody:createPolygon(points, cc.PhysicsMaterial(1,0.2,0))
star:setPhysicsBody(star_body)
self.edge_layer:addChild(star)
end
return GlassBottle
网友评论