我为 iOS 游戏编写了一个简单的动画,并尝试将其转换为 Mac OS 游戏。
这是我的 iOS 代码
NSMutableArray *imgArr = [[[NSMutableArray alloc] init] autorelease];
for(int i = 1; i < 6 + 1; i++) {
[imgArr addObject: [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.png", filePrefix, i]]];
}
UIImageView * animation = [[UIImageView alloc] initWithFrame:rect];
animation.animationImages = imgArr;
animation.animationRepeatCount = 1;
[animation startAnimating];
[self.view addSubview:animation];
至于 cocoa ,这部分出现错误..我该如何解决这个问题?
//animation.animationImages = imgArr;
//animation.animationRepeatCount = 1;
//[animation startAnimating];
带有计时器的新代码
for(int i = 0; i < 6; i++)
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(anim)
userInfo:nil
repeats:NO];
- (void)anim {
++num;
NSImageView *animation = [[NSImageView alloc] initWithFrame:NSMakeRect(shipPosition.x, shipPosition.y, shipSize.width, shipSize.height)];
[animation setImage: [NSImage imageNamed:[NSString stringWithFormat:@"explosprite%d.png", num]] ];
[objectView addSubview:animation];
[animation release];
}
最佳答案
Cocoa 中根本不存在 UIImageView。 Cocoa 并不真正支持 Sprite 动画,就像您在这里尝试做的那样。 AFAIK 最常见的方法就是设置一个 NSTimer 并在计时器触发时适当更新图像。
您还可以创建一个处理动画图像的自定义 CALayer 子类。我不确定这一定更容易,但这是一个选择。
关于cocoa - Cocoa Objective-C 中的简单动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062809/