我正在尝试制作一个使用动画(UIImages)的游戏,但我有一个非常不寻常的(对我来说)错误:当两个 CGRects 之间发生交叉时,我试图为图像制作动画。检测很棒,但动画的行为很奇怪。这是代码:
Coin.h : (UIView 类)
@property (retain) NSArray * imagesCoinEclate;
我在一些教程中读到错误可能来自我的变量的无保留状态,所以我把它放在了保留中
硬币.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 摘要不可用”
谢谢你的帮助 !