2011年6月30日 星期四

NSMutableArray sort NSString , NSNumber

//內容都是nsstring的
//


NSLog(@"**************************************************");
//內容都是nsstring的
NSMutableArray *arrData = [NSMutableArray new];
[arrData addObject:@"A4"];
[arrData addObject:@"A2"];
[arrData addObject:@"a2"];
[arrData addObject:@"A2"];
[arrData addObject:@"A1"];

NSLog(@"------------- original:");
for (id obj in arrData) NSLog(@"arrData:%@", obj);

NSLog(@"------------- sorted:");
NSArray * sortedData =
[arrData sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
for (id obj in sortedData) NSLog(@"sortedData:%@", obj);
NSLog(@"------------- sorted:");
NSArray * sortedData2 =
[arrData sortedArrayUsingSelector:@selector(localizedCompare:)];
for (id obj in sortedData2) NSLog(@"sortedData2:%@", obj);

output:
2011-07-02 11:38:29.906 testCode[449:207] **************************************************
2011-07-02 11:38:29.907 testCode[449:207] ------------- original:
2011-07-02 11:38:29.908 testCode[449:207] arrData:A4
2011-07-02 11:38:29.908 testCode[449:207] arrData:A2
2011-07-02 11:38:29.908 testCode[449:207] arrData:a2
2011-07-02 11:38:29.909 testCode[449:207] arrData:A2
2011-07-02 11:38:29.909 testCode[449:207] arrData:A1
2011-07-02 11:38:29.910 testCode[449:207] ------------- sorted:
2011-07-02 11:38:29.910 testCode[449:207] sortedData:A1
2011-07-02 11:38:29.911 testCode[449:207] sortedData:A2
2011-07-02 11:38:29.911 testCode[449:207] sortedData:a2
2011-07-02 11:38:29.911 testCode[449:207] sortedData:A2
2011-07-02 11:38:29.911 testCode[449:207] sortedData:A4
2011-07-02 11:38:29.912 testCode[449:207] ------------- sorted:
2011-07-02 11:38:29.914 testCode[449:207] sortedData2:A1
2011-07-02 11:38:29.914 testCode[449:207] sortedData2:a2
2011-07-02 11:38:29.915 testCode[449:207] sortedData2:A2
2011-07-02 11:38:29.915 testCode[449:207] sortedData2:A2
2011-07-02 11:38:29.915 testCode[449:207] sortedData2:A4



//內容都是NSNumber的

NSLog(@"**************************************************");
//內容都是NSNumber的
NSMutableArray *arrNum = [NSMutableArray new];
[arrNum addObject:[[NSNumber alloc] initWithInt:5]];
[arrNum addObject:[[NSNumber alloc] initWithInt:1]];
[arrNum addObject:[[NSNumber alloc] initWithInt:3]];
[arrNum addObject:[[NSNumber alloc] initWithInt:2]];
NSLog(@"------------- original:");
for (id obj in arrNum) NSLog(@"arrNum:%@", obj);
NSLog(@"------------- sorted:");
NSArray * sortedNum = [arrNum sortedArrayUsingSelector:@selector(compare:)];
for (id obj in sortedNum) NSLog(@"sortedNum:%@", obj);



output:
2011-07-02 11:38:29.915 testCode[449:207] **************************************************
2011-07-02 11:38:29.929 testCode[449:207] ------------- original:
2011-07-02 11:38:29.929 testCode[449:207] arrNum:5
2011-07-02 11:38:29.930 testCode[449:207] arrNum:1
2011-07-02 11:38:29.930 testCode[449:207] arrNum:3
2011-07-02 11:38:29.930 testCode[449:207] arrNum:2
2011-07-02 11:38:29.931 testCode[449:207] ------------- sorted:
2011-07-02 11:38:29.931 testCode[449:207] sortedNum:1
2011-07-02 11:38:29.931 testCode[449:207] sortedNum:2
2011-07-02 11:38:29.931 testCode[449:207] sortedNum:3
2011-07-02 11:38:29.932 testCode[449:207] sortedNum:5

Cocos2d: 取得 CCSprite 寬跟高

 //取得 CCSprite 寬跟高

CCSprite *sp = [CCSprite spriteWithFile:@"Icon.png"];
NSLog(@"sp width:%f,heigt,:%f",sp.contentSize.width , sp.contentSize.height);

2011年6月26日 星期日

用 NSMutableArray 記錄與取得 int ,CGPoint

Objective-C 真是一個麻煩的語言
//int 記錄與取得
myArray = [NSMutableArray array];
[myArray addObject:[NSNumber numberWithInteger:1234]];
//..
int theNumber = [[myArray objectAtIndex:0] integerValue];
//CGPoint記錄與取得
NSArray *points = [NSArray arrayWithObjects:
  [NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)],
  [NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)], nil];
NSValue *val = [points objectAtIndex:0];
CGPoint p = [val CGPointValue];

2011年6月22日 星期三

load url image

// 用UIView的

id path = @"https://www.google.com/images/logos/ps_logo2.png";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
// UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
UIImage *img =[ UIImage imageWithData:data];

UIImageView *imView = [[UIImageView alloc] initWithImage:img];

[self.view addSubview:imView ];


//在cocos2d

id path = @"https://www.google.com/images/logos/ps_logo2.png";
NSURL *imgUrl = [NSURL URLWithString:path];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
CCTexture2D *tt2d = [[CCTexture2D alloc] initWithImage:image];
CCSprite *sp = [CCSprite spriteWithTexture:tt2d];
[self addChild: sp];

2011年6月21日 星期二

cocos2d:開啟muti touch

在 AppDelegate.m
- (void) applicationDidFinishLaunching:(UIApplication*)application

  //開啟muti touch
    [glView setMultipleTouchEnabled:YES];   // <===新增這一行

}





2011年6月16日 星期四

Error:Xcode 4 Error: Error Starting Executable!!!

今天遇到一個怪問題:
XCode 4 編譯時跳出錯誤視窗


Error: Error Starting Executable!
Error launching remote program: No such file or directory. ......  之類的,看嘸~


明明沒改到什麼東西 ,而且這個也不像 Code 的問題


怎麼發生的還不知到~~
最後解決方法 是
1.把 Device上的程式砍掉~
2.把XCode4 關掉
3.重開機 


傑克,這真是太神奇了~ 一切又可編譯了~



let the CCSprite detect touch(讓CCSprite 觸發 touch 事件)

1. 首先在你的 CCLayer  開啟觸碰
    self.isTouchEnabled = YES;


2. 然後一樣在 CCLayer 加入這一段

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
    UITouch* touch = [touches anyObject];
    //先取得touch 的坐標
  CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
  //驗證坐標
CCSprite *m_targetSprite = xxx; <==取得目標物件
    if (CGRectContainsPoint(m_targetSprite.boundingBox, location))
    {
//touch.....
}
}
附註: 有時CCLayer 的坐標也要考慮進去才行~~~