ios - Action not working correctly in SpriteKit -
i'm new ios programing , i'm experimenting learn trying create game in swift using sprite kit.
what i'm trying achieve having constant flow of blocks being created , moving rightwards on screen.
i start creating set contains initial blocks, action "constant movement" added each one, makes them move right. i'm having trouble adding new blocks screen.
the last column of blocks has "islast" boolean set true, when passes threshold supposed switch false , add new column of blocks set have "islast" set true.
each block in set has "constantmovement" action added makes them move right, new blocks have added well, don't work original ones.
not of move, tho if print "hasactions()" says do, , ones move stop doing when middle of screen. have no idea why happens, can experienced give me hint please?
this update function:
override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ let constantmovement = skaction.movebyx(-1, y: 0, duration: 10); background.runaction(skaction.repeatactionforever(constantmovement)); let removeblock = skaction.removefromparent(); let frame = self.frame; var currentblocksprite:skspritenode; var newblock: block; block in blocks { currentblocksprite = block.sprite!; currentblocksprite.runaction(constantmovement); if(block.column == numcolumns - 1) { block.islast = true; } if(block.isnew) { println("position \(currentblocksprite.position.x) has actions \(currentblocksprite.hasactions())"); } if(block.islast && currentblocksprite.position.x < frame.maxx - 50) { println("the block hits " + block.description); println("hits @ \(currentblocksprite.position.x)"); block.islast = false; row in 0..<numrows { newblock = block(column: numcolumns - 1, row: row, blocktype: blocktype.random(), islast: true, isnew: true); blocks.addelement(newblock); addblocksprite(newblock); println("new block: " + newblock.description + "position \(newblock.sprite?.position.x)"); } } if(currentblocksprite.position.x < frame.minx) { currentblocksprite.runaction(removeblock); blocks.removeelement(block); } } }
my whole project in here: https://github.com/thanniab/jumpinggame/tree/master/experimenting keep in mind since i'm new might full of cringeworthy bad practices.
i remove skaction code update function that's kind of bad idea. instead apply skaction when add block sprite scene, this.
func addblocksprite(block: block) { let blocksprite = skspritenode(imagenamed: "block"); blocksprite.position = pointforcolumn(block.column, row:block.row); if(block.blocktype != blocktype.empty) { addchild(blocksprite); let constantmovement = skaction.movebyx(-10, y: 0, duration: 1) var currentblocksprite:skspritenode let checkposition = skaction.runblock({ () -> void in if(blocksprite.position.x < -512){ blocksprite.removeallactions() blocksprite.removefromparent() } }) let movementsequence = skaction.sequence([constantmovement, checkposition]) let constantlycheckposition = skaction.repeatactionforever(movementsequence) blocksprite.runaction(constantlycheckposition) } block.sprite = blocksprite; }
that allow add new block whenever see fit , have appropriate action when it's added.
i've used 512 thats size of iphone 5 screen swap out screen size or better variable dynamically reflects screen size.
Comments
Post a Comment