2013-03-15 119 views
0

我試圖製作一個遊戲,它使用動畫(的UIImages),但我有一個真正unuasal(對我)的bug: 我試圖動畫時出現的圖像兩個CGRects之間的交集。檢測很好,但動畫的行爲很奇怪。下面是代碼:Nsarray摘要不可用(EXC_BAD_ACCESS代碼1)

Coin.h:(UIView類)

@property (retain) NSArray * imagesCoinEclate; 

我的一些教程,錯誤可能來自我的變量的無保留statu讀,所以我把它保留

Coin.m:

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

CGImageRef imageToSplitEclate = [UIImage imageNamed:@"Coin_Anim2_Spritesheet4x4_110x100.png"].CGImage; 

    int x2 = 300; 
    int y2 = 0; 
    CGImageRef partOfImgEclate1 = CGImageCreateWithImageInRect(imageToSplitEclate, CGRectMake(x2, y2, 100, 110)); 
    UIImage *imgEclate1 = [UIImage imageWithCGImage:partOfImgEclate1]; 
    CGImageRelease(partOfImgEclate1); 

    ... 

    x2 = 0; 
    y2 = 330; 
    CGImageRef partOfImgEclate16 = CGImageCreateWithImageInRect(imageToSplitEclate, CGRectMake(x2, y2, 100, 110)); 
    UIImage *imgEclate16 = [UIImage imageWithCGImage:partOfImgEclate16]; 
    CGImageRelease(partOfImgEclate16); 

    _imagesCoinEclate = [NSArray arrayWithObjects:imgEclate1,imgEclate2,imgEclate3,imgEclate4,imgEclate5,imgEclate6,imgEclate7,imgEclate8,imgEclate9,imgEclate10,imgEclate11,imgEclate12,imgEclate13,imgEclate14,imgEclate15,imgEclate16, nil]; 

} 
    return self; 
} 

而這個陣列被稱爲在一個方法:

-(void)AnimationCoinExplose:(UIImageView *)imageViewCoin withCurrentX:(float)current 
{ 
    [imageViewCoin stopAnimating]; 
    CGPoint centerCoin; 
    centerCoin.x = currentX; 
    centerCoin.y = imageViewCoin.center.y - ((imageViewCoin.frame.size.height)/2); 
    UIImageView * Explode = [[UIImageView alloc] initWithFrame:CGRectMake(currentX + imageViewCoin.center.x, centerCoin.y, imageViewCoin.frame.size.width + 10, imageViewCoin.frame.size.height + 10)]; 
    //NSLog(@"CurrentX : %f\nX : %f\nY : %f\nX size : %f\nY size : %f", currentX, currentX + imageViewCoin.center.x, centerCoin.y, imageViewCoin.frame.size.width + 10, imageViewCoin.frame.size.height + 10); 
    imageViewCoin.alpha = 0.0f; 
    [Explode setAnimationImages:_imagesCoinEclate]; 
    [Explode setAnimationRepeatCount:1]; 
    [Explode setAnimationDuration:(1/24)]; 
    [Explode startAnimating]; 
    [self addSubview:Explode]; 
    [Explode release]; 
    _IntersectionCheck = -1; 
} 

這種方法被稱爲在一個UIViewController,和先前初始化像這樣:

View = [[Coin alloc] init]; 
[self.view addSubview:View]; 

並與加速度計這樣的使用方法:

if (View.IntersectionCheck == -1) 
    { 
     if (Test.alpha == 1.0f) 
     { 
      [View CoinIntersection:Test becausePigX:current.x andPigY:current.y]; 
     } 
     if (Test2.alpha == 1.0f) 
     { 
      [View CoinIntersection:Test2 becausePigX:current.x andPigY:current.y]; 
     } 
    } 
    else if (View.IntersectionCheck == 0) 
    { 

} 

當我把所有的代碼Coin.m進入方法

-(void)AnimationCoinExplose:(UIImageView *)imageViewCoin withCurrentX:(float)current 

它是完美的工作,但不是當它在初始化。 錯誤是「EXC_BAD_ACCESS(代碼1)」,並在調試控制檯「Nsarray summary summapsed」

感謝您的幫助!

+0

@Rob Nah,'init'調用'initWithFrame:CGRectZero'。 – 2013-03-15 16:52:13

+0

即使我把View = [[Coin alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,480.0)];沒有改變任何東西。我在初始部分調用了另一個動畫,她正在工作。 – user2057209 2013-03-15 16:52:52

+0

@ H2CO3我不知道名爲'initWithFrame'的默認'init'。謝謝! – Rob 2013-03-15 16:58:50

回答

0

我的一些教程,錯誤可能來自我的變量的無保留statu讀,所以我把它在保留

這是正確的,它只是你不是在做任務對。

_imagesCoinEclate = [NSArray arrayWithObjects:imgEclate1, ..., nil]; 

這就賦予的實例變量直接,繞過setter方法,讓你創建自動釋放的情況下不被保留(二傳手會這麼做),它的過早釋放。使用屬性setter來代替,而不要指望神奇的地方是不是:

self.imagesCoinEclate = [NSArray arrayWithObjects:imgEclate1, ..., nil]; 

或者,如果你想直接使用實例變量,分配的初始化對象分配給它,避免自動釋放:

_imagesCointEclate = [[NSArray alloc] initWithObjects:imgEclate1, ..., nil]; 

順便說一句,你應該重構這16個對象的創建。這太糟糕了。

+0

不,你唯一不應該在'init'和'dealloc'方法中使用setter存取方法的地方。請參閱[不要在初始化方法和dealloc中使用訪問器方法](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6)。 – Rob 2013-03-15 16:53:34

+0

@Rob我不同意這一點。 – 2013-03-15 16:54:06

+0

正常工作!謝謝=)將盡可能接受你的答案=)通過折射這16個objetcs,你的意思是什麼? – user2057209 2013-03-15 16:56:17

相關問題