У меня проблема с касаниями на спрайтах в 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
У меня возникли проблемы. Когда я касаюсь одного из спрайтов, я хочу что-то сделать (на данный момент это только вход в консоль). Почему мои прикосновения не распознаются?
вставить код в touchesEnded: withEvent: – AndrewShmig
@AndrewShmig Это Безразлично» t, похоже, сработало ... – benblanchard
Вы должны установить userInteractionEnabled в YES, прежде чем пытаться поймать touch, например, в вашем методе init. –