Xcode repetitive falling images - ios

I'm making a game that involves falling images. I have 6 images and I want each one to fall randomly (ex. The $1 bill will be falling and then a $5 bill (or whatever randomly generated image) falling either beside it or right behind it (The iOS game Sky Burger by: NimbleBit LLC or Crazy Goats by: Barry Calvagna are a couple of examples)). Can anybody help me? This is all the code I have so far
-(void)moneyDrop {
money.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"$1 (bill).png"],
[UIImage imageNamed:#"$5 (bill).png"],
[UIImage imageNamed:#"$20 (bill).png"],
[UIImage imageNamed:#"$50 (bill).png"],
[UIImage imageNamed:#"$100 (bill).png"],nil]; }
I have not necessarily worked with falling image, unless you count a Flappy Bird remake or a Ball Pong game, so please excuse the lack of code. If my question doesn't make any since, I will re-word it.

First off, it sounds to me like you want multiple UIImageViews each with a different image, not a single UIImageView with an array of animationImages.
For making things move, you can use NSTimer.
- (void)start {
mainTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(mainTimerLoop) userInfo:nil repeats:YES];
}
- (void)mainTimerLoop {
NSLog(#"Loop");
//In here, you can assign random images to and adjust the centers of money image views.
}
You could make a function like this to return an image.
- (UIImage *)moneyImageForRandomInt:(int)random {
if (random == 0) return [UIImage imageNamed:#"$1 (bill).png"];
if (random == 1) return [UIImage imageNamed:#"$5 (bill).png"];
if (random == 2) return [UIImage imageNamed:#"$20 (bill).png"];
if (random == 3) return [UIImage imageNamed:#"$50 (bill).png"];
return [UIImage imageNamed:#"$100 (bill).png"];
}
And use a function like this to assign a random image to a UIImageView.
- (void)assignRandomImageToMoneyImageView:(UIImageView *)moneyImageView {
moneyImageView.image = [self moneyImageForRandomInt:arc4random() % 5];
}
This is how I would go about it, anyway.

Related

Whats the best way to animate images to my apporach?

I have used
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1"],
[UIImage imageNamed:#"image2"],
[UIImage imageNamed:#"image3"],
[UIImage imageNamed:#"image4"], nil];
[image setAnimationRepeatCount:1];
image.animationDuration = 1;
[image startAnimating];
but it gives a flip book effect to the animation. What I am looking for is a smooth animation to set each image at given time. I have currently is NSTimers to trigger a method then I have code within the method to set a image and another NSTimer to set another image which is in a method so fourth. It gives the animation I am looking for but the problem I am facing is that I am developing a game and I want when the user pauses the game it stops on the current image it is on. The game runs on a NSTimer which triggers a method. All I do to pause the game is to invalidate the NSTimer.
What approach should I take?
you should add NSTimer and user_flag input.
This user_flag input you can declare as static int user_flag = 0
and when you are doing stop then simply make this variable as user_flag = 1.
Now implement the logic in all this condition that will look something like this one :
if(NSTimer_obj == condition && user_flag == 0)
{
//your code...
}

Why does the view disappear after an animation?

I've been trying to animate an image using this code.
- (void)viewDidLoad
{
dice.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"], nil];
[dice setAnimationRepeatCount:3];
dice.animationDuration = 1;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
Then I have a button that makes the animation play.
-(IBAction) animate:(id)sender {
[dice startAnimating];
}
Everything works perfectly. The animation plays 3 times with duration of 1 second each time. However after the animation has played 3 times, the dice image view simply disappears from the screen. How do I stop this from happening so the image doesn't disappear and I can do other stuff to the image view?
Are you setting dice.image anywhere? The animations are different than the image the imageView contains, and "play in front of" the image and then are removed. I recommend using UIImageView's startAnimatingWithCompletionBlock method:
- (void) viewDidload {
dice.image = [UIImage imageNamed:#"image3.png"]; // big assumption on my part
// the assumption is that this is the image you want
// after animating
dice.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"], nil];
[dice setAnimationRepeatCount:3];
dice.animationDuration = 1
[super viewDidLoad];
}
A "block" is a chunk of one or more lines of code that wait until after the animation finishes to be executed. If you set the image beforehand it will appear "behind" the animations as they run and may not be what you want.

How can I make an animation correlate to the current position of a UIImageView?

I have an ImageView that changes between 2 different images. When an event happens and it's in ImageView1, I want it to play a specific animation. And when an event happens and it's in ImageView2, I want it to play a specific, different animation. My code set up so far is essentially
if (ImageView1 == YES)
{
ImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"ImageAnimate1.png"],
[UIImage imageNamed:#"ImageAnimate2.png"],
[UIImage imageNamed:#"ImageAnimate3.png"],
[UIImage imageNamed:#"ImageAnimate4.png"], nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration = 1;
[ImageView startAnimating];
}
else
{
ImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"ImageAnimate5.png"],
[UIImage imageNamed:#"ImageAnimate6.png"],
[UIImage imageNamed:#"ImageAnimate7.png"],
[UIImage imageNamed:#"ImageAnimate8.png"], nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration = 1;
[ImageView startAnimating];
}
I'm aware it doesn't need to have 2 different sets of animation repeats and duration, but that was just the beginning in the event they change later.
The main problem is, the first set of animations only plays if i'm currently setting it to ImageView1. What I mean is, If i'm tapping/ touching the screen to the left it creates ImageView1, and if i'm tapping to the right it creates ImageView2. How can I change it from having to currently be tapping to the left to play that animation, to just playing the animation when the ImageView is currently displayed as such?
It might be as simple as setting it to the current UIImage, but i'm not exactly sure how to do it.
In other words, I would want to somehow write.
if (UIImage ImageViewLeft = YES)
ImageView.animationImages etc etc etc.
else ImageView.animationImages would be the second variation.
EDIT: I just changed it to
if (ImageView.image == [UIImage imageNamed:#"ImageLeft.png"])
{ code here
What happens is though now i cant change direction until another event happens. Essentially it will be stuck in either left or right until the event happens and then it correlates to the correct image then. .
A simple way would be to create a BOOL for imageIsRight. You set it to YES when you start playing an animation on the right, you set it to NO, when it goes left. Then create a timer which fires every .05 seconds (or whatever works). That timer can call a method which checks the BOOL you created and either repeats/continues the current animation or stops it and starts the other.
The above details may not be perfect for your scenario, which is a bit fuzzy to me, but the approach should get you where you need to be.

iOS Animating a series of images

I have an iOS App that loads a set of images and a duration into an array then I have a timer that displays the images like so:
- (void)fireTimer:(NSTimer *)theTimer
{
self.image = [frames objectAtIndex:currentFrame];
NSTimeInterval time = [[durations objectAtIndex:currentFrame] floatValue];
self.timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:#selector(fireTimer:) userInfo:nil repeats:NO];
currentFrame++;
if (currentFrame >= [frames count])
{
currentFrame = 0;
[timer invalidate];
}
}
To start the animation I call fireTimer and cycle through the images then when all the images are processed I call [timer invalidate] to stop the animation. I cannot use startAnimation because I need different durations for each image.
Right know I am NOT doing this on a background thread so the animation is choppy because other processing is happening whilst the image animates.
What is the best way to animate this in the background? Can I simply put this call to fireTimer in a block?
I know this may not be the best way to animate an image on iOS, but I do not want to do a lot of refactoring on the code right now.
Thanks for any suggestions or examples for a better solution!!!
I would suggest you use UIImageView to animate your images : it is made to do this task. If, like you say, some images needs to remains longer then others, then you can just display them many times. Let's say that image2 needs to be displayed twice longer than image1 and image3, just initialize the array animationImages on your UIImageView like : #[image1, image2, image2, image3].
Instead of using a timer, you could use performSelector: withObject: afterDelay:
-(void)loopBackground:(int)index
{
if(index < [self.durations count]{
self.image = [self.frames objectAtIndex:index];
[self performSelector:#selector(loopBackground:) withObject:index++ afterDelay:[[self.durations objectAtIndex:index] floatValue]];
}
}

How can I take the current integer value out of my array loop when I press my button on Xcode?

I need to take the current value of mtype and pass it forward to Mselect so that the image pushed forward is the same as the one rotating in the animation.
here is the code sample
- (void)viewDidLoad
{
//Array to hold Images
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:Mush_Count];
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
//make button
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
Mselect = mtype;
}
}
-(IBAction)Selection:(id)sender{
[self Placement];
}
-(void)Placement{
if (Place1Occ == NO) {
[self Place1];
}
}
-(void)Place1{
if (Place1Occ == NO) {
Place1.image =[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", Mselect]];
Place1Occ = YES;
}
}
The animation loops just fine and the selection of the images works but it's not selecting the image the is currently on the screen it selects the last image on the Array.
Any suggestions?
for (mtype = 1; mtype < Mush_Count; mtype++)
{
[imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"Mush%i.png", mtype]]];
}
That loop is enough to build the array of images - you don't need to set the imageView's animation images until this loop completes and the array is populated.
As it stands, you reset the animation images each loop and set Mselect to type each iteration: this is why you always get the last image index stored in Mselect
This code should be after the for loop:
SelectBt.imageView.animationImages = [NSArray arrayWithArray:imageArray];
SelectBt.imageView.animationDuration = 5;
[SelectBt.imageView startAnimating];
As far as I know, you can't get the current frame of the animation directly - you have to create a timer when you start the animation and have this count the number of frames (by increasing your Mselect value. Then use that to load the current image
Is SelectBT a subclass of UIButton? if so is it the (id)sender coming from your IBAction?
If so then you can just grab the image directly from the SelectBT instance by calling sender.imageView.image
As an aside, objective-C convention is to capitalize ClassNames, but start instances with lowerCase like this ClassName *instanceOfClassName and you're likely to take flack for it around here if you don't adhere to that convention
Update
Could you bump your imageArray into a class variable or a property? It already contains all the fully loaded UIImages, which you can get back out by calling [imageArray objectAtIndex:i]
Then you just have to keep track of which numeric index is facing your user, or which index they tapped and pull the corresponding image.
Feel free to post your whole class if you'd like us to explain more.

Resources