我有 10 只萤火虫,我使用下面的代码在屏幕上“飞”。该代码还用于将萤火虫保持在屏幕上。
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *blueArray = [NSArray array];
blueArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"blue1.png"],
[UIImage imageNamed:@"blue2.png"], nil];
blue.animationImages = blueArray;
blue.animationDuration = 0.20;
blue.animationRepeatCount = -1;
[blue startAnimating];
bluepos =CGPointMake(2.0, 1.5);
}
-(void) someMethod {
endingAnimationTimer = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:self selector:@selector(makeFly) userInfo:nil repeats:YES];
}
-(void) makeFly {
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y);{
if(blue.center.x > 480 || blue.center.x <0)
bluepos.x = -bluepos.x;
if(blue.center.y > 320 || blue.center.y <0)
bluepos.y = -bluepos.y;
}
}
“飞行”效果很好,除了当萤火虫撞击屏幕边缘并反转方向以将它们保持在屏幕上时,萤火虫图像本身仍然“面向”其他方向,因此看起来它们有一半时间向后飞行。
我想设置它,以便当萤火虫撞击屏幕边缘时,它们会反转方向并且图像本身也会反转。
我试过这个:
在.h
@property (nonatomic, assign) BOOL blueIsFacingRight;
在.m
@synthesize blueIsFacingRight;
-(void) makeFly {
blue.center = CGPointMake(blue.center.x+bluepos.x, blue.center.y+bluepos.y); {
if(blue.center.x > 480 ) {
if (blueIsFacingRight == YES) {
blue.transform = CGAffineTransformMakeScale(-1, 1);
blueIsFacingRight = NO;
}
bluepos.x = -bluepos.x;
}
if(blue.center.x <0) {
bluepos.x = -bluepos.x;
if (blueIsFacingRight == NO) {
blue.transform = CGAffineTransformMakeScale(1, 1);
blueIsFacingRight = YES;
}
}
if(blue.center.y > 320 )
bluepos.y = -bluepos.y;
if( blue.center.y <0)
bluepos.y = -bluepos.y;
}
}
我认为这会起作用,但是当它撞到“墙壁”时,图像并没有反转
谁能解释为什么这不起作用以及是否有更好的原因来实现我正在寻找的效果?