swift - Method of creating a user created line with physics and strong performance -
i building infinite side scrolling game need draw line in ball can move upon. original system of achieving line spawning in skspritenode touch moved. unsuccessful due performance. tried using cgpath , skshapenode, preformance of extremely poor , out of ideas. basically, need path created player swipes or draws , need physics body surround stroke of line drawn. important remove stroke or points of path not in view of camera anymore save performance. illustration of goal below.
here example of how working efficiently. can work on how on , off of scene more efficiency.
the basic premises use 1 skshapenode
draw continuous line. once finished drawing line, convert on texture used skspritenode
// // gamescene.swift // physics // // created anthony randazzo on 7/28/17. // copyright © 2017 sychogaming. rights reserved. // import spritekit import gameplaykit class gamescene: skscene { private var shape = skshapenode() private var path : cgmutablepath! override func touchesbegan(_ touches: set<uitouch>, event: uievent?) { let position = touches.first!.positiononscene path = cgmutablepath() path.move(to: cgpoint.zero) shape.path = path shape.position = position self.addchild(shape) } override func touchesmoved(_ touches: set<uitouch>, event: uievent?) { let position = touches.first!.positiononscene - shape.position path.addline(to: position) shape.path = path } override func touchesended(_ touches: set<uitouch>, event: uievent?) { let position = touches.first!.positiononscene - shape.position path.closesubpath() shape.removefromparent() autoreleasepool { let sprite = skspritenode(texture: self.view?.texture(from: shape,crop:shape.frame)) sprite.position = cgpoint(x:shape.frame.midx,y:shape.frame.midy) let physicsbody = skphysicsbody(polygonfrom: path) physicsbody.isdynamic = false sprite.physicsbody = physicsbody addchild(sprite) } } override func touchescancelled(_ touches: set<uitouch>, event: uievent?) { let position = touches.first!.positiononscene } override func update(_ currenttime: timeinterval) { } } extension uitouch { var positiononscene : cgpoint { return self.location(in: (self.view as! skview).scene!) } } extension cgpoint { static public func -(lhs:cgpoint,rhs:cgpoint) -> cgpoint { return cgpoint(x: lhs.x - rhs.x, y: lhs.y - rhs.y) } static public func -= (lhs:inout cgpoint,rhs:cgpoint) { lhs = lhs - rhs } }
Comments
Post a Comment