Created
December 9, 2013 09:00
-
-
Save kankikuchi/7869368 to your computer and use it in GitHub Desktop.
バーチャルパッド(アナログスティック) cocos2d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//タッチ開始 | |
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ | |
//メニュー画面が開いていない,操作キャラが死んでいない,ゲームが終わっていない | |
if (TopMenu.enabled && !MainPlyaer.DeathFlag && !GameOverFlag) { | |
TouchFlag = YES;//タッチ状態か否かのフラグをオン | |
//タッチ位置 | |
CGPoint TouchPos = [touch locationInView: [touch view]];//タッチされた位置 | |
CGPoint ConvertTouchPoint = [[CCDirector sharedDirector] convertToGL:TouchPos];//タッチされた位置を変換 | |
//ボタン初期化 表示 | |
FastTouchPoint = ConvertTouchPoint;//タッチ開始位置 | |
Button.position = ccp(ButtonBack.contentSize.width/2,ButtonBack.contentSize.height/2);//ボタンの位置をボタン台の中央に戻す | |
ButtonBack.scale = 1;//ボタン表示 | |
ButtonBack.position = FastTouchPoint;//ボタン台の位置をタッチ開始点に | |
} | |
//YESを返したタッチイベントのみMoved等が実行される | |
return YES; | |
} | |
//タッチした指が移動 | |
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{ | |
//タッチ位置 | |
CGPoint TouchPos = [touch locationInView: [touch view]];//移動後の指の位置 | |
CGPoint ConvertTouchPoint = [[CCDirector sharedDirector] convertToGL:TouchPos];//移動後の指の位置を変換 | |
//最初にタッチした地点から | |
CGPoint Distance = ccp(ConvertTouchPoint.x - FastTouchPoint.x, ConvertTouchPoint.y - FastTouchPoint.y); | |
float MaxMoveRate = ButtonBack.contentSize.width * MaxMoveBottonRate;//最大移動量量 MaxMoveBottonRateで移動範囲を調整 | |
//正方向と負方向どちらに合わせて移動量の制限 | |
if (Distance.x > 0) { | |
Distance.x = MIN(Distance.x, MaxMoveRate); | |
} | |
else if (Distance.x < 0) { | |
Distance.x = MAX(Distance.x, -MaxMoveRate); | |
} | |
if (Distance.y > 0) { | |
Distance.y = MIN(Distance.y, MaxMoveRate); | |
} | |
else if (Distance.y < 0) { | |
Distance.y = MAX(Distance.y, -MaxMoveRate); | |
} | |
//円範囲に移動を制限 | |
if (sqrt(Distance.x * Distance.x + Distance.y * Distance.y) > MaxMoveRate) { | |
//角度 | |
float Angle = atan2f(Distance.y, Distance.x); | |
//制限後のボタン位置 | |
Distance.x = MaxMoveRate * cos(Angle); | |
Distance.y = MaxMoveRate * sin(Angle); | |
} | |
//ボタン移動 | |
Button.position = ccp(ButtonBack.contentSize.width * 0.5f + Distance.x , ButtonBack.contentSize.height * 0.5f + Distance.y); | |
} | |
//タッチ終了 | |
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{ | |
ButtonBack.scale = 0;//ボタン非表示 | |
TouchFlag = NO;//タッチ状態か否かのフラグをオフ | |
} | |
//タッチキャンセル | |
-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event{ | |
ButtonBack.scale = 0;//ボタン非表示 | |
TouchFlag = NO;//タッチ状態か否かのフラグをオフ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment