我在SpriteKit中遇到sprite的问题。
这是我的代码。
#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
我遇到了麻烦。当我触摸其中一个精灵时,我想做一些事情(此刻只是登录到控制台)。为什么我的触摸不被识别?
答案 0 :(得分:1)
只有当你试图在同一个函数中执行的userInteractionEnabled = YES
时才调用该函数,因此永远不会调用它。将代码放入精灵的init
方法中。
答案 1 :(得分:0)
我尝试了你的代码,触摸注册就好了。但是,代码中几乎没有其他问题:
在init中的for循环中,只将最后创建的节点设置为ivars(dot,hTouchArea,vTouchArea)。然后,在touchesEnded -method中,您尝试使用此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