2

もっと速く動かしたいボールは*敵です。

*敵がボールに触れずに20秒後に速く動くようにしたい. *プレイヤーボールはあなたがコントロールできるボールです。このゲームでは、敵のボールに触れてはいけません。

20秒ごとに敵球が速くなるようにしたい.. 配列ループで何か考えていた....

助けてくれてありがとう


.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UIButton *startButton;
    NSTimer *randomMain;

    CGPoint pos; 
    IBOutlet UIImageView *enemy;
    IBOutlet UIImageView *player;

    int MainInt;
    IBOutlet UILabel *seconds;


    IBOutlet UILabel *lastTime;
    IBOutlet UILabel *theTime;
}

@property (nonatomic, retain) NSTimer *numro;

- (IBAction)start;
@end

.m

#import "ViewController.h"

@implementation ViewController

@synthesize numro;

- (IBAction)start {
    [startButton setHidden:YES];
    randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03)
      target:self selector: @selector(onTimer) userInfo:(nil) repeats:YES];
    numro = [NSTimer scheduledTimerWithTimeInterval:(1.0)
      target:self selector:@selector(countup) userInfo:nil repeats:YES];
    MainInt = 0;
    [lastTime setHidden:YES];
    [theTime setHidden:YES];
}

-(void)countup {
    MainInt += 1;
    seconds.text = [NSString stringWithFormat:@"%i", MainInt];

// I TRIED IT WITH "IF"  BUT IT I DONT WANT TO WRITE THIS FOR EVERY 20 SECONDS

    if (MainInt == 20) {
        randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.002)
      target:self selector: @selector(checkCollision) userInfo:(nil) repeats:YES];
    }
     /*   if (MainInt == 40) {
        randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.01)
      target:self selector: @selector(onTimer) userInfo:(nil) repeats:YES];
    }*/
}

-(void)onTimer{
    [self checkCollision];
    enemy.center = CGPointMake(enemy.center.x+pos.x,enemy.center.y+pos.y);
    if (enemy.center.x > 320 || enemy.center.x < 0)
        pos.x = -pos.x;
    if (enemy.center.y > 480 || enemy.center.y < 0)
        pos.y = -pos.y;
}

- (void)checkCollision {
    if( CGRectIntersectsRect(player.frame,enemy.frame))
    {
        [randomMain invalidate];
      [startButton setHidden:NO];

        CGRect frame = [player frame];
        frame.origin.x = 137.0f;
        frame.origin.y = 326.0;
        [player setFrame: frame];

        CGRect frame2 = [enemy frame];
        frame2.origin.x = 137.0f;
        frame2.origin.y = 20.0;
        [enemy setFrame: frame2];

  UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"AAA"
        message: [NSString stringWithFormat:@"BBB"] delegate:nil 
                 cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
        MainInt = 0 ;
        [numro invalidate];
        [lastTime setHidden:NO];
        [theTime setHidden:NO];
        theTime.text = seconds.text;
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [[event allTouches] anyObject];
    player.center = [myTouch locationInView:self.view]; //player ball
}

-(void)viewDidLoad {
    [lastTime setHidden:YES];
    [super viewDidLoad];
    // X Speed Y Speed
    //I tried to do anything with the X and Y speed ... but it didn't work
    pos = CGPointMake(5.0,4.0);
}

@end
4

2 に答える 2

2

ifメソッドのブロックを次のように変更しますcountup...

if(MainInt % 20 == 0) {    //True every 20th second

   int speedUpCount=MainInt/20;
   float interval=1.0-(speedUpCount/10.0);

   [randomMain invalidate];   //Must stop the previous timer

   randomMain = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector: @selector(checkCollision) userInfo:(nil) repeats:YES];
}

これが問題の解決に役立つことを願っています。

于 2012-12-22T19:40:33.440 に答える
0

// さて、20 秒から、ボールは 1 秒ごとに速くなります :S

-(void)countup {

MainInt += 1;
seconds.text = [NSString stringWithFormat:@"%i", MainInt];


if(MainInt % 20 == 0) {    //True every 20th second

    int speedUpCount=MainInt/20;
    float interval=1.0 - (speedUpCount/1000.0);

    [randomMain invalidate];   //Must stop the previous timer
    randomMain = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector: @selector(start) userInfo:(nil) repeats:YES];
于 2012-12-22T20:50:37.143 に答える