for loop help和sprite addition

时间:2014-05-13 00:38:51

标签: lua corona

我有两个问题。我是什么 试图做的是每次我射击敌人/藤蔓它应该是 移除并发生爆炸。删除工作完美 但是没有调用精灵爆炸

  1. for循环#sections[sectInt]["vines"]中的内容是什么?    他们是父/子参考吗?有人可以打破这个    甚至告诉我#是什么?

  2. 每个葡萄藤被毁后如何使用我的爆炸精灵?    我很难搞清楚每个x和y的调用方式    在移除时,for循环中的藤蔓会爆炸。

  3. 代码:

    local sections = require("sectionData")
    
    local lastSection = 0
    function createSection()
        --Create a random number. If its eqaul to the last one, random it again.
        local sectInt = mR(1,#sections)
        if sectInt == lastSection then sectInt = mR(1,#sections) end
        lastSection = sectInt
    
        --Get a random section from the sectionData file and then
        --Loop through creating everything with the right properties.
        local i
        -- the random creation of vines throughout the screen
        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
    
    -- explosion sprite
    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)
    
    -- i want to reference the for loop position if each vine so it plays sprite when they are removed
    explode.x = "vine.x" ; explode.y = "vine .y"
    explode.name = "explode"
    explode.position=1
    extraGroup:insert(explode)
    

3 个答案:

答案 0 :(得分:0)

  

1)for循环#sections [sectInt] [" vines"]中的内容是什么?他们是父/子参考吗?有人可以将这一点告诉我甚至告诉我#是什么吗?

正如我在评论中所说,#table length

循环遍历" vine"所选分段数据中的数据(无论这意味着什么与游戏完全相同),然后为这些葡萄藤创建对象。

答案 1 :(得分:0)

当你的葡萄藤爆炸的时候,你会玩精灵。如果你想爆炸每一棵葡萄树,你会有类似的东西:

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

注意:未经测试,请告知我是否有任何问题无法解决。

答案 2 :(得分:0)

好吧,这就是我为了让任何有同样问题的人让我的对象爆炸而做的事情。

function isShot( event )
           print('shot!!!')
        if (event.other.class == "enemy") then
            if (event.other.name == "limitLine") then  
                event.target:doRemove() --remove bullet if hits the limitLine

            elseif (event.other.name == "vine") then    

                event.other:doRemove()  
                spriteExplode(event.other.x, event.other.y) -- this function calls itself & runs the sprite

                if event.other.name == "vine" then
                    if event.other.name == "explode" then
                        event.other:doRemove()  --this removes the explosion sprite
                    end
               end                  
            end 
            -- remove the bullet & explosion sprite
            timer.performWithDelay( 5000, function(event) explode:doRemove(); end, 1 )
            event.target:doRemove()
       end
    return true
end

function spriteExplode( x, y)

        explode.isVisible = true
        explode:play()
        print("play explode")

        if (x and y) then   -- this is the code that keeps my sprite updating upon removal of vine
            explode.x, explode.y = x, y
        end

        function explode:doRemove(event)
            timer.performWithDelay(
                        1,
                            function(event)
                                display.remove(self)
                                self= nil
                            end,
                    1 )
        end


end

我在Bullet函数中添加了isShot函数eventListener  bullet:addEventListener(" collision",isShot)&一个子弹:doRemove函数也在bullet函数内。 希望这有帮助。