Skip to content

Instantly share code, notes, and snippets.

@20m61
Created November 13, 2012 02:01
Show Gist options
  • Save 20m61/4063471 to your computer and use it in GitHub Desktop.
Save 20m61/4063471 to your computer and use it in GitHub Desktop.
balance_121112.m
//
// ViewController.m
// ball
//
// Created by changhwi on 12/11/12.
// Copyright (c) 2012年 changhwi. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize ball;
@synthesize flash;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//ballの初期値
//ballの初期位置(position
posX = 160.0;
posY = 230.0;
//ballの初期速度(speed
speedX = 0.0;
speedY = 0.0;
//ballの初期加速度(acsel
acselsX = 0.0;
acselsY = 0.0;
acselsZ = 0.0;
//加速度センサー
//加速度センサーの値を参照
UIAccelerometer* acsel = [UIAccelerometer sharedAccelerometer];
//加速度センサーの値を参照する間隔
acsel.updateInterval = 0.2; //ここの値で動きが変わった。
//加速度センサーの値を受け取るデリゲートを自分自身に設定
acsel.delegate = self;
//効果音
NSString *path = [[NSBundle mainBundle] pathForResource:@"Beep" ofType:@"caf"];
NSURL *url = [NSURL fileURLWithPath:path];AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
acselsX = acceleration.x;
acselsY = acceleration.y;
acselsZ = acceleration.z;
[self move];
NSLog(@"X");
NSLog(@"%f", acselsX);
NSLog(@"Y");
NSLog(@"%f", acselsY);
}
- (void)move
{
speedX = speedX + acselsX;
speedY = speedY - acselsY;
posX = posX + speedX;
posY = posY + speedY;
if (posX <= 60) {
speedX *= -1;
posX = 60 + (60 - posX);
[self bamp];
}
if (posX >= 260) {
speedX *= -1;
posX = 260 - (posX - 260);
[self bamp];
}
if (posY <= 60) {
speedY *= -1;
posY = 60 + (60 - posY);
[self bamp];
}
if (posY >= 400) {
speedY *= -1;
posY = 400 - (posY - 400);
[self bamp];
}
ball.center = CGPointMake(posX, posY);
}
- (void)bamp
{
AudioServicesPlaySystemSound(soundID);
flash.alpha = 1.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
flash.alpha = 0.0;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment