Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-03-2010, 06:44 AM   #1 (permalink)
Persian Developer
 
Join Date: Mar 2009
Posts: 274
Momeks is on a distinguished road
Default Moving a ball randomly

Hi , I am trying to move a ball across the screen with random coordinates . but my object moves just around x axis ! here is my code :


PHP Code:
 [UIView beginAnimations:nil context:nil];
 [
UIView setAnimationDuration:0.70];
 [
UIView setAnimationRepeatCount:1000];
 [
UIView setAnimationRepeatAutoreverses:YES];

 
CGFloat x = (CGFloatrandom()/(CGFloatRAND_MAX self.view.bounds.size.width;
 
CGFloat y = (CGFloatrandom()/(CGFloatRAND_MAX self.view.bounds.size.height;


 
CGPoint squarePostion CGPointMake(yx);
 
blue3.center squarePostion;

 [
UIView commitAnimations]; 
Thank you .
__________________
My Applications On the App Store


Momeks is offline   Reply With Quote
Old 12-03-2010, 08:25 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Momeks View Post
Hi , I am trying to move a ball across the screen with random coordinates . but my object moves just around x axis ! here is my code :


PHP Code:
 [UIView beginAnimations:nil context:nil];
 [
UIView setAnimationDuration:0.70];
 [
UIView setAnimationRepeatCount:1000];
 [
UIView setAnimationRepeatAutoreverses:YES];

 
CGFloat x = (CGFloatrandom()/(CGFloatRAND_MAX self.view.bounds.size.width;
 
CGFloat y = (CGFloatrandom()/(CGFloatRAND_MAX self.view.bounds.size.height;


 
CGPoint squarePostion CGPointMake(yx);
 
blue3.center squarePostion;

 [
UIView commitAnimations]; 
Thank you .
Several things.

You should use arc4random(), not random(). arc4radom gives much better random numbers, and it's self-seeding, so you don't get the same sequence every time you run your program.

Second, you need to use the modulo operator ("%") to get a random number in a given range. Try this:

Code:
 CGFloat x = (CGFloat) arc4random() % self.view.bounds.size.width;
 CGFloat y = (CGFloat) random() % self.view.bounds.size.height;
Note that if you let the ball's center move all the way from 0 to the height/width of your bounds, the ball will be 1/2 off screen at the max/min value. You should really keep it from going below ball.bounds.size.width/2, ball.bounds.size.height/2, and don't let it go above self.view.bounds.size.width - ball.bounds.size.width/2, self.view.bounds.size.height - ball.bounds.size.height/2;
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 12-03-2010, 08:57 AM   #3 (permalink)
Persian Developer
 
Join Date: Mar 2009
Posts: 274
Momeks is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Several things.

You should use arc4random(), not random(). arc4radom gives much better random numbers, and it's self-seeding, so you don't get the same sequence every time you run your program...........

thank you ,, but the compiler gives me this errors :

__________________
My Applications On the App Store


Momeks is offline   Reply With Quote
Old 12-03-2010, 10:09 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Momeks View Post
thank you ,, but the compiler gives me this errors :
Oh, sorry about that. I forgot that CGSize variables are floats. You need to cast them to integers:


Code:
 CGFloat x = (CGFloat) arc4random() % (int)self.view.bounds.size.width;
 CGFloat y = (CGFloat) random() % (int) self.view.bounds.size.height;
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 12-03-2010, 11:04 AM   #5 (permalink)
Persian Developer
 
Join Date: Mar 2009
Posts: 274
Momeks is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Oh, sorry about that. I forgot that CGSize variables are floats. You need to cast them to integers:


Code:
 CGFloat x = (CGFloat) arc4random() % (int)self.view.bounds.size.width;
 CGFloat y = (CGFloat) random() % (int) self.view.bounds.size.height;

Thank you , but did you test your code? because have same errors !
Code:
[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.70];
	[UIView setAnimationRepeatCount:1000];
	[UIView setAnimationRepeatAutoreverses:YES];
	

	CGFloat x = (CGFloat) arc4random() % (int) self.view.bounds.size.width;
	CGFloat y = (CGFloat) random() % (int) self.view.bounds.size.height;
	
	CGPoint squarePostion = CGPointMake(y, x);
	blue1.center = squarePostion ;
I think problem its because of % ! , when I change % to / or * works fine ! but doesn't move great .
__________________
My Applications On the App Store


Momeks is offline   Reply With Quote
Old 12-03-2010, 11:29 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Momeks View Post
Thank you , but did you test your code? because have same errors !
Code:
[UIView beginAnimations:nil context:nil];
	[UIView setAnimationDuration:0.70];
	[UIView setAnimationRepeatCount:1000];
	[UIView setAnimationRepeatAutoreverses:YES];
	

	CGFloat x = (CGFloat) arc4random() % (int) self.view.bounds.size.width;
	CGFloat y = (CGFloat) random() % (int) self.view.bounds.size.height;
	
	CGPoint squarePostion = CGPointMake(y, x);
	blue1.center = squarePostion ;
I think problem its because of % ! , when I change % to / or * works fine ! but doesn't move great .
No, it's not my code, it's yours. I'm trying to help you out. You need to experiment and figure this out for yourself. I'm trying to work on my own project and help out a little on the forum too.

You might need an extra pair of parenthesis around the type cast, like this:


Code:
CGFloat x = (CGFloat) arc4random() % ((int) self.view.bounds.size.width);
CGFloat y = (CGFloat) random() % ((int) self.view.bounds.size.height);
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 12-04-2010, 03:09 PM   #7 (permalink)
Persian Developer
 
Join Date: Mar 2009
Posts: 274
Momeks is on a distinguished road
Default

finally solved the compiler error ! and it works ! ,but still doesn't move randomly !!!! just specific axis !!!! i don't know why !



Code:
	
    CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
    CGFloat y =  (CGFloat) ( random() % (int)self.view.bounds.size.height );
__________________
My Applications On the App Store


Momeks is offline   Reply With Quote
Old 12-04-2010, 04:59 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Momeks View Post
finally solved the compiler error ! and it works ! ,but still doesn't move randomly !!!! just specific axis !!!! i don't know why !



Code:
	
    CGFloat x = (CGFloat) (arc4random() % (int) self.view.bounds.size.width);
    CGFloat y =  (CGFloat) ( random() % (int)self.view.bounds.size.height );


I only changed the code that calculated x. You'll need to do the same thing for y.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 367
6 members and 361 guests
chemistry, daudrizek, HemiMG, Kirkout, MarkC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,665
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, daudrizek
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0