2014-10-03 63 views
0

我試圖爲將被重複調用的節點創建一個SKNode子類,每次都有用戶交互,然後產生中斷/爆炸效果。但是,每當我嘗試運行代碼時,都會收到SIGABRT錯誤。與調試終端中的___stack_chk_fail有關。SKNode子類中的__stack_chk_fail

此外,在檢查器中,當SIGABRT的代碼中斷時,所有這些屬性都顯示爲nil

這是我SKNode子類的.M:

#import "CandyNode.h" 

static const int MAX_VELOCITY = 20; 
static const int MIN_VELOCITY = 2; 

@implementation CandyNode 

- (instancetype)init { 
    self = [super init]; 
    if (self == [super init]) 
    { 
     _spriteType = arc4random()%4+1; 
     NSString* imageRand = [NSString stringWithFormat:@"Sprite %i", _spriteType]; 

     _background = [SKSpriteNode spriteNodeWithImageNamed:imageRand]; 
     _background.name = @"Main"; 
     _background.zPosition = 3; 
     [self addChild:_background]; 

     _burst1 = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"S%dB%d",_spriteType,1]]; 
     _burst2 = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"S%dB%d",_spriteType,2]]; 
     _burst3 = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"S%dB%d",_spriteType,3]]; 

     _burst1.name = @"Burst #1"; 
     _burst2.name = @"Burst #2"; 
     _burst3.name = @"Burst #3"; 

     [_background setHidden:NO]; 

     [_burst1 setHidden:YES]; 
     [_burst2 setHidden:YES]; 
     [_burst3 setHidden:YES]; 

     [self addChild:_burst1]; 
     [self addChild:_burst2]; 
     [self addChild:_burst3]; 

     double burstAngle[3]; 
     int offsetAngle = 240; 
     double burstDisplacement[3]; 
     CGVector burstVector[3]; 

     CGFloat x, y; 
     for (int i = 0; i<=3; i++){ 
      burstAngle[i] = (arc4random()%60+120*i+offsetAngle)*3.1415/180; 
      burstDisplacement[i] = arc4random()%(MAX_VELOCITY-MIN_VELOCITY)+ MIN_VELOCITY; 
      x = burstDisplacement[i]*cos(burstAngle[i]); 
      y = burstDisplacement[i]*sin(burstAngle[i]); 
      burstVector[i] = CGVectorMake(x, y); 
     } 

     _explode1 = [SKAction sequence:@[[SKAction fadeInWithDuration:0], 
             [SKAction moveBy:burstVector[0] duration:.5], 
             [SKAction fadeOutWithDuration:.2]]]; 
     _explode2 = [SKAction sequence:@[[SKAction fadeInWithDuration:0], 
             [SKAction moveBy:burstVector[1] duration:.5], 
             [SKAction fadeOutWithDuration:.2]]]; 
     _explode3 = [SKAction sequence:@[[SKAction fadeInWithDuration:0], 
             [SKAction moveBy:burstVector[2] duration:.5], 
             [SKAction fadeOutWithDuration:.2]]]; 

    } 
    return self; 

} 

.H:

#import <SpriteKit/SpriteKit.h> 
@interface CandyNode : SKNode 

//-(id)initWithImageNumber:(int)spriteType; 
//-(void)explodeCandy; 

@property (nonatomic) SKSpriteNode *background; 
@property (nonatomic) SKSpriteNode *burst1; 
@property (nonatomic) SKSpriteNode *burst2; 
@property (nonatomic) SKSpriteNode *burst3; 

@property (nonatomic) SKAction *explode1; 
@property (nonatomic) SKAction *explode2; 
@property (nonatomic) SKAction *explode3; 

@property (nonatomic) int spriteType; 


@end 

而且這一切都是叫在主SKScene有:_NodeA = [CandyNode new];,_NodeA在類的被定義標題爲@property (nonatomic) CandyNode *NodeA;

我的問題很明顯,我怎麼能得到這個工作,或者如果我有一個可怕的誤解如何使用子類和SpriteKit,如何c我有不同的做法嗎?

回答

2

要調用初始化兩次:

self = [super init]; 
if (self == [super init]) 

首先正確地分配給自己,第二個創建一個實例,它具有自我比較,然後拋出新的實例了。

爲了解決這個問題:

self = [super init]; 
if (self) 

UPDATE:

可以聲明與3個元素的數組:

double burstAngle[3]; 

然後,你枚舉與一定範圍的0陣列上3(用於循環運行四次):

for (int i = 0; i<=3; i++){ 

當我是3你有一個索引超出界限的情況下,數組索引是從零開始的。

修復它像這樣:

for (int i = 0; i < 3; i++){ 
+0

這不是問題。奇怪的是,[super init]在調用兩次時返回相同的實例。 – 0x141E 2014-10-03 12:42:24

+0

已更新,您也有索引超出範圍 – LearnCocos2D 2014-10-03 12:51:57

+0

謝謝。這是固定它的索引越界。當然,不要讓錯誤信息直觀。 – Jackson 2014-10-03 13:42:50