Ios 在精灵上启用触摸(spritekit)

Ios 在精灵上启用触摸(spritekit),ios,objective-c,cocoa,sprite-kit,Ios,Objective C,Cocoa,Sprite Kit,在SpriteKit中,我对精灵的触摸有问题 这是我的密码 #define kRowCount 8 #define kColCount 6 #define kDotGridSpacing CGSizeMake (50,-50) #import "BBMyScene.h" @implementation BBMyScene @synthesize dot; @synthesize htoucharea; @synthesize vtoucharea; @synthesize hFrame; @

在SpriteKit中,我对精灵的触摸有问题

这是我的密码

#define kRowCount 8
#define kColCount 6
#define kDotGridSpacing CGSizeMake (50,-50)
#import "BBMyScene.h"

@implementation BBMyScene

@synthesize dot;
@synthesize htoucharea;
@synthesize vtoucharea;
@synthesize hFrame;
@synthesize vFrame;



-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    self.userInteractionEnabled = YES;

    //  Set up Background
    self.backgroundColor = [SKColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1]; /*#f4f4f4*/


    // Set up Lattice of Dots
    CGPoint baseOrigin = CGPointMake(35, 385);
    for (NSUInteger row = 0; row < kRowCount; ++row) {


        CGPoint dotPosition = CGPointMake(baseOrigin.x, row * (kDotGridSpacing.height) + baseOrigin.y);


        for (NSUInteger col = 0; col < kColCount; ++col) {

            dot = [SKSpriteNode spriteNodeWithImageNamed:@"dot"];
            dot.position = dotPosition;
            NSString *dotName = [NSString stringWithFormat:@"dot_%d_%d", row, col];
            dot.name = dotName;
            [self addChild:dot];
            dotPosition.x += kDotGridSpacing.width;



        }

    }


    //Set up horizontal touch areas
    for (NSUInteger row = 0; row < kRowCount; ++row) {

        CGPoint htouchareaPosition = CGPointMake(baseOrigin.x + 0.5*(kDotGridSpacing.width), row * (kDotGridSpacing.height) + baseOrigin.y);

        for (NSUInteger col = 0; col < kColCount-1; ++col) {

            htoucharea = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:0.18 green:0.702 blue:0.91 alpha:0.5] size:CGSizeMake(35,25)];
            htoucharea.position = htouchareaPosition;
            NSString *htouchareaName = [NSString stringWithFormat:@"htoucharea_%d_%d", row, col];
            htoucharea.name = htouchareaName;
            htoucharea.userInteractionEnabled = YES;

            htouchareaPosition.x += kDotGridSpacing.width;

            [self addChild:htoucharea];


        }

    }







    // Set up vertical touch areas
    for (NSUInteger row = 0; row < kRowCount-1; ++row) {

        CGPoint vtouchareaPosition = CGPointMake(baseOrigin.x, row * (kDotGridSpacing.height) + baseOrigin.y + 0.5*(kDotGridSpacing.height));

        for (NSUInteger col = 0; col < kColCount; ++col) {

            vtoucharea = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:1 green:0.478 blue:0.478 alpha:0.5] size:CGSizeMake(25,35)];
            vtoucharea.position = vtouchareaPosition;
            NSString *vtouchareaName = [NSString stringWithFormat:@"vtoucharea_%d_%d", row, col];
            vtoucharea.name = vtouchareaName;
            vtoucharea.userInteractionEnabled = YES;
            [self addChild:vtoucharea];
            vtouchareaPosition.x += kDotGridSpacing.width;


        }

    }








}

return self;
}





-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */



UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];

if (CGRectContainsPoint(vtoucharea.frame, location)) {
    NSLog(@"Hello");;
}
















}

-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

@end
#定义kRowCount 8
#定义kColCount 6
#定义kDotGridSpacing CGSizeMake(50,-50)
#导入“BBMyScene.h”
@实现BBMyScene
@合成点;
@区域综合;
@合成vtoucharea;
@合成hFrame;
@合成vFrame;
-(id)initWithSize:(CGSize)size{
if(self=[super initWithSize:size]){
/*在这里设置场景*/
self.userInteractionEnabled=是;
//设置背景
self.backgroundColor=[SKColor COLOR WITHRED:0.957绿色:0.957蓝色:0.957 alpha:1];/*#F4*/
//建立点阵
CGPoint baseOrigin=CGPointMake(35385);
对于(整数行=0;行

我有麻烦了。当我触摸其中一个精灵时,我想做点什么(此时只是登录到控制台)。为什么我的触摸没有被识别?

该函数仅在您试图在同一函数中执行的
userInteractionEnabled=YES
时才被调用,因此它永远不会被调用。将该代码放入您的sprite的
init
方法中。

我尝试了您的代码,触摸效果很好。但是,代码中还有一些其他问题:

  • 在init中的for循环中,仅将最后创建的节点设置为ivar(点、hTouchArea、vTouchArea)。稍后,在touchesedend-method中,您尝试使用这个ivar来检测触摸是否在这个节点的框架内。当然,这不起作用,因为您只缓存了最后一个节点。(我在这里使用了不同的方法-参见touchesbeent方法)

  • 如果要仅在场景节点中注册触摸,则必须禁用添加到其上的其他节点的用户交互。否则,其他节点将阻止场景获得接触。(使用userInteractionEnabled=NO)

  • [触摸位置查看:self.view]提供视图中的位置。相反,您希望必须在节点本身中指定位置。您必须改用[触摸位置Innode:self]

在这里,我为您修复了代码(我也测试了它,所以它应该可以工作):

#导入“测试场景”
#定义kRowCount 8
#定义kColCount 6
#定义kDotGridSpacing CGSizeMake(50,-50)
@实现测试场景
-(id)initWithSize:(CGSize)大小
{
if(self=[super initWithSize:size])
{
self.userInteractionEnabled=是;
self.backgroundColor=[SKColor COLOR WITHRED:0.957绿色:0.957蓝色:0.957阿尔法:1];
CGPoint baseOrigin=CGPointMake(35385);
对于(整数行=0;行#import "Test scene"
#define kRowCount 8
#define kColCount 6
#define kDotGridSpacing CGSizeMake (50,-50)

@implementation TestScene

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.userInteractionEnabled = YES;
        self.backgroundColor = [SKColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];

        CGPoint baseOrigin = CGPointMake(35, 385);
        for (NSUInteger row = 0; row < kRowCount; ++row)
        {
            CGPoint dotPosition = CGPointMake(baseOrigin.x, row * (kDotGridSpacing.height) + baseOrigin.y);

            for (NSUInteger col = 0; col < kColCount; ++col)
            {
                SKSpriteNode* dot = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(10, 10)];
                dot.position = dotPosition;
                dot.name = [NSString stringWithFormat:@"dot_%d_%d", row, col];

                dotPosition.x += kDotGridSpacing.width;

                [self addChild:dot];
                dot.userInteractionEnabled = NO;
            }
        }

        for (NSUInteger row = 0; row < kRowCount; ++row)
        {
            CGPoint htouchareaPosition = CGPointMake(baseOrigin.x + 0.5*(kDotGridSpacing.width), row * (kDotGridSpacing.height) + baseOrigin.y);

            for (NSUInteger col = 0; col < kColCount-1; ++col)
            {
                SKSpriteNode* htoucharea = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:0.18 green:0.702 blue:0.91 alpha:0.5] size:CGSizeMake(35,25)];
                htoucharea.position = htouchareaPosition;
                NSString *htouchareaName = [NSString stringWithFormat:@"htoucharea_%d_%d", row, col];
                htoucharea.name = htouchareaName;
                htoucharea.userInteractionEnabled = YES;

                htouchareaPosition.x += kDotGridSpacing.width;

                [self addChild:htoucharea];
                htoucharea.userInteractionEnabled = NO;
            }
        }

        for (NSUInteger row = 0; row < kRowCount-1; ++row)
        {
            CGPoint vtouchareaPosition = CGPointMake(baseOrigin.x, row * (kDotGridSpacing.height) + baseOrigin.y + 0.5*(kDotGridSpacing.height));

            for (NSUInteger col = 0; col < kColCount; ++col)
            {
                SKSpriteNode* vtoucharea = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithRed:1 green:0.478 blue:0.478 alpha:0.5] size:CGSizeMake(25,35)];
                vtoucharea.position = vtouchareaPosition;
                vtoucharea.name = [NSString stringWithFormat:@"vtoucharea_%d_%d", row, col];
                vtoucharea.userInteractionEnabled = YES;

                vtouchareaPosition.x += kDotGridSpacing.width;

                [self addChild:vtoucharea];
                vtoucharea.userInteractionEnabled = NO;
            }
        }
    }

    return self;
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];

    for (SKNode* node in [self nodesAtPoint:touchLocation])
    {
        NSLog(@"Node was touched: %@", node.name);
    }
}

@end