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?
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.
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?
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
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. ?
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.
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.