More Related Content
More from John Wilker (20)
Making things Move on the iPhone
- 7. Background
2005
2007
2008
Wednesday, March 4, 2009
- 17. Coded Animation
Apply a
rule
Update Change
the screen something
Wednesday, March 4, 2009
- 18. Coded Animation
Apply a
rule
Update Change
the screen something
Wednesday, March 4, 2009
- 21. Make something
to move
(project files: Animation101)
Wednesday, March 4, 2009
- 22. // interface
UIImageView *ball;
// viewDidLoad
ball = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@quot;ball.pngquot;]];
[self.view addSubview:ball];
Wednesday, March 4, 2009
- 24. NSTimer
[NSTimer
scheduledTimerWithTimeInterval:1.0/60.0
target:self
selector:@selector(onTimer)
userInfo:nil
repeats:YES];
Wednesday, March 4, 2009
- 27. Moving UIViews
• view.center
• view.transform
Wednesday, March 4, 2009
- 28. Moving UIViews
• view.center
• view.transform
• view.frame
Wednesday, March 4, 2009
- 29. // interface
float x;
float y;
// viewDidLoad
x = 50.0;
y = 50.0;
Wednesday, March 4, 2009
- 32. ball.transform =
CGAffineTransformMakeTranslation(x, y);
x += 2.0;
y += 3.0;
Wednesday, March 4, 2009
- 33. ball.transform =
CGAffineTransformTranslate(
ball.transform, 2.0, 3.0);
Wednesday, March 4, 2009
- 36. Which is fastest?
• view.center
• view.transform (~2-3x)
Wednesday, March 4, 2009
- 37. Which is fastest?
• view.center
• view.transform (~2-3x)
• view.frame (~2-3x)
Wednesday, March 4, 2009
- 38. Velocity
(project files:Velocity)
Wednesday, March 4, 2009
- 39. Velocity
+
(project files:Velocity)
Wednesday, March 4, 2009
- 40. speed
and
direction
Wednesday, March 4, 2009
- 41. speed
and
direction
y velocity
x velocity
Wednesday, March 4, 2009
- 42. - y velocity
- x velocity + x velocity
+ y velocity
Wednesday, March 4, 2009
- 45. // interface:
float vx;
float vy;
// viewDidLoad:
vx = 2.0;
vy = 3.0;
Wednesday, March 4, 2009
- 46. // interface:
float vx;
float vy;
// viewDidLoad:
vx = 2.0;
vy = 3.0;
// onTimer:
ball.center = CGPointMake(x, y);
x += vx;
y += vy;
Wednesday, March 4, 2009
- 47. speed
angle
(project files: AngularVelocity)
Wednesday, March 4, 2009
- 48. y velocity
speed =
sin(angle)
x
speed
angle
x velocity = cos(angle) x speed
(project files: AngularVelocity)
Wednesday, March 4, 2009
- 50. // interface:
float angle;
float speed;
Wednesday, March 4, 2009
- 51. // interface: // viewDidLoad:
float angle; angle = 45.0;
float speed; speed = 4.0;
Wednesday, March 4, 2009
- 52. // interface: // viewDidLoad:
float angle; angle = 45.0;
float speed; speed = 4.0;
// onTimer:
ball.center = CGPointMake(x, y);
x += cos(angle * M_PI / 180.0) * speed;
y += sin(angle * M_PI / 180.0) * speed;
Wednesday, March 4, 2009
- 53. Acceleration
(project files: Acceleration)
Wednesday, March 4, 2009
- 54. Acceleration
+
(project files: Acceleration)
Wednesday, March 4, 2009
- 57. // interface: // viewDidLoad:
float ax; ax = 0.07;
float ay; ay = 0.1;
Wednesday, March 4, 2009
- 58. // interface: // viewDidLoad:
float ax; ax = 0.07;
float ay; ay = 0.1;
// onTimer:
ball.center = CGPointMake(x, y);
x += vx;
y += vy;
vx += ax;
vy += ay;
Wednesday, March 4, 2009
- 59. Bouncing
(project files: Bouncing)
Wednesday, March 4, 2009
- 61. -x velocity y velocity
y velocity
x velocity
Wednesday, March 4, 2009
- 64. // interface:
float bounce;
// viewDidLoad:
bounce = -1.0;
Wednesday, March 4, 2009
- 65. ball.center = CGPointMake(x, y);
x += vx;
y += vy;
if(x > 300) // 320 - radius (20)
{
x = 300;
vx *= bounce;
}
else if(x < 20) // 0 + radius
{
x = 20;
vx *= bounce;
}
Wednesday, March 4, 2009
- 66. if(y > 440) // 460 - radius
{
y = 440;
vy *= bounce;
}
else if(y < 20) // 0 + radius
{
y = 20;
vy *= bounce;
}
Wednesday, March 4, 2009
- 67. Gravity
acceleration
(+y)
Wednesday, March 4, 2009
- 70. // interface:
float gravity;
// viewDidLoad:
gravity = 0.5;
Wednesday, March 4, 2009
- 71. // interface:
float gravity;
// viewDidLoad:
gravity = 0.5;
// onTimer:
vy += gravity;
Wednesday, March 4, 2009
- 74. @interface GravityViewController :
UIViewController <UIAccelerometerDelegate>
{
UIImageView *ball;
float x;
float y;
float vx;
float vy;
float bounce;
CGPoint gravity;
}
Wednesday, March 4, 2009
- 76. // viewDidLoad
gravity = CGPointZero;
[[UIAccelerometer sharedAccelerometer]
setDelegate:self];
Wednesday, March 4, 2009
- 77. // viewDidLoad
gravity = CGPointZero;
[[UIAccelerometer sharedAccelerometer]
setDelegate:self];
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
gravity = CGPointMake(acceleration.x,
-acceleration.y);
}
Wednesday, March 4, 2009
- 78. // viewDidLoad
gravity = CGPointZero;
[[UIAccelerometer sharedAccelerometer]
setDelegate:self];
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
gravity = CGPointMake(acceleration.x,
-acceleration.y);
}
// onTimer
vx += gravity.x;
vy += gravity.y;
Wednesday, March 4, 2009
- 83. // interface // viewDidLoad
BOOL dragging; dragging = NO;
Wednesday, March 4, 2009
- 84. // interface // viewDidLoad
BOOL dragging; dragging = NO;
- (void)onTimer
{
if(!dragging)
{
...
}
}
Wednesday, March 4, 2009
- 85. - (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
float dx = point.x - x;
float dy = point.y - y;
float dist = sqrt(dx * dx + dy * dy);
if(dist < 20)
{
dragging = YES;
x = point.x;
y = point.y;
vx = 0;
vy = 0;
}
}
Wednesday, March 4, 2009
- 87. - (void)touchesMoved:(NSSet *)touches
withEvent:(UIEvent *)event
{
if(dragging)
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
vx = point.x - x;
vy = point.y - y;
x = point.x;
y = point.y;
ball.center = point;
}
}
Wednesday, March 4, 2009
- 88. Thank you
• Keith Peters
• kp@bit-101.com
• www.bit-101.com
• www.wickedpissahgames.com
• www.bit-101.com/360iDev/presentation.zip
Wednesday, March 4, 2009