我有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;
}
}
我认为这会有效,但是当它撞到“墙”时图像不会反转
任何人都可以解释为什么这不起作用,如果有更好的原因可以实现我正在寻找的效果?
答案 0 :(得分:2)
在两种情况下,您都将变换X值设置为-1。当它面向右边时,你应该设置像这样的CGAffineTransformMakeScale(1,1);
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;
}
}
答案 1 :(得分:0)
**blue.transform =CGAffineTransformIdentity;**
上面是我的makeFly方法,上面有一堆其他东西,我正在反转图像,所以我在下次调用该方法时撤消了更改(0.03以后)
男人我感到愚蠢