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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 09-04-2008, 02:03 PM   #1 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default button to create a random image?

im trying to create a button that will generate a random image every time it is pushed. so this leads me to a few questions. If I include the images in the app, then how do I dynamically pop up the image on the fly? Do I need to create a NSRect function to put the image in? can I store all the image names in an array of some sort to get a random image?

tia: rob
roberthuttinger is offline   Reply With Quote
Old 09-04-2008, 02:25 PM   #2 (permalink)
1026 Development
 
Join Date: Jul 2008
Age: 23
Posts: 1,088
Send a message via AIM to Jeremy1026
Default

Quote:
Originally Posted by roberthuttinger View Post
im trying to create a button that will generate a random image every time it is pushed. so this leads me to a few questions. If I include the images in the app, then how do I dynamically pop up the image on the fly? Do I need to create a NSRect function to put the image in? can I store all the image names in an array of some sort to get a random image?

tia: rob
I would have a UIImageView in IB. Programmatically hide it until the button is pressed, then when the button is pressed generate a random number. Each number would be linked to a specific if statement (if number == 1, if number == 2, etc.) Then in each if have a different image being placed into the imageview, and unhide it.
Jeremy1026 is offline   Reply With Quote
Old 09-04-2008, 03:59 PM   #3 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

this makes sense to me. in php you can do a rand(0,10) etc to get a random number between 1 and 10 but what is the syntax for xcode? and concatenating strings so if I generate a random number, how can I take that number to get the equivelent of: "image/imgNum" . $i . ".png"?
is there a 'cheat sheet for how to do these basic things?
roberthuttinger is offline   Reply With Quote
Old 09-04-2008, 04:02 PM   #4 (permalink)
1026 Development
 
Join Date: Jul 2008
Age: 23
Posts: 1,088
Send a message via AIM to Jeremy1026
Default

Quote:
Originally Posted by roberthuttinger View Post
is there a 'cheat sheet for how to do these basic things?
Yes, its all in the documentation.

1 + arc4random() %10

would return a random number between 1-10.

You will need to define the image for each number in the if. Scan through the documentation for "UIImageView"
Jeremy1026 is offline   Reply With Quote
Old 09-05-2008, 10:18 AM   #5 (permalink)
Registered Member
 
roberthuttinger's Avatar
 
Join Date: Sep 2008
Posts: 96
Default

what about a range between numbers? I cant find this. in php it would be something like rand(10,20);. Now I need to get a number in specific ranges based on user input.

added note: i guess I could do 1 + arc4random() %10 + 10 for a number 11 - 20 but there hs to be a more elegant way.
tia!
rob

Last edited by roberthuttinger; 09-05-2008 at 10:24 AM. Reason: poo poo an unelegant way
roberthuttinger is offline   Reply With Quote
Old 12-09-2008, 04:49 PM   #6 (permalink)
New Member
 
Join Date: Dec 2008
Posts: 12
Default

Quote:
Originally Posted by roberthuttinger View Post
this makes sense to me. in php you can do a rand(0,10) etc to get a random number between 1 and 10 but what is the syntax for xcode? and concatenating strings so if I generate a random number, how can I take that number to get the equivelent of: "image/imgNum" . $i . ".png"?
is there a 'cheat sheet for how to do these basic things?
I have a similar question, how can i call or, create a object name dynamically? Something like this:

Imagine i have this:

for (int i = 0; i < 10; i++) {
[newRecord setNumber'i':@"0"];
}

what is the syntax for this? Or in this case:

for (int i = 0; i < 100; i++) {
[image'i' setAlpha:1];
}

so it can automatically set the alpha to 100 pictuers named image1, image2, image3 etc. ?


thanks =)
gavinsan is offline   Reply With Quote
Old 12-09-2008, 05:18 PM   #7 (permalink)
New Member
 
Join Date: Nov 2008
Posts: 88
Default

Quote:
Originally Posted by roberthuttinger View Post
what about a range between numbers?
You can easily write your own range function. It'd be something like

Code:
- (int)randomIntBetween:(int)min andMax:(int)max {
	int range = max - min;
	int baseRand = rand() % range;

	return baseRand + min;
}
You'd of course want to check for error conditions and such. I have this very method in my game, when I get home I can post the code for it if you want.
City41 is offline   Reply With Quote
Old 03-18-2009, 01:24 AM   #8 (permalink)
Registered Member
 
DenVog's Avatar
 
Join Date: Jan 2009
Location: Silicon Valley, USA
Posts: 622
Default

Quote:
Originally Posted by roberthuttinger View Post
what about a range between numbers? I cant find this. in php it would be something like rand(10,20);. Now I need to get a number in specific ranges based on user input.

added note: i guess I could do 1 + arc4random() %10 + 10 for a number 11 - 20 but there hs to be a more elegant way.
tia!
rob
How could this be modified to deliver a range between a negative and positive number? Right now I'm using
Code:
int endX = round(random() % 320);
to get a random number between 0 and 320. I'd like to get a range between -40 and +360, for example.
DenVog is offline   Reply With Quote
Old 05-20-2009, 02:04 PM   #9 (permalink)
New Member
 
Join Date: Mar 2009
Location: Covina, California - United States
Age: 28
Posts: 22
Default

Quote:
Originally Posted by DenVog View Post
How could this be modified to deliver a range between a negative and positive number? Right now I'm using
Code:
int endX = round(random() % 320);
to get a random number between 0 and 320. I'd like to get a range between -40 and +360, for example.

That's the same as before, except instead of adding a minimum value, you're subtracting.

360 + 40 = 400

so it'd be something like:

my Number = (arc4random() % 400) - 40;
Legrend is offline   Reply With Quote
Reply

Bookmarks

Tags
array, button, images, random

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: 263
20 members and 243 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, diyora, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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