首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gideros游戏引擎box2d车身

Gideros游戏引擎box2d车身
EN

Stack Overflow用户
提问于 2014-01-27 10:14:15
回答 1查看 317关注 0票数 0

我要做一个简单的游戏。我的代码中有许多体在计时器函数中(例如,每秒钟):

代码语言:javascript
复制
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

我要移除任何点击的物体。但是当这个听众点击时,所有的精灵都被移除了。

代码语言:javascript
复制
function onTouchBegin(e)
    e:removeFromParent()
end

function onTouchEnd(e)

end

怎么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-30 17:27:42

它实际上是这样工作的:

代码语言:javascript
复制
-- 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21378131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档