iPhoneのaccelerometerの分かりやすいビデオがあったのでメモ。
http://www.switchonthecode.com/tutorials/iphone-tutorial-reading-the-accelerometer
x,y,zの三つの方向を、筐体の回転で取得するのが、こんなに簡単とは。
臆面もなくド素人ぶりを晒しながら、 iOS(Mac)上で実現したいものを創っていくための学習メモブログです。web上の参考資料は英語が多いので、英語の世界に迷い込んで試行錯誤。がむばろうニッポン。 (誤謬がありましたら、コメント等でお教え下さい)
2013年1月21日月曜日
Kobold2dのKKTouchPhaseのメモ
Kobold2dのKKTouchPhaseのまとめメモです。
以下、Kobold2dのリファレンスから。
http://www.learn-cocos2d.com/api-ref/KoboldTouch/6.0/Kobold2D/html/_k_k_input_enums_8h.html
以下、Kobold2dのリファレンスから。
http://www.learn-cocos2d.com/api-ref/KoboldTouch/6.0/Kobold2D/html/_k_k_input_enums_8h.html
| enum KKTouchPhase |
A touch can have just began, it can be moving, or it can be ended this frame. The kKKTouchPhaseAny can be used if want to include all three phases in a touch test. The KKTouchPhase enum values are equal to those in the UITouchPhase enum (except for any and lifted), that means they can be used interchangeably.
- Enumerator:
CCRemoveFromParentActionって?
Kobold2dのuser-inputプロジェクトのコードで、以下が気になったので、調査。
id remove = [CCRemoveFromParentAction action];
[CCRemoveFromParentAction ation];って文字通りの意味だろうけど、どうやって使うのか、と。
で、Kobold2dのKobold2d Cocos2d Extensionsの中にクラスが定義されていました。
@implementation CCRemoveFromParentAction
-(void) startWithTarget:(id)target
{
[super startWithTarget:target];[target removeFromParentAndCleanup:YES];}
@end
superで親クラスがstartWithTargetというメソッドを実装している様子。
で、startWithTargetは、以下のCocos2dリファレンス(CCAction Class Reference)に定義が書かれていました。
http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_action.html#a791b44f936b12d01e2aab5df8af41bfb
| - (void) startWithTarget: | (id) | target |
called before the action start. It will also set the target.
なるほど。
親クラスで、actionがスタートする前に呼び出され、CCActionのターゲットがsetされている、と。
で、removeFromParentAndCleanup:YESで、削除される、と。
まあ、文字通りの意味ですが、ちょっとすっきりしました。
CCの接頭辞ですが、どうもKobold2dのエクステンションのようですね。
CCMoveToとCCMoveBy
CCMoveToとCCMoveByの違いを理解するために、リファレンスを参照。
CCMoveByはCCMoveToを継承し、deltaPosition(フレームごとのポジション移動量?)が指定されている。
deltaはどうやら、frameの差分の様子。
参考1 (どこかの質疑応答)
参考2「Unity におけるフレームと Update」
CCMoveToは、特定地点(絶対位置)への移動。一度移動したら、そこで終了。
CCMoveByは、現在地からの相対的移動。絶対位置の指定ではなく。どんどん移動できる。現在地から、次の地点へ、同量の距離を。
ということらしい。
どんどんSpriteを動かす場合には、CCMoveByが便利。
CCMoveByはCCMoveToを継承し、deltaPosition(フレームごとのポジション移動量?)が指定されている。
deltaはどうやら、frameの差分の様子。
参考1 (どこかの質疑応答)
参考2「Unity におけるフレームと Update」
00134 @interface CCMoveTo : CCIntervalAction <NSCopying> 00135 { 00136 CGPoint endPosition; 00137 CGPoint startPosition; 00138 CGPoint delta; 00139 } 00141 +(id) actionWithDuration:(ccTime)duration position:(CGPoint)position; 00143 -(id) initWithDuration:(ccTime)duration position:(CGPoint)position; 00144 @end 00145 00150 @interface CCMoveBy : CCMoveTo <NSCopying> 00151 { 00152 } 00154 +(id) actionWithDuration: (ccTime)duration position:(CGPoint)deltaPosition; 00156 -(id) initWithDuration: (ccTime)duration position:(CGPoint)deltaPosition; 00157 @end
CCMoveToは、特定地点(絶対位置)への移動。一度移動したら、そこで終了。
CCMoveByは、現在地からの相対的移動。絶対位置の指定ではなく。どんどん移動できる。現在地から、次の地点へ、同量の距離を。
ということらしい。
どんどんSpriteを動かす場合には、CCMoveByが便利。
ccpSub、ccpLengthのメモ。
Cocos2dの「ccpSub」や「ccpLength」などの定義を知るために、リファレンスを見たので、メモ。
http://www.cocos2d-iphone.org/api-ref/0.99.0/_c_g_point_extension_8h.html
ccpLengthの定義は、みつからず。。
たぶん、ccpSubが二つのCGPointのx,yの差分を取るので、ccpLenghでその線分の長さを計算するんですね。
で、fabsfで絶対値として取得、と。
なるほど。
http://www.cocos2d-iphone.org/api-ref/0.99.0/_c_g_point_extension_8h.html
float kMoveDistance = fabsf(ccpLength(ccpSub(swipeEndPoint, input.gestureSwipeLocation))) * 4;
というKobold2dのuser-inputのプロジェクトソースを理解するため。
ccpSubの定義は、以下。
00085 ccpSub(const CGPoint v1, const CGPoint v2) 00086 { 00087 return ccp(v1.x - v2.x, v1.y - v2.y); 00088 }
ccpLengthの定義は、みつからず。。
たぶん、ccpSubが二つのCGPointのx,yの差分を取るので、ccpLenghでその線分の長さを計算するんですね。
で、fabsfで絶対値として取得、と。
なるほど。
2013年1月20日日曜日
Kobold2dのKKInput Touchesのドキュメントメモ
Kobold2dのKKInput Touchesについてのドキュメントを、メモ用にコピペします。
元記事はここ。
Kobold2dの「My-User-Input-Project」のソースを読んでいて、ドキュメントを読む必要を感じ、ついでにメモ的な日本語も少々付加いたしました。
(KKInput gestureに関してのメモは、こちら。)
元記事はここ。
Kobold2dの「My-User-Input-Project」のソースを読んでいて、ドキュメントを読む必要を感じ、ついでにメモ的な日本語も少々付加いたしました。
(KKInput gestureに関してのメモは、こちら。)
KKInput Touches
Enable Multi-Touch (マルチタッチをEnabledに)
To enable multiple touches, either set the config.lua setting EnableMultiTouch = YES or set themultipleTouchEnabled property:
[KKInput sharedInput].multipleTouchEnabled = YES; |
Simple Touch Began/Ended Tests (Touch開始、終了を簡単に取得。 anyTouchBeganThisFrame,anyTouchEndedThisFrame)
You can quickly test if any touch began or ended in the current frame with the following methods. This is a quick & easy way to, for example, create screens which the user can dismiss by touching the screen anywhere, or having the user "touch the screen to play again".
if ([KKInput sharedInput].anyTouchBeganThisFrame) { ... }if ([KKInput sharedInput].anyTouchEndedThisFrame) { ... } |
Touches Available (指がタッチ中か確認。touchesAvailable)
To test if touches are available, ie if any finger is touching the screen, use touchesAvailable:
if ([KKInput sharedInput].touchesAvailable) { ... } |
Did you just touch my Node??? (あるNodeをタッチしたか確認。isAnyTouchOnNode: touchPhase:)
You can determine if a node is being touched (oh my) with the isAnyTouchOnNode:touchPhase:method. You pass in the node like a CCSprite, CCLabelTTF, or any other CCNode derived object to test if any touch with the given phase (began, ended, any, etc) was on that node.(beganやendedやanyなどのphaseも指定できて、あるNodeのタッチを確認。)
The isAnyTouchOnNode method correctly performs the test even if the node is rotated, scaled or both.(nodeが回転したり、拡大縮小していても、isAnyTouchOnNodeは正しく動作します。)
KKInput* input = [KKInput sharedInput];if ([input isAnyTouchOnNode:someNode touchPhase:KKTouchPhaseBegan]) {...}if ([input isAnyTouchOnNode:someNode touchPhase:KKTouchPhaseAny]) {...} |
| Instances of CCNode by definition have a zero contentSize, so you can not use this method to test for touches on CCNode, unless you have modified the contentSize property of the CCNode object.(CCNodeのインスタンスは、contentSizeはゼロに定義されている。だから、contentSizeプロパティを修正しないと、このメソッドはつかえない。isAnyTouchOnNode。isAnyTouchOnNode: touchPhase:)
CCScene or CCLayer by definition have a contentSize that equals that of the screen/window size. You can test if a touch was on a CCScene or CCLayer object, but it makes little sense because if there is a touch on the screen, it will naturally be on the CCScene or CCLayer object — unless you have modified the contentSize property.
|
Location of Touches (タッチの位置取得。locationOfAnyTouchInPhase:メソッド。)
You can get the location of any touch with locationOfAnyTouchInPhase:. You can use one of the standard phases (began, moved, stationary, ended, cancelled) or use the special Kobold2D phaseKKTouchPhaseAny to test for the location of any touch in any phase.(phaseKKTouchPhaseAnyは、Kobold2dの特別なもので、どんなphaseのどんなtouchでも位置を取得できるとのこと。)
When is this useful? Whenever you don't care which finger is at that location, or if multiple touches are disabled you can get the location of the one (any) finger on the screen.
KKInput* input = [KKInput sharedInput];CGPoint pos = [input locationOfAnyTouchInPhase:KKTouchPhaseAny]; |
If no finger is currently touching the screen that is in the given phase, then CGPointZero is returned.(指がタッチしてなかったら、CGPointZeroが返される、と。)
Accessing all Touches (全てのtouchesにアクセス。)
The most powerful way to work with touches is to obtain the list of currently available touches via thetouches property, which is a CCArray* containing zero to five KKTouch* objects.(ゼロから5つのKKTouch*オブジェクトを格納したCCArray* touchesを使えるとのこと。)
KKTouch is a wrapper for UITouch that allows you to work with touches already converted to the Cocos2D view space(KKTouchはUITouchのラッパーで、Cocos2dのview spaceに既に転換されたタッチを使えるようにしたものです), and without having to use #ifdef if you also build for Mac(Mac用にビルドする際にも、#ifdefを使う必要がありません。). You can get the KKTouch* array and iterate over it like this:(KKTouch*配列を、以下のように取得して、繰り返すことが出来ます。)
CCArray* touches = [KKInput sharedInput].touches;//[KKInput sharedInput].touchesで取得し、CCArray *touchesに格納、と。KKTouch* touch;CCARRAY_FOREACH(touches, touch)//CCARRAY_FOREACHというメソッドがあるのですね。便利だ。{ // do something with touch here mySprite.position = touch.location;} |
| The KKTouch location and previousLocation properties are already converted to Cocos2D view coordinates. You do not need to (and shouldn't) call CCDirector's convertGL method. (KKTouchのlocationとpreviousLocationプロパティは、すでにCocos2dのview座標に転換されているので、CCDirectorのconvertGLメソッドを使う必要がなく、使ってはいけません。) |
Each KKTouch object corresponds to a particular finger on the screen. The same KKTouch object is used for the same finger starting when the touch "began" until the touch either "ended" or got "cancelled". ("ended"か"cancelled”になるまで、"began"によって開始されたtouchは、同じ指がタッチしている限り、KKTouchオブジェクトは維持されている、と。)
You can not rely on the array index for accessing a particular finger's KKTouch as other touches may end and get started(他のタッチが終わったり始まったりした場合は、配列中のインデックスで、特定のKKTouchにアクセスするのは信頼できません。). You can either store and later compare the touchID property to make sure you're using the same KKTouch as before, or simply keep a weak reference to the KKTouch object. In that case you have to check that it's phase property isn't KKTouchPhaseLifted, which indicates that the KKTouch object is no longer associated with a finger on the screen.(KKTouchPhaseLiftedは、すでに指が画面から離されていて、KKTouchオブジェクトが関連づけられていないというphase) You will also want to nil your reference when the touch phase is either KKTouchPhaseEnded or KKTouchPhaseLifted.
If you ever need access to the underlying UITouch* object, you can simply cast the touchID property:
// access the underlying UITouch object if neededUITouch* uiTouch = (UITouch*)touch.touchID; |
| Do not retain or otherwise hold on to UITouch* objects, they may contain invalid data in the next frame. Access them only through the touches property on a "as needed" basis. |
Removing a Touch (タッチを削除も出来る、と。)
As of Kobold2D v1.0.2 you can remove a touch to prevent other classes from handling the same touch. The finger associated with that KKTouch object will be ignored from then on.
CCArray* touches = [KKInput sharedInput].touches;KKTouch* touch;CCARRAY_FOREACH(touches, touch){ // remove touch [[KKInput sharedInput] removeTouch:touch];} |
It is legal to remove touches while iterating over the touches array. The touch is first invalidated (KKTouchPhase is set to KKTouchPhaseLifted among other things) and later removed.
登録:
投稿 (Atom)