Objective c SpriteKit通过触摸节点执行操作

Objective c SpriteKit通过触摸节点执行操作,objective-c,sprite-kit,Objective C,Sprite Kit,如果我的魔术师被触碰了,我想进行一场恶作剧。 用我现在的密码,只要我一碰,我的精灵“石头”就会移动到巫师那里。 我可以用特定的x坐标和y坐标来确定触摸的位置,但由于“魔术师”正在移动,我不能这样做。 我怎样才能解决这个问题 - (void)Sorcerer { Sorcerer = [SKSpriteNode spriteNodeWithImageNamed:@"Sorcerer.png"]; Sorcerer.size = CGSizeMake(85, 85); Sorcerer.zPosi

如果我的魔术师被触碰了,我想进行一场恶作剧。 用我现在的密码,只要我一碰,我的精灵“石头”就会移动到巫师那里。 我可以用特定的x坐标和y坐标来确定触摸的位置,但由于“魔术师”正在移动,我不能这样做。 我怎样才能解决这个问题

- (void)Sorcerer {

Sorcerer = [SKSpriteNode spriteNodeWithImageNamed:@"Sorcerer.png"];
Sorcerer.size = CGSizeMake(85, 85);
Sorcerer.zPosition = 2;

Sorcerer.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
Sorcerer.physicsBody.dynamic = NO;
Sorcerer.physicsBody.allowsRotation = NO;
Sorcerer.physicsBody.usesPreciseCollisionDetection = YES;
Sorcerer.physicsBody.restitution = 0;

Sorcerer.position = CGPointMake(self.frame.size.width * 1.25, self.frame.size.height / 2.2);

SKAction * actionMove = [SKAction moveToX:self.frame.size.width / 2 duration:10];

[Sorcerer runAction:[SKAction repeatActionForever:[SKAction sequence:@[actionMove]]]];

[self addChild:Sorcerer];

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:Sorcerer];

Stone = [SKSpriteNode spriteNodeWithImageNamed:@"Stone.png"];

Stone.position = Human.position;
Stone.zPosition = 1;
Stone.scale = 0.6;

SKAction *action = [SKAction moveTo:Sorcerer.position duration:0.5];
SKAction *remove = [SKAction removeFromParent];

[Stone runAction:[SKAction sequence:@[action,remove]]];

[self addChild:Stone];

}

//给石头或巫师起个名字

通过此名称,您可以轻松访问该节点

-voidtouchesEnded:NSSet*与事件接触:UIEvent*事件{

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

// decide if the node is of interest or can be ignored, something like this:



if ([node.name isEqualToString:@“stone”]) {
      //apply action on stone

       }

}

在许多情况下,SKNode需要将某些事件通知其场景,或者想要强制执行只有场景才能提供的特定行为

当然,您可以在场景中实现touchesend:方法并迭代节点,但如果屏幕上有数千个呢?最干净的方法是使用委派或通知

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    [[NSNotificationCenter defaultCenter]
             postNotificationName:@"UserDidTapSourcererNotification" object:self];
}
代表团 我们将创建一个协议,并将场景设置为其代理。Sourcerer将在触摸委托的方法时调用该方法。让我们定义协议:

@class Sorcerer;
@protocol SorcererInteractionDelegate <NSObject>

- (void)sorcererTapped:(Sorcerer)sorcerer;

@end
最后,实现touchesend:方法

记住在Sourcerer类的init方法中将userInteractionEnabled设置为YES。 在场景中创建sourcerer时,设置代理:

Sourcerer *sourcerer = ...
sourcerer.delegate = self;
并实现魔法地图:方法:

- (void)sorcererTapped:(Sorcerer)sorcerer;
{
    // Do stuff with sorcerer.
}
通知 委派很酷,但如果您需要同时通知多个对象有关更改的信息,该怎么办。然后,您应该使用通知

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];

    [[NSNotificationCenter defaultCenter]
             postNotificationName:@"UserDidTapSourcererNotification" object:self];
}
您的场景需要注册为@UserDidTapSourcererNotification通知的观察者并进行处理

 [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(handleUserDidTapSourcererNotification:)
                                                 name:@"UserDidTapSourcererNotification";
                                               object:nil]; 

- (void)handleUserDidTapSourcererNotification:(NSNotification *)notification
{
    Sorcerer *sorcerer = (Sorcerer *)[notification object];

    // Do stuff with sorcerer.
}
希望能有帮助

 [[NSNotificationCenter defaultCenter] addObserver:self

                                             selector:@selector(handleUserDidTapSourcererNotification:)
                                                 name:@"UserDidTapSourcererNotification";
                                               object:nil]; 

- (void)handleUserDidTapSourcererNotification:(NSNotification *)notification
{
    Sorcerer *sorcerer = (Sorcerer *)[notification object];

    // Do stuff with sorcerer.
}