2014-02-20 55 views
0

我基本上是在看到一個精靈被觸摸後執行一個動作。這是我的代碼到目前爲止,但目前似乎沒有執行我需要的功能。任何人都能發現任何東西嗎謝謝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"); 
} 

} 

回答

0

你htoucharea指針只會指向您所做的最後^ h精靈。此代碼可能適用於一個精靈。不確定沒有測試它。你可以將所有精靈放入一個數組中,並根據觸摸點測試每一個精靈,或者你可以對精靈進行子類化,並在每一個精靈都被觸摸的情況下進行報告。

數組方法可能更容易理解。當你創建每個精靈時,將它添加到一個可變數組屬性中。然後你會讓他們去測試。

+0

感謝您的快速響應和答案! – benblanchard

0

個人而言,我已經考慮到一個事實,即SKSpriteNodeUIResponder的後裔,並通過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屬性上執行選擇器/方法調用。 (我個人認爲代表模式是最好的選擇)。