我基本上想要在触摸精灵后执行动作。到目前为止这是我的代码,但目前它似乎没有执行我需要的功能。谁能发现任何东西?感谢
注意:代码设置了一个点阵,点之间有矩形的精灵,应该代表触摸区域。即,当触摸其中一个触摸区域时,执行动作。如果我对这看起来很糟糕的解释,请参考:
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");
}
}
答案 0 :(得分:0)
你的htoucharea指针只会指向你制作的最后一个精灵。此代码可能适用于一个精灵。不确定没有测试它。您可以将所有精灵放入一个数组中,并根据触摸点测试每个精灵的矩形,或者您可以创建精灵子类并让每个精灵都触摸它们。
数组方法可能更容易理解。创建每个sprite时,将其添加到可变数组属性中。然后你会让他们进行测试。
答案 1 :(得分:0)
就个人而言,我已经考虑到SKSpriteNode
是UIResponder
的后代并且精灵本身通过touchesBegan: withEvent:
注册触摸的事实我已经用委托实现了整个事情场景充当精灵代表的模式。然后在我的精灵子类中,我只需:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.delegate touchesBeganOnSprite:self];
}
然后在我的场景中我可以:
- (void)touchesBeganOnSprite:(SKSpriteNode *)sprite{
// do whatever I like with the sprite...
}
您当然可以在不使用委托模式的情况下使用touchesBegan:withEvent:
。通过使用通知或在节点的scene
属性上执行选择器/方法调用。 (我个人认为委托模式是可行的方式)。