2010-12-26 1 views
0

iPhoneのプログラミングが初めてであると宣言することから始めることができます。だから、 "a"ボールがiPhoneのスクリーンの上からランダムに落ちるアニメーションをどのように実装するのかという解決策について、私は助けを求めたい。ボールの属性は、異なるサイズ、スピード、およびバウンスすることができ、衝突も含まれる。iPhoneでランダムにボールを落とすにはどうすればいいですか

私はボールの画像をInterface Builder(IB)で追加し、IBのファイル所有者にUIImageViewを接続しました。 IBOutletキーワードを.hファイルに追加します(下記の.hと.mコードを参照してください)。 コードを実行すると、イメージが表示され、即座に消えてしまいます。どのように私はこのボールをいくつかのバウンスまたは衝突でランダムに落とさせる。

具体的には、iPhoneのゲームで得られたようなアニメーションのようなものを作りたいと思っています...ボールが落ちてもまだ跳ね返ります。

ご協力いただきありがとうございます CHEERS !!

***The .h file*** 

@interface TestBallsViewController : UIViewController { 

    IBOutlet UIImageView *ball; 

CGPoint ballMovement; 

double size; 
double speed; 

} 


@property(nonatomic, retain)IBOutlet UIImageView *ball; 


- (void)initializeTimer; 


- (void)animateBall:(NSTimer *)theTimer; 



**The .m file** 

@implementation TestBallsViewController 

@synthesize ball; 


- (void)dealloc { 

[ball release]; 

    [super dealloc]; 
} 



- (void)viewDidLoad { 
[super viewDidLoad]; 
ballMovement = CGPointMake(2,2); 
[self initializeTimer]; 

} 



- (void)initializeTimer 
{ 
    float theInterval = 0.3; 

    [NSTimer scheduledTimerWithTimeInterval:theInterval target:self 

        selector:@selector(animateBall:) userInfo:nil repeats:YES]; 
} 



- (void)animateBall:(NSTimer *)theTimer 

{ 
    ball.center = CGPointMake(ball.center.x+ballMovement.x, 
                 ball.center.y+ballMovement.y); 


    int startX =arc4random()%100; 
        int endX = arc4random()%320; 
    ball.frame= CGRectMake(startX, -100, 15.0 * size, 15 * size); 
        ball.frame = CGRectMake(endX, 480, 15.0 * size, 15.0 * size); 



    if(ball.center.x > 300 || ball.center.x < 20) ballMovement.x = 
                    - ballMovement.x; 
    if(ball.center.y > 440 || ball.center.y < 40) ballMovement.y = 
                    - ballMovement.y; 

} 
+0

真ん中にランダムな内容のポイントは何ですか?ボールを15 *サイズx 15 *サイズのポイント(ランダム、-100)に移動し、すぐに同じサイズで(ランダム、480)に移動します。ここで何をしようとしていますか? – ughoavgfhw

+0

途中のランダムな内容が開始点になっているか、画面上で画像が開始(startX)している(-100)と(480)で終了すると想定されています。 y軸。私がしようとしているのは、画面の一番上から一枚の画像を落とし、一番下に消えることです。画像は、iPhoneの画面のx軸上の任意の位置と1つの画像から作成されます。 – Zaki

答えて

0
- (void)animateBall:(NSTimer *)theTimer 

{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.0]; 
    ball.center = CGPointMake(ball.center.x+ballMovement.x, 
                 ball.center.y+ballMovement.y); 


    int startX =arc4random()%100; 
        int endX = arc4random()%320; 
    ball.frame= CGRectMake(startX, -100, 15.0 * size, 15 * size); 
        ball.frame = CGRectMake(endX, 480, 15.0 * size, 15.0 * size); 



    if(ball.center.x > 300 || ball.center.x < 20) ballMovement.x = 
                    - ballMovement.x; 
    if(ball.center.y > 440 || ball.center.y < 40) ballMovement.y = 
                    - ballMovement.y; 
    [UIView commitAnimations]; 

} 
+0

クリスタル・スカルはコードを編集してくれたことに感謝しています...私は今、画面上のイメージを見ることができますが、それでも私が望むものは私に与えられていません。乾杯! – Zaki

関連する問題