我正在尝试制作一款游戏,其中我有一些SKSpriteNodes,用户可以用手指移动它们,我正在使用苹果的新Sprite Kit。
要做到这一点,我尝试了一个技巧 - 放置一个Sprite - 手指所在的“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];
}
答案 0 :(得分:8)
经过搜索和大量实验,我终于得到了答案!
解决方案的第一步由AyatollahAndy提供 - 找到影响点:
SKNode *node = [self nodeAtPoint:location];
此行获取我们在指定位置点击的节点,如果它返回nil,则我们没有点击任何内容。
其次 - 我们想要“击中”对象,因此我们需要ApplyImpulse:
[node.physicsBody applyImpulse:CGVectorMake(thrustV.x, thrustV.y) atPoint:location];
在这里你可以看到我用一些我在冲击点创建的向量来应用脉冲, 就是这样 - 非常直接,我希望这会有所帮助。
分享是关怀, 感谢您抽出宝贵时间阅读我的帖子。
答案 1 :(得分:0)
没有时间实际运行这个但是这样的事情:不要打扰如上所述的放置节点X,而不是:
在touchesBegan中,记录触摸的时间和位置
- (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中,检查是否触摸了任何节点,如果是,请根据触摸的开始和结束位置计算角度和速度,然后将速度应用于受影响的节点?
- (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];
}
}
注意最后的物理位可能(完全)不正确。
答案 2 :(得分:0)
你可以这样做。
-(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;
}
}
对节点进行子类化并创建一个名为update的方法,该方法接收来自场景的更新以及时间的变化。 还可以添加触摸方法并跟踪当前触摸位置。