如何在Sprite Kit中使用手指移动物体,目标C

我试图做一个游戏,我有一些SKSpriteNodes和用户可以用手指移动他们,我正在使用苹果的新雪碧套件。

要做到这一点,我尝试了一个技巧 – 放置一个精灵 – 手指是“X”(SKSpriteNode),当用户移动手指 – 改变这个X精灵的位置,

问题是只有当它不在运动时,才会碰到其他精灵,我希望其他精灵能够响应手指的当前速度 – 手指运动越快 – 碰撞应该越强。

你能帮我吗?

  • 虽然我觉得这个技巧不是正确的方法,但我也是在发布代码。

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if([touches count]==1) { self.X= [[SKSpriteNode alloc]initWithImageNamed:@"X"]; self.X.name= @"X"; UITouch *t= touches.allObjects[0]; self.X.position= [t locationInNode:self.GameNode]; self.X.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20]; self.X.physicsBody.dynamic=YES; // Tried NO too... self.X.zPosition=1; [self.GameNode addChild:self.X]; } } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *t = touches.allObjects[0]; self.X.position = [t locationInNode:self.GameNode]; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self.X removeFromParent]; } 

经过search和做了很多实验,我终于有了一个答案!

解决scheme的第一步是由AyatollahAndy提供的 – 寻找影响的重点:

 SKNode *node = [self nodeAtPoint:location]; 

这条线获得我们在指定的位置点击的节点,如果它返回零,我们什么也没有打。

其次,我们要“打”对象,因此我们需要ApplyImpulse:

 [node.physicsBody applyImpulse:CGVectorMake(thrustV.x, thrustV.y) atPoint:location]; 

在这里,你可以看到我用了一些我在冲击点创build的vector的冲动,就是这样 – 很简单,我希望这能帮助别人。

分享是关怀,感谢您花时间阅读我的文章。

你可以做这样的事情。

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { touchOn=YES; UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self.parent]; touchPos =location; [self.physicsBody setVelocity:CGVectorMake(0, 0)]; } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { touchOn=YES; UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self.parent]; touchPos =location; } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { touchOn=NO; } -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { touchOn=NO; } -(void)update:(NSTimeInterval)dt { if (touchOn) { CGVector vector = CGVectorMake((touchPos.x-self.position.x)/dt, (touchPos.y-self.position.y)/dt); self.physicsBody.velocity=vector; } } 

创build一个节点子类并创build一个名为update的方法,以便随着时间的变化从场景中接收更新。 还要添加触摸方法并跟踪当前的触摸位置。

没有时间去实际运行这个,但是如何处理这样的事情:不要打扰像上面描述的那样放置一个节点X,而是:

触摸开始,跟踪触摸的时间和位置

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; //store location of first touch self.firstTouch = location; //get time self.startTouch = [NSDate date]; } 

在touchesMoved中,检查是否触摸了任何节点。如果是,根据触摸的开始和结束位置计算出angular度和速度,然后将速度应用到受影响的节点。

 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; SKNode *node = [self nodeAtPoint:location]; //if you've touched one of your nodes if ([node.name isEqualToString:@"whateverNode"]) { //work out time between touches NSTimeInterval timeBetween = [[NSDate date] timeIntervalSinceDate:self.startTouch]; //work out angle between touches (if you want to send the impacted node in that direction) float angle = atan2f (location.y - self.firstTouch.y, location.x - self.firstTouch.y) ; //apply to node CGFloat thrust = 0.01/timeBetween; CGPoint thrustVector = CGPointMake(thrust*cosf(angle), thrust*sinf(angle)); [node.physicsBody applyTorque:thrustVector]; } } 

注意最后的物理位可能(完全)不正确。