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 01-09-2010, 12:32 PM   #1 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default Hopping animation with UIView CommitAnimations?

ok, so im trying to make a image do a hopping motion by animating wit UIView. Heres what i have so far:

Code:
[UIView beginAnimations:@"Move Puppy" context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:speed];
	
	
	image.center = CGPointMake(image.center.x - 10, image.center.y - 10);
	
	[UIView commitAnimations];
This will work for making the image go up and to the left, but how do i get it to go back don and to the left?

Last edited by justill45; 01-10-2010 at 10:05 AM.
justill45 is offline   Reply With Quote
Old 01-10-2010, 03:18 AM   #2 (permalink)
I like pizza
 
Picoman's Avatar
 
Join Date: Jul 2009
Location: Scotland
Age: 27
Posts: 321
Picoman is on a distinguished road
Default

Have you tried putting YES instead of TRUE?
The docs use YES/NO for setAnimationRepeatAutoreverses.
__________________
Picoman is offline   Reply With Quote
Old 01-10-2010, 10:05 AM   #3 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by Picoman View Post
Have you tried putting YES instead of TRUE?
The docs use YES/NO for setAnimationRepeatAutoreverses.
oops, i didnt even mean to put that in there, even if it did autoreverse, it would just go back to the original position.
justill45 is offline   Reply With Quote
Old 01-10-2010, 12:48 PM   #4 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

Do you want it to just jump up and down as if there's no friction i.e. it bounces up and down at the same height throughout? If so, then based on the level of the table it's bouncing off, set the y velocity to negate whenever it the bottom level's y value, and when it hits 'near' where you want it to start going down, the y velocity continues to go down until it's negative of what it was going up

Bit complicated to write, read it a few times if you must
MiniRobinho is offline   Reply With Quote
Old 01-10-2010, 02:56 PM   #5 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by MiniRobinho View Post
Do you want it to just jump up and down as if there's no friction i.e. it bounces up and down at the same height throughout? If so, then based on the level of the table it's bouncing off, set the y velocity to negate whenever it the bottom level's y value, and when it hits 'near' where you want it to start going down, the y velocity continues to go down until it's negative of what it was going up

Bit complicated to write, read it a few times if you must
so in simpler terms are you saying that when it hits the bottom of where you want it, make it go up, and when you hit the top, make it go down?


how would i do something like this?
justill45 is offline   Reply With Quote
Old 01-11-2010, 12:50 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

Quote:
Originally Posted by justill45 View Post
so in simpler terms are you saying that when it hits the bottom of where you want it, make it go up, and when you hit the top, make it go down?


how would i do something like this?
Not quite, because when it bounces at the bottom it goes straight back up doesn't it? But when it goes up it doesn't suddenly go down at the same angle, it slowly stops going up and starts going down.

What you want to do is (note that I'm considering the ball center is the normal ball center):

Code:
//When the ball is touching the bottom of the ground, y velocity inverses
if(image.center.y - ballRadius == ground.y) {
ballVelocity.y = -ballVelocity.y;
}

//When ball is more 50 pixels off the ground, it starts to slow down and go down
if(image.center.y - ballRadius - 50 > ground.y) {
ballVelocity.y = ballVelocity.y - (image.center.y - ballRadius - 50 - ground.y)
}
What I've done in the second part is make the ballVelocity.y go down by the amount of difference between its current height and 50 pixels above the ground. This should work however I'm not sure how quick it updates so it might keep updating so quickly that it immediately goes down. You're gonna have to check this
MiniRobinho is offline   Reply With Quote
Old 01-11-2010, 05:01 PM   #7 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by MiniRobinho View Post
Not quite, because when it bounces at the bottom it goes straight back up doesn't it? But when it goes up it doesn't suddenly go down at the same angle, it slowly stops going up and starts going down.

What you want to do is (note that I'm considering the ball center is the normal ball center):

Code:
//When the ball is touching the bottom of the ground, y velocity inverses
if(image.center.y - ballRadius == ground.y) {
ballVelocity.y = -ballVelocity.y;
}

//When ball is more 50 pixels off the ground, it starts to slow down and go down
if(image.center.y - ballRadius - 50 > ground.y) {
ballVelocity.y = ballVelocity.y - (image.center.y - ballRadius - 50 - ground.y)
}
What I've done in the second part is make the ballVelocity.y go down by the amount of difference between its current height and 50 pixels above the ground. This should work however I'm not sure how quick it updates so it might keep updating so quickly that it immediately goes down. You're gonna have to check this
i could make a new timer, how fast of a timer would work well with this?
justill45 is offline   Reply With Quote
Old 01-12-2010, 03:05 PM   #8 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

Quote:
Originally Posted by justill45 View Post
i could make a new timer, how fast of a timer would work well with this?
I don't have a clue, test yourself You don't really need to ask that question do you? Just put in a value and see how it works. Best to start with a slow timer so that it's obvious whether it's working, then bring the time down as you see necessary.
MiniRobinho is offline   Reply With Quote
Old 01-12-2010, 03:42 PM   #9 (permalink)
Shmoopi LLC
 
Shmoopi's Avatar
 
Join Date: Jun 2009
Location: Virginia
Posts: 213
Shmoopi is on a distinguished road
Default

Quote:
Originally Posted by justill45 View Post
ok, so im trying to make a image do a hopping motion by animating wit UIView. Heres what i have so far:

Code:
[UIView beginAnimations:@"Move Puppy" context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:speed];
	
	
	image.center = CGPointMake(image.center.x - 10, image.center.y - 10);
	
	[UIView commitAnimations];
This will work for making the image go up and to the left, but how do i get it to go back down and to the left?
What I would do is simply reverse the image movement so it goes up and down over the timer in the animation. Something like this:
Code:
[UIView beginAnimations:@"Move Puppy" context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:speed];
	
	
	image.center = CGPointMake(image.center.x - 10, image.center.y - 10);
        image.center = CGPointMake(image.center.x + 10, image.center.y + 10);
	
	[UIView commitAnimations];
Shmoopi is offline   Reply With Quote
Old 01-12-2010, 03:44 PM   #10 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by Shmoopi View Post
What I would do is simply reverse the image movement so it goes up and down over the timer in the animation. Something like this:
Code:
[UIView beginAnimations:@"Move Puppy" context:nil];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDuration:speed];
	
	
	image.center = CGPointMake(image.center.x - 10, image.center.y - 10);
        image.center = CGPointMake(image.center.x + 10, image.center.y + 10);
	
	[UIView commitAnimations];
UIView animates everything at the same time, that code would make nothing happen since they cancel eachother out
justill45 is offline   Reply With Quote
Old 01-12-2010, 03:49 PM   #11 (permalink)
Shmoopi LLC
 
Shmoopi's Avatar
 
Join Date: Jun 2009
Location: Virginia
Posts: 213
Shmoopi is on a distinguished road
Default

Quote:
Originally Posted by justill45 View Post
UIView animates everything at the same time, that code would make nothing happen since they cancel eachother out
Haha, good point. Guess I'm just a little off my game today. But you could put the reverse into the animationdidfinish method if that's what you wanted.
Shmoopi is offline   Reply With Quote
Old 01-12-2010, 04:14 PM   #12 (permalink)
Shmoopi LLC
 
Shmoopi's Avatar
 
Join Date: Jun 2009
Location: Virginia
Posts: 213
Shmoopi is on a distinguished road
Default

Maybe something like this:
Code:
        *Add this to your "Move Puppy" Animation:
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context {
	if (animationID == @"Move Puppy") {
		[UIView beginAnimations:@"Move Puppy Back" context:nil];
		[UIView setAnimationDelegate:self];
		[UIView setAnimationDuration:1.0];
		
		image.center = CGPointMake(image.center.x + 10, image.center.y + 10);
		
		[UIView commitAnimations];
	}
}
Shmoopi is offline   Reply With Quote
Old 01-13-2010, 03:38 PM   #13 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 159
MiniRobinho is on a distinguished road
Default

Quote:
Originally Posted by Shmoopi View Post
Maybe something like this:
Code:
        *Add this to your "Move Puppy" Animation:
        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context {
	if (animationID == @"Move Puppy") {
		[UIView beginAnimations:@"Move Puppy Back" context:nil];
		[UIView setAnimationDelegate:self];
		[UIView setAnimationDuration:1.0];
		
		image.center = CGPointMake(image.center.x + 10, image.center.y + 10);
		
		[UIView commitAnimations];
	}
}
He doesn't want to go forwards and then back to its original place, he wants it to hop, so constantly go forward, so the image.center.y value (seeing as it seems hes working in LandscapeLeft so y value is horizontal) should stay as -10 in the second animation too, and only image.center.x go to +10. However that would make it only move in diagonals whereas slowly changing the x value will make it look more like a hop.

I just realised you're still trying to do it through UIView animations. I think for the transition stage you need to have set where there is some sort of ballVelocity.y set so that you can make it look more realistic by going up then slowing down and coming back down again.
MiniRobinho is offline   Reply With Quote
Old 01-13-2010, 03:54 PM   #14 (permalink)
Beast Iphone Developor
 
justill45's Avatar
 
Join Date: Aug 2009
Location: Atlanta, Georgia
Age: 16
Posts: 1,302
justill45 is on a distinguished road
Default

Quote:
Originally Posted by MiniRobinho View Post
He doesn't want to go forwards and then back to its original place, he wants it to hop, so constantly go forward, so the image.center.y value (seeing as it seems hes working in LandscapeLeft so y value is horizontal) should stay as -10 in the second animation too, and only image.center.x go to +10. However that would make it only move in diagonals whereas slowly changing the x value will make it look more like a hop.

I just realised you're still trying to do it through UIView animations. I think for the transition stage you need to have set where there is some sort of ballVelocity.y set so that you can make it look more realistic by going up then slowing down and coming back down again.
hmm, im really not sure how to do that though
justill45 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: 336
15 members and 321 guests
akacaj, alexP, ClerurcifeDer, Domele, Duncan C, givensur, GraffitiCircus, JmayLive, michelle, NetGuru, NSString, Paul Slocum, Sloshmonster, soohyun, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

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