2011年6月30日 星期四

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;
}




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