Hi I have a series of of animation images that i coded using an NSAray. I need to know the best way to randomize those animations so that i don't have to see the same sequence ever time. In the same order. Any Help would be appreciated very much. Thanks
Hi I have a series of of animation images that i coded using an NSAray. I need to know the best way to randomize those animations so that i don't have to see the same sequence ever time. In the same order. Any Help would be appreciated very much. Thanks
Search this board for "array shuffle" - I know we've covered it before.
So i have quite a few of these and i need to shuffle them. Any help would be great
It seems you have a lot of stuff like "button += 1; switchView += 1;" that happens in every case? Then you can move that outside the if statements so you don't have to repeat yourself.
You can also make a master array of arrays, and then choose an array from that by index. Something like this.
Code:
//do this once, maybe in viewDidLoad or initWithNib?
NSArray *cowArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Cube01.png"],
//etc.
[UIImage imageNamed:@"Cube19.png"],
[UIImage imageNamed:@"Cow On Cube.png"],
nil];
NSArray *horseArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Cube01.png"],
//etc.
[UIImage imageNamed:@"Cube19.png"],
[UIImage imageNamed:@"Cow On Cube.png"],
nil];
arrayOfImageArrays = [[NSArray alloc] initWithObjects:
cowArray, horseArray, nil];
//when you need to get the image array
box.animationImages = [arrayOfImageArrays objectAtIndex: switchView];
//if you want a random array from the array of arrays:
int choice = arc4random() % [arrayOfImageArrays count];
box.animationImages = [arrayOfImageArrays objectAtIndex: choice];
//when you need to get the image array
//box.animationImages = arrayOfImageArrays objectAtIndex: nil];
//if you want a random array from the array of arrays:
int choice = arc4random() % [arrayOfImageArrays count];
box.animationImages = [arrayOfImageArrays objectAtIndex: choice];
(1) you're adding the image arrays to arrayOfImageArrays too soon - you can't add them to arrayOfImageArrays until after they're actually created. Until you create horse array and cow array those variables still point to nil, so you can't add them to arrayOfImageArrays .
(2)Also, arrayOfImageArrays should be in instance variable (declared in the .h file between the brackets of the @interface ) if you need to access it from other methods in this class like the pushButton method.
I am in the process of trying out the code you suggested but one problem I see is that there doesn't appear to be any way to keep the array from producing repeats. How can I keep this from happening? I don't mind repeats so long as they aren't the same back to back. Thanks in advance!
I am in the process of trying out the code you suggested but one problem I see is that there doesn't appear to be any way to keep the array from producing repeats. How can I keep this from happening? I don't mind repeats so long as they aren't the same back to back. Thanks in advance!
Its true arc4random will produce repeats; you can check for them if you make an instance variable "lastChoice" though. Something like this.
Code:
int choice=0;
// keep pulling random numbers until you find
// one that's not equal to lastChoice
while (choice==lastChoice){
choice = arc4random() % [arrayOfImageArrays count];
}
lastChoice=choice;
Hi thanks for your help it has been fantastic however i have run into one more snag, i must admit i am beginning to feel pretty stupid, but i guess that's part of the learning curve. Anyway if you could help me with one more thing i would be very grateful. I am struggling keeping the last image in the animation on the screen until that sequence has been terminated. So I was wondering if you have any suggestions for this problem. Thanks
Hi thanks for your help it has been fantastic however i have run into one more snag, i must admit i am beginning to feel pretty stupid, but i guess that's part of the learning curve. Anyway if you could help me with one more thing i would be very grateful. I am struggling keeping the last image in the animation on the screen until that sequence has been terminated. So I was wondering if you have any suggestions for this problem. Thanks
What's not working? What does it do now after the last frame? I see animationRepeatCount=1 in your code, I though that would do it.
It is running through the animation fine and at a random. However the method i was using to keep the last frame (Image) on screen untill the animation reset is coming up for each animation. I need to figure out how to set an individual image for the end of each animation frequency. So for instance when I push the button the cow will animate up. well i need the cow to stay up for about 2 seconds so i have an image set for the to happen. then when i push the button again it will come up with the horse for instance, well it keeps the cow image for all the images at the end of the animation. I will show you the code for what i am talking about. I need to be able to call a certain image to come for each animation array. Here let me know if this makes sense.
What happens if you do nothing, though, and just let the animation run out? Does the animation stop on the last frame? I haven't done animationImages in a while so I'm curious.
I am trying to implement the code you gave me however it is coming up with building errors could you send me an example of how you would implement what you told me to do with freezing. Here is some code i have been trying to implement it into. Thanks
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
I don't see any of the code I wrote in your last post. You said it was freezing OK, just on the wrong image.
Code:
//where you had this before
[self performSelector:@selector(cowArray) withObject:nil];
//you want this now
[self performSelector:@selector(freezeLastFrame) withObject:nil];
Then add my freezeLastFrame method to your class.
EDIT: don't use this, it's not needed. UIImageView seems to show the default .image when it's done playing.
Oh I am sorry i didn't mean that what is happening is the animation is running through the animation fine. But it doesn't keep the last image in the animation up. it basically runs through the animation and then leaves. What i am trying to accomplish is i want the animation to run and then leave the last frame or image up for about 2 to 3 seconds then set back to the image that i put as the default image in interface builder, then the button will get pushed again and the the next animation will begin and do the same thing. I hope this makes better sense. Thanks again for all the help.
I tried writing it out the way you said but it doesn't freeze the last frame. Could you take a look at this and see what you think might be the problem. Thanks
@implementation Animal_CubeViewController
- (IBAction) pushButton{
int lastChoice = 0;
int choice = 0;
// keep pulling random numbers until you find
// one that's not equal to lastChoice
I don't think there's any feedback from the system when the animation is complete. I think the best you can do is set a method to fire when you expect the animation to be over.
EDIT: Scratch that. I just did a test, and at the end of the animation the UIImageView showed the image that's in the .image property. So just make sure that box.image is set to the image that you want to show when the animation is over. No tricks required.
That works except for that it now brings up that image for every animation. I need to be able to specify a select image for the end of each array animation. If you look back at my code you will see that i have four different animation going on, and i runs through the animation and then brings up that one select image now that i just coded in. Is there a way i can set an image for each individual array animation, for example. cowArray = set cow pic. horseArray = set horse pic. etc. can you let me know what you think. thanks
Sure, set box.image to the last image in the array you chose.
Code:
//if you want a random array from the array of arrays:
int choice = arc4random() % [arrayOfImageArrays count];
box.animationImages = [arrayOfImageArrays objectAtIndex: choice];
box.image = [box.animationImages lastObject];