ios – Swift SpriteKit drawback with SKNodes in a grid configuration


I’m making a “easy” sport the place the person will “pop” blocks by tapping on “clusters” of blocks. When the person faucets it destroys the “cluster” of blocks, the opposite blocks transfer all the way down to fill within the empty spots on the board, and new ones are available in from the highest to fill the board again up. The primary time the person faucets, every little thing works completely. All blocks transfer all the way down to fill within the lacking spots and new ones come on display screen:
enter image description here

After the person faucets the second time, every little thing appears to get misplaced:

The container that the blocks are created in and added as youngsters is an SKShapeNode, and within the screenshot it’s the space contained in the “grey” line. The white packing containers to the left, proper and backside are my try and hold the blocks contained in the “degree” node from shifting misplaced. It clearly doesn’t assist.

Right here is my code for creating the preliminary set of “blocks” and including them to “degree”:

func addBlocks() {
    var row = 0
    var col = 0
    var id = 0
    for r in stride(from: 13, by way of: body.width, by: blockSize.width) {
        for c in stride(from: 13, by way of: body.top - blockSize.top * 4, by: blockSize.top) {
            let coloration = degree!.generateColors.chooseSet(colorSet: degree!.generateColors)
            let place = CGPoint(x: r, y: c)
            let block = BlockEntity(identify: "Field-(id)", 
                                    coloration: coloration, 
                                    blockSize: blockSize,
                                    place: place, 
                                    pointValue: 60, 
                                    column: col, 
                                    row: row)
            addChild(block)
            blocks.append(BlockData(id: id, 
                                    place: block.place, 
                                    block: block, 
                                    row: row, 
                                    col: col)
            )
            id += 1
            col += 1
        }    
        row += 1
    }
}

The code to fill within the empty spots when the blocks are popped is:

func fillInBlocks() {
    repeat {
        let missingBlocks = blocks.filter({ $0.block == nil })
        for lacking in missingBlocks {
            let col = lacking.col
            let column = blocks.filter({ $0.col == col })
            for lacking in column {
                if lacking.block == nil {
                    repeat {                         
                        if let blockExistsIndex = blocks.firstIndex(the place: { $0.col == lacking.col + 1 && $0.row == lacking.row && $0.block != nil }) {
                            if let missingBlockIndex = blocks.firstIndex(the place: { $0.id == lacking.id }) {
                                guard let existingBlock = blocks[blockExistsIndex].block else { return }
                                let transfer = SKAction.transfer(to: blocks[missingBlockIndex].place, length: 0.25)
                                existingBlock.run(transfer) 
                                blocks[missingBlockIndex].block = blocks[blockExistsIndex].block
                                blocks[blockExistsIndex].block = nil
                            }
                        }
                    } whereas blocks.first(the place: { $0.col == lacking.col + 1 && $0.row == lacking.row })?.block != nil
                    
                    if lacking.place.y.rounded() == 438.0 {
                        if let index = blocks.firstIndex(the place: { $0.id == lacking.id }) {
                            let coloration = degree!.generateColors.chooseSet(colorSet: degree!.generateColors)

                            var place = CGPoint(x: blocks[index].place.x, y: 475)
                            let block = BlockEntity(
                                identify: "Field-(blocks[index].id)", 
                                coloration: coloration, 
                                blockSize: blockSize,
                                place: place, 
                                pointValue: 60, 
                                column: blocks[index].col, 
                                row: blocks[index].row
                            )
                            
                            blocks[index].block = block
                            place = CGPoint(x: blocks[index].place.x, y: 440)
                            let transfer = SKAction.transfer(to: place, length: 0.25)
                            blocks[index].block?.run(transfer)
                            addChild(block)
                        }
                    }
                }
            }
        }
    } whereas blocks.filter({ $0.block == nil }).depend > 0
}

Does anybody have any ideas on how I can repair this so that it’ll reliably transfer the blocks with out inflicting the “wreck” seen when the person faucets the second time?

Additionally it needs to be famous that I used this text by Kodeco as inspiration to getting my blocks to align in a grid and get crammed in when blocks are eliminated:
Find out how to make a sport like sweet crush

Additionally notice, I’m not making a sport like Sweet Crush LOL.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles