ios - Dragging and flicking/throwing skspritenode -


this question has answer here:

i have ball sprite want able flick towards other sprites. right now, have function touchesmoved below, moves sprite towards touch movement on screen, when want user first touch sprite , drag finger towards target, causing sprite move. dysfunctional function.. appreciated!

edit: wanted ball velocity remain constant no matter flick length.. not addressed other answers.

    override func touchesmoved(touches: set<nsobject>, withevent event: uievent) {      if firsttimerstarted == false {         let touch = touches.first as! uitouch         let touchlocation = touch.locationinnode(self)         scenetouched(touchlocation)      } 

this should work, dug out old project.

cgfloat dt changing speed/power of movement.

var touching = false  override func touchesbegan(touches: set<nsobject>, withevent event: uievent) {      let touch = touches.first as! uitouch     let location = touch.locationinnode(self)     if sprite.frame.contains(location) {         touchpoint = location         touching = true     } } override func touchesmoved(touches: set<nsobject>, withevent event: uievent) {      let touch = touches.first as! uitouch     let location = touch.locationinnode(self)     touchpoint = location } override func touchesended(touches: set<nsobject>, withevent event: uievent) {     touching = false } override func update(currenttime: cftimeinterval) {     if touching {         if touchpoint != sprite.position         {             let dt:cgfloat = 0.15             let distance = cgvector(dx: touchpoint.x-sprite.position.x, dy: touchpoint.y-sprite.position.y)             let vel = cgvector(dx: distance.dx/dt, dy: distance.dy/dt)             sprite.physicsbody!.velocity = vel         }     } } 

edit: reason gets stronger farther distance, because vector distance between the sprite , touch point. try popping in update function. should work...

override func update(currenttime: cftimeinterval) {     if touching {         if touchpoint != sprite.position         {             let pointa = touchpoint             let pointb = sprite.position             let pointc = cgpointmake(sprite.position.x + 2, sprite.position.y)             let angle_ab = atan2(pointa.y - pointb.y, pointa.x - pointb.x)             let angle_cb = atan2(pointc.y - pointb.y, pointc.x - pointb.x)             let angle_abc = angle_ab - angle_cb             let vectorx = cos(angle_abc)             let vectory = sin(angle_abc)             let dt:cgfloat = 15             let vel = cgvector(dx: vectorx * dt, dy: vectory * dt)             sprite.physicsbody!.velocity = vel         }     } } 

with touchpoint (pointa), , sprite's position (pointb) can create angle.

atan2, famous function c, creates angle between 2 points. but, it's 0 degrees in different location usual.

so, need our own 0 degrees marker, use mid-right of point marker. it's common 0 degree placement:

http://www.cs.ucsb.edu/~pconrad/images/math/unitcirclewithdegrees.png

since it's right of sprite's position, create point right of sprite (pointc).

we use atan2 find angle.

to create vector angle, use cos , sin x , y values.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -