繁体   English   中英

启用精灵(spritekit)的触摸

[英]Enable touches on an sprite (spritekit)

我在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

我有麻烦了 当我触摸其中一个精灵时,我想做一些事情(此刻仅记录到控制台)。 为什么我的触摸无法被识别?

仅当您在同一函数内尝试执行userInteractionEnabled = YES时才调用该函数,因此永远不会调用该函数。 而是将该代码放入您的Sprite的init方法中。

我尝试了您的代码,并且触摸效果很好。 但是,代码中几乎没有其他问题:

  • 在init的for循环中,仅将最后创建的节点设置为ivars(点,hTouchArea,vTouchArea)。 然后,在touchesEnded方法中,您尝试使用此ivar来检测触摸是否在此节点的框架内。 当然,这是行不通的,因为您仅缓存了最后一个节点。 (我在这里使用了不同的方法-请参见touchesBegan方法)

  • 如果只想在场景节点中注册触摸,则必须从添加的其他节点上禁用用户交互。 否则,您的其他节点将阻止您的场景进行触摸。 (使用userInteractionEnabled = NO)。

  • [touch locationInView:self.view]为您提供视图中的位置。 相反,您需要定位在节点本身中。 您必须改为使用[touch locationInNode:self]。

在这里,我为您修复了代码(我也对其进行了测试,以便可以正常工作):

#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM