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 的坐標也要考慮進去才行~~~

2011年6月15日 星期三

warning: Semantic Issue: Local declaration of 'm_aniWelcome' hides instance variable 的意思...

warning: Semantic Issue: Local declaration of 'm_aniWelcome' hides instance variable


假設
//--------
the.h

@interface myLayer : CCLayer {

    AAA *a;
}
//--------
the.m
-(id)init{
    self = [super init];
    if (self) {
      AAA *a = [[AAA alloc] init];   <====這一行會有 !
    }

   return self;
}




嗯~就是變數重覆!!!  編譯器在抱怨了~~





2011年6月14日 星期二

cocos2d UITextField!!( 1.0.0 rc2)

想要在 cocos2d的環境下,呼叫 keyboard 出來
目前版本CCLabel似乎沒有支援,在拜請google神之後,到是找到另一個方法,所以先記錄下來


原文:http://stackoverflow.com/questions/693483/textfield-example-in-cocos2d


這個解法是 把UITextField 想辦法放到目前的畫面,自然就可以支援 keyboard 了~~


重點         (1) 創建一個 UITextField *myText;
(2)讓你的 layer <UITextFieldDelegate>
(3)把 myText 加到畫面上 (討厭的新舊版小細節)


[[[[CCDirector sharedDirector] openGLView] window] addSubview:myText];
    [myText becomeFirstResponder];

                 (4) 一些mothed


-(BOOL)ccTouchesBegan:(NSSet  *)touches withEvent:(UIEvent *)event 
-(BOOL)textFieldShouldReturn:(UITextField *)textField 
-(void)textFieldDidEndEditing: (UITextField *)textField 



主要code 節錄:
//------------------------------------------------------------------------------------------------------------------------------
.h




#import <UIKit/UIKit.h>
#import "cocos2d.h"
@interface MYSCENE : Layer <UITextFieldDelegate>
{
    UITextField *myText;
}
-(void)specificStartLevel;
@end
@interface textFieldTestAppDelegate : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate, UIApplicationDelegate>
{
    UIWindow *window;
}
@end



//------------------------------------------------------------------------------------------------------------------------------

.m




#import "textFieldTestAppDelegate.h"
@implementation MYSCENE-(id) init{
    self = [super init];
    isTouchEnabled = YES;
    return self;
}
-(BOOL)ccTouchesBegan:(NSSet  *)touches withEvent:(UIEvent *)event {
    [self specifyStartLevel];
    return kEventHandled;
}
-(void)specifyStartLevel {
    myText = [[UITextField alloc] initWithFrame:CGRectMake(60, 165, 200, 90)];
    [myText setDelegate:self];
    [myText setText:@""];
    [myText setTextColor: [UIColor colorWithRed:255 green:255 blue:255 alpha:1.0]];
    [[[[Director sharedDirector] openGLView] window] addSubview:myText];
    [myText becomeFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [myText resignFirstResponder];
    return YES;
}
-(void)textFieldDidEndEditing: (UITextField *)textField {
    if(textField == myText) {
        [myText endEditing:YES];
        [myText removeFromSuperview];
        NSString *result = myText.text;
        NSLog([NSString stringWithFormat:@"entered: %@", result]);
    } else {
        NSLog(@"textField did not match myText");
    }
}
-(void) dealloc{
[super dealloc];
}
@end
@implementation textFieldTestAppDelegate- (void)applicationDidFinishLaunching:(UIApplication *)application{
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [window setUserInteractionEnabled:YES];
    [[Director sharedDirector] setDisplayFPS:YES];
    [[Director sharedDirector] attachInWindow:window];
    Scene *scene = [Scene node];
    [scene addChild: [MYSCENE node]];
    [window makeKeyAndVisible];
    [[Director sharedDirector] runWithScene: scene];
}
-(void)dealloc{
    [super dealloc];
}
-(void) applicationWillResignActive:(UIApplication *)application{
    [[Director sharedDirector] pause];
}
-(void) applicationDidBecomeActive:(UIApplication *)application{
    [[Director sharedDirector] resume];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application{
    [[TextureMgr sharedTextureMgr] removeAllTextures];
}
@end