我試圖製作一個遊戲,它使用動畫(的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」
感謝您的幫助!
@Rob Nah,'init'調用'initWithFrame:CGRectZero'。 – 2013-03-15 16:52:13
即使我把View = [[Coin alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,480.0)];沒有改變任何東西。我在初始部分調用了另一個動畫,她正在工作。 – user2057209 2013-03-15 16:52:52
@ H2CO3我不知道名爲'initWithFrame'的默認'init'。謝謝! – Rob 2013-03-15 16:58:50