ios - 如何检查一个 SKSpriteNode 是否被点击

标签 ios objective-c sprite-kit skspritenode

我一直在尝试制作一个简单的应用程序,当您点击一个圆圈时,它会消失,而新的圆圈会出现在其他地方。

这是我所有的代码,经过编辑,但仍然无法正常工作。

#import "GameScene.h"

@implementation GameScene

-(void)didMoveToView:(SKView *)view {



[self LabelShow];
}

int Count = 0;
int CountCheck = 0;

-(void) LabelShow {

//Nothing yet

}



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


while (CountCheck == Count) {

int FNum1 = 350;
int TNum1 = 650;
int newx = (arc4random()%(TNum1-FNum1))+FNum1;

NSLog(@"RandomXCoord: %i", newx);

int FNum2 = 100;
int TNum2 = 700;
int newy = (arc4random()%(TNum2-FNum2))+FNum2;
NSLog(@"RandomYCoord: %i", newy);

//Location to newx and newy
CGPoint location =  CGPointMake(newx, newy);


SKSpriteNode *Circle = [SKSpriteNode spriteNodeWithImageNamed:@"Circle.png"];

Circle.xScale = 0.2;
Circle.yScale = 0.2;
Circle.position = location;


//Shows chircle

[self addChild:Circle];
CountCheck++;




for (UITouch *touch in touches)
        {
            CGPoint locationTouch = [touch locationInNode:self];

            if ([Circle containsPoint:locationTouch]) {

                    NSLog(@"Success!");
                    [Circle runAction:[SKAction fadeOutWithDuration:0]];
                    Count++;
            }
        }

}
}
@end

正如我所说,我有代码以另一种方法将圆圈显示在屏幕上。但是每当我运行此代码(单击圆圈)时,底部的 if 语句都不会执行。

我尝试了这个线程中的所有东西: Can't tap SKSpriteNode - no touch detected ios ,但我无法让它继续工作,并且已将代码改回原来的代码。

谁能告诉我如何在点击 SKSpriteNode 时执行 if 语句?

感谢您阅读这个问题。

编辑我改了代码,还是不行

最佳答案

在您的 Circle.xScale = 0.2; 声明之上添加以下内容

Circle.name = @"Circle";

并用下面的代码替换你的for-loop

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:location];
    if ([node.name isEqualToString:@"Circle"]) {
        NSLog(@"Success!");
        [node removeFromParent];
        ++Count;
    }
}

关于ios - 如何检查一个 SKSpriteNode 是否被点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001963/

相关文章:

ios - dispatch_after 和 [UIView 动画 : duration] happen immediately (but shouldn't)

iOS - backgroundTimeRemaining 值未显示预期值

ios - 在 SpriteKit Hello World 中加载图像时出错

iphone - 如何在 iPhone 中以编程方式将图像旋转 90 度?

ios - 短路评估中评估条件的顺序是什么

swift - 检测物体靠近墙壁

ios - Opengl ES 着色器 - 获取属性

ios - 简单的Objective-C属性设置错误

swift - SpriteKit SKSequence 使 UI 按钮出现

swift - 使用 SpriteKit 在 iPhone 上出现屏幕尺寸问题