SpriteKit / Obj-C -Restarting游戏接触开始于nodesatPoint

时间:2023-01-23 14:46:28

I ma having trouble restarting my game with my SKSpriteNode playAgainButton with a name property of "play". The game over method is working fine, but I cannot restart the game in the touches began function. Here are the respective functions. Any help would be great, I have been stuck for a while on this problem and have tried several different methods in the doc including - (CGPoint)convertPoint:(CGPoint)point fromNode:(SKNode *)node; and - (NSArray *)nodesAtPoint:(CGPoint)p;

我无法使用名称属性为“play”的SKSpriteNode playAgainButton重新启动游戏。游戏结束方法工作正常,但我无法在触摸开始功能重启游戏。以下是各自的功能。任何帮助都会很棒,我已经在这个问题上停留了一段时间,并在doc中尝试了几种不同的方法,包括 - (CGPoint)convertPoint:(CGPoint)指向fromNode:(SKNode *)节点;和 - (NSArray *)nodesAtPoint:(CGPoint)p;

Here is the code I have in the touches began function:

这是我在触摸开始功能中的代码:

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

//    Jumping fucntionality

    [mainChar.physicsBody applyForce:CGVectorMake(0, 75000)];

//    Play Again Button functionality

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self.view];
    SKNode *node;
    NSArray *nodes = [node nodesAtPoint:location];

    for (node in nodes) {

    if ([node.name isEqual: @"play"]){
        score = 0;
        [node removeFromParent];
        [self.yourScoreLabel removeFromParent];
        [self.finalScoreLabel removeFromParent];
        self.scene.view.paused = NO;
        scoreLabel.text = [NSString stringWithFormat:@"Score %ld", (long)score];
        [self startTimers];
    }
    }
}

And the gameOver function :

而gameOver功能:

-(void)gameOver{

    self.scene.view.paused = YES;

    [planetTimer invalidate];
    [asteroidTimer invalidate];

    self.yourScoreLabel = [SKLabelNode new];
    self.yourScoreLabel.position = CGPointMake(0, 200);
    self.yourScoreLabel.fontSize = 100;
    self.yourScoreLabel.zPosition = 1;
    self.yourScoreLabel.text = @"Your score:";
    [self addChild:self.yourScoreLabel];

    self.finalScoreLabel = [SKLabelNode new];
    self.finalScoreLabel.position = CGPointMake(0, 0);
    self.finalScoreLabel.fontSize = 200;
    self.finalScoreLabel.zPosition =1;
    self.finalScoreLabel.text = [NSString stringWithFormat:@"%ld", (long)score];
    [self addChild:self.finalScoreLabel];

    SKSpriteNode * playAgainButton = [SKSpriteNode spriteNodeWithImageNamed:@"play"];
    playAgainButton.position = CGPointMake(0, -200);
    playAgainButton.name = @"play";
    playAgainButton.zPosition = 1;
    [self addChild:playAgainButton];
}

Thank you.

谢谢。

1 个解决方案

#1


1  

You are using [touch locationInView:view] which will return the location in the view's coordinate system. In SpriteKit, you need to work off of the scenes's coordinate system, which may be at a different interval. To get the location in the scene, simply do CGPoint location = [touch locationInNode:self];

您正在使用[touch locationInView:view],它将返回视图坐标系中的位置。在SpriteKit中,您需要处理场景的坐标系,它可能处于不同的间隔。要获取场景中的位置,只需执行CGPoint location = [touch locationInNode:self];

#1


1  

You are using [touch locationInView:view] which will return the location in the view's coordinate system. In SpriteKit, you need to work off of the scenes's coordinate system, which may be at a different interval. To get the location in the scene, simply do CGPoint location = [touch locationInNode:self];

您正在使用[touch locationInView:view],它将返回视图坐标系中的位置。在SpriteKit中,您需要处理场景的坐标系,它可能处于不同的间隔。要获取场景中的位置,只需执行CGPoint location = [touch locationInNode:self];