[英]Gideros game engine box2d bodies
I am going to make a simple Game. 我要做一个简单的游戏。 my code have many bodies in on timer function (every second for example):
我的代码在定时器功能上有很多机构(例如每秒):
local function onTimer()
local sx = 20
local sy = 20
body = world:createBody{type = b2.DYNAMIC_BODY, position = {x = i*48, y = 50}}
local shape = b2.PolygonShape.new()
-- box images are 70x70 pixels. we create bodies 1 pixel smaller than that.
shape:setAsBox(20, 20)
body:createFixture{shape = shape, density = 1, restitution = 0.1, friction = 0.3}
rand = math.random(1,32)
sprite = createBoxSprite(0.6,0.6,rand)
stage:addChild(sprite)
actors[body] = sprite
actors_r[sprite] = body
table.insert(sp, sprite)
--print (sprite)
--print (sp[#sp])
sprite:addEventListener(Event.TOUCHES_BEGIN, onTouchBegin,sprite)
sprite:addEventListener(Event.TOUCHES_END, onTouchEnd,sprite)
i=i+1
all=all+1
--print(all)
if i>8 then
i=1
end
if all>88 then
print("game over")
end
end
I want remove any body on click. 我要删除点击时的任何物体。 but with this listeners when clicked all of sprites removed only.
但是单击此侦听器后,仅删除了所有精灵。
function onTouchBegin(e)
e:removeFromParent()
end
function onTouchEnd(e)
end
how to do this? 这个怎么做?
It actually works like this: 它实际上是这样的:
-- receive sprite as first parameter and event as second
function onTouchBegin(sprite, e)
-- check if event hit this exact sprite
if sprite:hitTestPoint(e.touch.x, e.touch.y) then
-- remove it from the stage
sprite:removeFromParent()
-- also lets not forget about references in actors tables
local body = actors_r[sprite]
actors[body] = nil
actors_r[sprite] = nil
-- and we could either destroy the body here
world:destroyBody(body)
-- or reuse it to attach to the next sprite
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.