我基本上是在看到一個精靈被觸摸後執行一個動作。這是我的代碼到目前爲止,但目前似乎沒有執行我需要的功能。任何人都能發現任何東西嗎謝謝SpriteKit - CGRects,精靈,觸摸 - 目前爲止沒有任何工作
注意:代碼設置了一個點陣,點之間有矩形子畫面,代表觸摸區域。即當其中一個觸摸區域被觸摸並且執行動作時。如果我的這是什麼樣子的解釋是窮人,請參閱本:
http://cl.ly/image/1W1e263B2b08/Screen%20Shot%202014-02-20%20at%2020.43.17.png
#define kRowCount 8
#define kColCount 6
#define kDotGridSpacing CGSizeMake (50,-50)
#import "BBMyScene.h"
@implementation BBMyScene
@synthesize dot;
@synthesize htoucharea;
@synthesize vtoucharea;
-(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;
}
-(CGRect)getSpriteRect:(SKNode *)inSprite
{
CGRect sprRect = CGRectMake(htoucharea.position.x, htoucharea.position.y, 35, 25);
return sprRect;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// get the touch
UITouch *touch = [touches anyObject];
// get the location of the touch in the main view of the view controller
CGPoint touchPoint = [touch locationInView:self.view];
CGRect hRect = [self getSpriteRect:htoucharea];
if (CGRectContainsPoint(hRect, touchPoint))
{
NSLog(@"Hello");
}
}
感謝您的快速響應和答案! – benblanchard