簡體   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