问题是动画只运行一次然后再也不能重复了。我是否需要在某个地方再添加一个NSTimer?
我错过了什么?寻求帮助和建议。非常感谢!
这是代码。
首先,在ViewController.h& ViewController.m
@interface ViewController : UIViewController {
GameView* gameView;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(gameView == nil) {
gameView = [[GameView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
gameView.backgroundColor = [UIColor clearColor];
}
[self.view addSubview:gameView];
[[AppEngine sharedInstance] addSnow:CGPointMake((random() % 320),(random() % -20))];
[[AppEngine sharedInstance] addSnow:CGPointMake((random() % 320),(random() % -20))];
[[AppEngine sharedInstance] addSnow:CGPointMake((random() % 320),(random() % -20))];
[[AppEngine sharedInstance] addSnow:CGPointMake((random() % 320),(random() % -20))];
[[AppEngine sharedInstance] addSnow:CGPointMake((random() % 320),(random() % -20))];
}
在GameView.h& .M
@interface GameView : UIView <UIAccelerometerDelegate>{
CADisplayLink* displayLink;
}
-(void) timeStep;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(timeStep)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
return self;
}
-(void) timeStep {
[[AppEngine sharedInstance] timeStep];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:@"Christmas.jpg"] drawInRect:CGRectMake(0,0,320,480)]; // set the background image
CGContextRef context = UIGraphicsGetCurrentContext();
for(MySnow* snowObject in [AppEngine sharedInstance].snowObjectArray)
{
CGContextSaveGState(context);
CGContextTranslateCTM(context, snowObject.x, snowObject.y);
NSString *imagePath = [NSString stringWithFormat:@"snowflake0%d.png",snowObject.type];
[[UIImage imageNamed: imagePath] drawInRect:CGRectMake(-16,-16,32,32)];
CGContextRestoreGState(context);
}
}
第三,在snow.h&amp;米
@interface MySnow : NSObject
{
float x,y;
float vx, vy;
float rotSpeed;
float rotation;
int type;
}
@property (nonatomic) float x,y;
@property (nonatomic) float vx, vy, rotSpeed,rotation;
@property (nonatomic) int type;
-(id) initWithStartPoint:(CGPoint) startPoint withType:(int) type;
-(void) timeStep;
@implementation MySnow
@synthesize x,y,vx,vy,rotation,rotSpeed,type;
-(id) initWithStartPoint:(CGPoint) startPoint withType:(int) _type {
self = [super init];
if(self) {
x = startPoint.x;
y = startPoint.y;
vx = RANDOM_FLOAT() * 1 + 0.1;
vy = RANDOM_FLOAT() * 2 + 0.1;
type = _type;
}
return self;
}
-(void) timeStep {
y += vy;
}
@interface AppEngine : NSObject {
NSMutableArray* snowObjectArray;
float ax, ay;
}
@property (readonly) NSMutableArray* snowObjectArray;
@property (nonatomic) float ax,ay;
+(AppEngine*) sharedInstance;
+(void) destoryInstance;
-(void) timeStep;
-(void) addSnow:(CGPoint) point;
static AppEngine* _sharedEngine;
@implementation AppEngine
@synthesize snowObjectArray;
@synthesize ax,ay;
+(AppEngine*) sharedInstance {
if(_sharedEngine == nil)
_sharedEngine = [[AppEngine alloc] init];
return _sharedEngine;
}
+(void) destoryInstance {
if(_sharedEngine != nil) {
_sharedEngine = nil;
}
}
-(id) init {
self = [super init];
if(self) {
snowObjectArray = [[NSMutableArray alloc] init];
}
return self;
}
-(void) addSnow:(CGPoint) point {
int type = (arc4random() % 9) + 1; // random snow image 01 to 09
MySnow* snowObject = [[MySnow alloc] initWithStartPoint:point withType:type];
[snowObjectArray addObject:snowObject];
}
-(void) timeStep {
NSMutableArray* removeArray = [NSMutableArray array];
for(MySnow* item in snowObjectArray) {
[item timeStep];
if(item.y > 400 || item.y < -100)
[removeArray addObject:item];
}
[snowObjectArray removeObjectsInArray:removeArray];
[removeArray removeAllObjects];
}
答案 0 :(得分:0)
这是你的代码吗?你能解释它是如何开始动画的吗?
在我看来,AppEngine的timeStep方法具有代码,一旦到达某个边界,就会专门删除snowObjectsArray中的每个项目。但是,如果这是您的代码,为什么不了解它的作用呢?
编辑:好的,所以这是来自演讲。如果你想让动画继续下去,我会这样做:
从viewDidLoad方法中获取最后一段代码(创建和添加snow对象的代码)并将其放在单独的方法中。使viewDidLoad调用该方法。我们调用方法makeItSnow。
然后,在AppEngine timeStep方法结束时,如果snowObjectArray中的对象计数降为0,则调用makeItSnow。
这种方法会让一组雪花从顶部开始,掉下来消失,然后另一组雪花开始。如果您希望连续的雪花流落下,则更改timeStep方法以计算它删除的雪花对象的数量,并在方法结束时将多个雪花添加回阵列。这会导致程序在每个雪花消失时添加新的雪花。
另一种方法是向AppEngine添加重复计时器,并使该计时器创建另一个雪花。这样可以定期添加雪花。