プログラマメモ2 - programmer no memo2

文字列からセレクターを作成する - NSSelectorFromString 2010/08/28

Objective-Cです。NSSelectorFromStringです。
文字列からセレクターを作成できます。

いろいろな生成ルーチンをランダムに呼ぶにはどうしようかなーと考えていて、ひとつの案として文字列からセレクタを作成して、
呼び出すという方法が浮かびました。

参考



以下コード
#import <Foundation/NSArray.h>
#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>

@interface MyObj : NSObject {

}
- a1;
- a2;
- a3;
@end

@implementation MyObj

- a1
{
NSLog(@"** a1");
}
- a2
{
NSLog(@"** a2");
}
- a3
{
NSLog(@"** a3");
}
@end

int main(){

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

id myObj = [MyObj alloc];

// NSSelectorFromStringを使ってるよ
SEL sel1 = NSSelectorFromString(@"a1");

// performSelectorを使ってるよ
[myObj performSelector:sel1];
[myObj performSelector:NSSelectorFromString(@"a2")];
[myObj performSelector:NSSelectorFromString(@"a3")];

[pool drain];
return 0;
}

Objective-Cでらんすう - 初期化がいらないarc4random() だそうです。 2010/08/27

Objective-Cです。といってもObjective-Cにかぎったものではないですが。。。
乱数が欲しいと思いました。それで、さがしたらarc4random()という関数がありました。
osx上で、man arc4randomするとでてきます。




arc4random() % n

isKindOfClass - 特定のクラスのインスタンスかどうかを調べる 2010/08/25

Objective-Cです。あるインスタンスが、特定のクラスのインスタンスかどうかを調べるときにisKindOfClassを使います。

ところで、今、cocos2dでレイヤーのaddしたspriteのクラスによって、何かしたいというときに使えるかなーと試しているところです。

id arr = [layer children];
for (id o in arr) {
if ([o isKindOfClass:[MySprite class]]) {
// NSLog(@"%@", [o description]);
} else {
}
}

objective-Cでしんぐるとん 2010/08/23

Objective-Cです。デザインパターンのシングルトンです。
グーグルさんで調べればいろいろでてきますが、とりあえず。


@implementation Manager

/*
シングルトンの部分のコードは、
「シングルトンインスタンスの作成」
http://developer.apple.com/jp/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html#//apple_ref/doc/uid/TP40002974-CH4-97333-CJBDDIBI

*/
static Manager *sharedManager = nil;

+ (Manager*)sharedManager
{

@synchronized(self) {
if (sharedManager == nil) {
[[self alloc] init]; // ここで代入してはだめ
}
}

return sharedManager;
}



+ (id)allocWithZone:(NSZone *)zone
{

@synchronized(self) {

if (sharedManager == nil) {

sharedManager = [super allocWithZone:zone];
return sharedManager; // 最初の割り当てで代入し、返す
}
}

return nil; // 以降の割り当てではnilを返すようにする
}

- (id)copyWithZone:(NSZone *)zone
{
return self;
}

- (id)retain
{
return self;
}

-(unsigned)retainCount
{
return UINT_MAX; // 解放できないオブジェクトであることを示す
}

- (void)release
{

}

- (id)autorelease
{
return self;
}

@end

// こんなの用意してみました。
Manager* mng()
{
return [Manager sharedManager];
}

cocos2dではNSTimerは使わない。 2010/08/23

cocos2dです。

スケジュールさせた時間にスプライトを動かすのどうするんだろうーということで。



いきなりNSTimerを使うコメントの評価が -8になっていました(現時点)。
なるほどcocos2dでは使ってはいけないのだなーと。

スケージュール実行はcocos2d frameworkが用意してくれてる仕組みを利用します。

スケジュールはinitで
// schedule timer
[self schedule: @selector(ticktack:)];
[self schedule: @selector(ticktack2:) interval:0.5];


で感じで定義して、
したのような関数をよういすれば、定期的によばれます。

-(void) ticktack: (ccTime) dt
{

}

フットサルのコートを描いてみる 2010/08/21



Objective-Cです。
フットサルのコートのラインを描いてます。
ペナルティエリアの部分はちょっと手をぬいてます。
iPad向けを意識してます。比率は、一応フットサルの公式ルールブックにある大きさ通りかなと思います。

iPad向けのスポーツの戦略ボードは、有料、無料向けを含めて乱立してますね。
もう乗り遅れてしまった感がありありですが....

コートをコードで描いてるコードです。

/**

*/
UIImage* createField1(int w, int h, UIColor *color)
{

float L_W = 5;
const int OFFSET = 50;
UIImage *retImage;

UIGraphicsBeginImageContext(CGSizeMake(w, h));
CGContextRef context = UIGraphicsGetCurrentContext();

{
CGContextSetShouldAntialias(context, 0);
// アンチエイリアスのためのコード
CGContextSetAllowsAntialiasing(context, true);
CGContextSetShouldAntialias(context, true);
}

CGContextBeginPath(context);

{


{// BOX
{
CGContextSetLineWidth(context,L_W);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextSetRGBFillColor(context,255,255,255,1);
CGContextSetRGBStrokeColor(context,255,255,255,1);
}

CGContextMoveToPoint(context, 0 + OFFSET, h - OFFSET);
CGContextAddLineToPoint(context, 0 + OFFSET, 0 + OFFSET);
CGContextAddLineToPoint(context, w - OFFSET, 0 + OFFSET);
CGContextAddLineToPoint(context, w - OFFSET, h - OFFSET);
CGContextClosePath(context);
CGContextStrokePath(context);

// center line
CGContextMoveToPoint(context, w / 2, 0 + OFFSET);
CGContextAddLineToPoint(context, w / 2, h - OFFSET);
CGContextStrokePath(context);
}



{// center circle
{
CGContextSetLineWidth(context,L_W);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextSetRGBFillColor(context,255,255,255,1);
CGContextSetRGBStrokeColor(context,255,255,255,1);
}


CGContextAddArc(context, w/2, h/2, (28*3), radians(0), radians(360), 0); // 円弧
//CGContextAddArc(context, 45, 200, 36, radians(25), radians(65), 1); // 円に近いもの
CGContextStrokePath(context);

{ // point
CGContextAddArc(context, w/2, h/2, L_W, M_PI*2.0, 0.0, 0);
//CGContextAddRect(context, CGRectMake(0, 0, d, d));
CGContextDrawPath(context, kCGPathFill);
}
}
{// left(up)

{// penalty area


CGContextAddArc(context, 0 + OFFSET, 342, (28*6), radians(-90), radians(0), 0); // 円弧
CGContextStrokePath(context);
CGContextAddArc(context, 0 + OFFSET, 426, (28*6), radians(90), radians(0), 1); // 円弧
CGContextMoveToPoint(context, (28*6) + OFFSET, 342);
CGContextAddLineToPoint(context, (28*6) + OFFSET, 426);
CGContextStrokePath(context);
}

{//goal
CGContextMoveToPoint(context, 0 + OFFSET, 342);
CGContextAddLineToPoint(context, 0 + (OFFSET - (OFFSET - 5)), 342);
CGContextAddLineToPoint(context, 0 + (OFFSET - (OFFSET - 5)), 426);
CGContextAddLineToPoint(context, 0 + OFFSET, 426);
CGContextStrokePath(context);
}

{ // penalty mark
CGContextAddArc(context, 0 + OFFSET + (28*6), h/2, L_W, M_PI*2.0, 0.0, 0);
//CGContextAddRect(context, CGRectMake(0, 0, d, d));
CGContextDrawPath(context, kCGPathFill);

// second penalty mark
CGContextAddArc(context, 0 + OFFSET + (28*10), h/2, L_W, M_PI*2.0, 0.0, 0);
//CGContextAddRect(context, CGRectMake(0, 0, d, d));
CGContextDrawPath(context, kCGPathFill);
}
}
{// right(down)

{// penalty area


CGContextAddArc(context, w - OFFSET, 342, (28*6), radians(180), radians(-90), 0); // 円弧
CGContextStrokePath(context);
CGContextAddArc(context, w - OFFSET, 426, (28*6), radians(180), radians(90), 1); // 円弧
CGContextMoveToPoint(context, w - ((28*6) + OFFSET), 342);
CGContextAddLineToPoint(context, w - ((28*6) + OFFSET), 426);
CGContextStrokePath(context);
}

{//goal
CGContextMoveToPoint(context, w - OFFSET, 342);
CGContextAddLineToPoint(context, w - (OFFSET - (OFFSET - 5)), 342);
CGContextAddLineToPoint(context, w - (OFFSET - (OFFSET - 5)), 426);
CGContextAddLineToPoint(context, w - OFFSET, 426);
CGContextStrokePath(context);
}

{ // penalty mark
CGContextAddArc(context, w - OFFSET - (28*6), h/2, L_W, M_PI*2.0, 0.0, 0);
//CGContextAddRect(context, CGRectMake(0, 0, d, d));
CGContextDrawPath(context, kCGPathFill);

// second penalty mark
CGContextAddArc(context, w - OFFSET - (28*10), h/2, L_W, M_PI*2.0, 0.0, 0);
//CGContextAddRect(context, CGRectMake(0, 0, d, d));
CGContextDrawPath(context, kCGPathFill);
}
}


}

retImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[retImage autorelease];
[retImage retain];

return retImage;

}

PIってどこにいるのさ 2010/08/15

Objective-Cです。
PIってどこにいるのさ、というわけでmath.hにいます。


#define M_PI 3.14159265358979323846264338327950288 /* pi */

というふうに定義されています。

他にもぞろぞろ定義されているので目を通しておくとよいのだろう。