for循环帮助和精灵添加

我有两个问题。我想要做的是每次射击敌人/藤蔓都应该被移除,并且会发生爆炸。移除可以完美工作,但精灵没有被调用来爆炸。

1.循环中的 `#sectInt.int[“vines”]是什么? 它们是父/子引用吗?有人能详细解释一下这个符号吗?

2.如何在每次摧毁藤蔓后使用我的爆炸精灵? 我很难弄清楚如何在循环中调用每个 x 和 y 的藤蔓在删除时爆炸。

代码:

local sections = require("sectionData")

local lastSection = 0
function createSection()
    --创建一个随机数。如果它和上一个相等,就再次随机。
    local sectInt = mR(1,#sections)
    if sectInt == lastSection then sectInt = mR(1,#sections) end
    lastSection = sectInt

    --从sectionData文件中获取一个随机部分,然后
    --循环创建具有正确属性的所有内容。
    local i
    -- 屏幕上藤蔓的随机创建
    for i=1, #sections[sectInt]["vines"] do
        local object = sections[sectInt]["vines"][i]
        local vine = display.newImageRect(objectGroup, "images/vine"..object["type"]..".png", object["widthHeight"][1], object["widthHeight"][2])
        vine.x = object["position"][1]+(480*object["screen"]); vine.y = object["position"][2]; vine.name = "vine"

        local rad = (vine.width*0.5)-8; local height = (vine.height*0.5)-8
        local physicsShape = { -rad, -height, rad, -height, rad, height, -rad, height }
        physics.addBody( vine, "static", { isSensor = true, shape = physicsShape } )
    end
end

-- 爆炸精灵
options1 =
{
    width = 96, height = 96,
    numFrames = 16,
    sheetContentWidth = 480,
    sheetContentHeight = 384
}
playerSheet1 = graphics.newImageSheet( "images/explosion.png", options1)
playerSprite1 = {
    {name="explosion", start=1, count=16, time = 400, loopCount = 1 },
}

explode = display.newSprite(playerSheet1, playerSprite1)
explode.anchorX = 0.5
explode.anchorY = 1
--player:setReferencePoint(display.BottomCenterReferencePoint)

-- 我希望引用循环位置,如果他们都被删除,那么就会播放精灵
explode.x = "vine.x" ; explode.y = "vine .y"
explode.name = "explode"
explode.position=1
extraGroup:insert(explode)
点赞
用户258523
用户258523

对于循环中的 #sections[sectInt]["vines"] 这是什么意思?它们是父/子引用吗?有人能详细解释一下甚至告诉我 # 是什么意思吗?

正如我在评论中所说,表示[表长度](http://www.lua.org/manual/5.1/manual .html#2.5.5)

该循环遍历所选择的片段数据中“藤蔓”数据的每一位(在游戏方面确切地表示什么),然后创建那些藤蔓的对象。

2014-05-13 11:55:34
用户869951
用户869951

当该葡萄藤要爆炸时,您可以播放精灵动画。如果要爆炸所有葡萄藤,则可以使用以下代码:

sheetOptions =
{
    width = 96, height = 96,
    numFrames = 16,
    sheetContentWidth = 480,
    sheetContentHeight = 384
}
playerSheet = graphics.newImageSheet( "images/explosion.png", sheetOptions)
spriteSequence = {
    {name="explosion", start=1, count=16, time = 400, loopCount = 1 },
}

for i, vine in ipairs(objectGroup) do
    local explode = display.newSprite(playerSheet, spriteSequence)
    explode.anchorX = 0.5
    explode.anchorY = 1
    explode.x = vine.x
    explode.y = vine.y
    explode.name = "explode"
    explode.position=1
    explode:play()

    extraGroup:insert(explode)
end

注意:此代码未经测试,请让我知道任何您无法解决的问题。

2014-05-13 18:16:43
用户3298518
用户3298518

好的,以下是我为了解决任何人都遇到的爆炸问题而做的事情。

``` function isShot(event) print('shot!!!') if(event.other.class ==“enemy”)then 如果(event.other.name ==“limitLine”)then event.target:doRemove()-如果击中limitLine,请删除子弹

     else if(event.other.name ==“vine”)then

         event.other:doRemove()
         spriteExplode(event.other.x,event.other.y)-此函数调用自己并运行精灵

         if event.other.name ==“vine” then
             if event.other.name ==“explode” then
                 event.other:doRemove()-这将删除爆炸精灵
             end
         end
     end
     -删除子弹和爆炸精灵
     定时器.performWithDelay(5000functioneventexplode:doRemove();end,1)
     event.target:doRemove()
 end
 返回真实

结束

function spriteExplode(x,y)

爆炸。isVisible = true 爆炸:播放() print(“播放爆炸”)

    如果(x和y)则此代码使我的精灵在删除藤蔓后保持更新
        爆炸。x,爆炸。y = x,y
    结束

    function explode:doRemoveevent)
        定时器.performWithDelay (
                    1,
                            function(event)
                                显示删除(自己)
                                自我=否
                            结束,
                    1 )
    结束

结束

我将isShot函数事件侦听器添加到了Bullet函数中 子弹:addEventListener(“collision”,isShot),同时子弹函数中还有一个子弹:doRemove函数。 希望这可以帮助。

2014-06-19 20:37:24