快速連結

2011年8月10日

我用cocos2D做出滑動選單了

好吧我知道這樣子做超笨的。可是我真的覺得剩下的lib都不符合我需求。
就連CCMenuItem也是,根本欺負人(欸?)

使用code/lib:XCode 4、cocos2D 1.0.1
有使用CCSpriteBatchNode,但這部份我不多做說明了。






先復習一下CCSpriteBatchNode:

CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"items.png" capacity:150];
int icon_width = 120;
int icon_height = 150;
int index = 0;
for (int xx=0;xx < 3; xx++){
  for (int yy=0;yy < 2; yy++){
     CCSprite *img = [CCSprite spriteWithBatchNode:batch rect:CGRectMake(icon_width * xx,icon_height * yy,icon_width,icon_height)];
     img.position = ccp(xx+ index * 230,yy);    //230是間距
     if (index != 0){    //如果不是第一張圖片的話就半透明
         img.opacity = 180;   
     }
     [batch addChild:img z:0 tag:index+1];
     index++;
  }
}



接著要作加工!!!!
在回圈底下加上這五句:


CCSprite *view = [[CCSprite alloc] init];
[self addChild:view z:1 tag:1];
[view addChild:batch z:0 tag:2];

totalItems = index;
nowItem = 1;


忘記說了,totalItems跟nowItem兩個變數請在.h檔案內宣告哦!!

再來寫上三個touch事件:
不知道為什麼不能用單一觸控點的事件來寫,如果有人能告訴我就真是太好了QAQ!!(還是模擬器問題?)

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
    UITouch *touch = [touches anyObject];
    CCLOG(@"touch start");
    //touch move:
    startPoint = [touch locationInView:[touch view]];
    CCSprite *view = (CCSprite*) [self getChildByTag:1];
    CCSprite *img = (CCSprite*) [[view getChildByTag:2] getChildByTag:nowItem];
    img.opacity = 180;  
    startPoint = ccp(startPoint.x +( -view.position.x),startPoint.y);
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Add a new body/atlas sprite at the touched location
    CCSprite *view = (CCSprite*) [self getChildByTag:1];
    UITouch *touch = [touches anyObject];
    endPoint = [touch locationInView: [touch view]];
    endPoint = ccp(endPoint.x +( -viewer.position.x),endPoint.y);
    //計算位置與ITEMID的關係
    nowItem = (int)(-(view.position.x / 230) + 0.5) +1 ;
    CCSprite *img = (CCSprite*) [[view getChildByTag:2] getChildByTag:nowItem];
 
    img.opacity = 255; 
    
    if (startPoint.x < endPoint.x){
        [view runAction:[CCMoveTo actionWithDuration:0.1 position:ccp(230*(nowItem-1), 0)]];
    }else if (startPoint.x > endPoint.x){
        [view runAction:[CCMoveTo actionWithDuration:0.1 position:ccp(-230*(nowItem-1), 0)]];

    }
    
    NSLog(@"view postion x? %0.2f",view.position.x);
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
    CCSprite *view = (CCSprite*) [self getChildByTag:1];
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView: [touch view]];
    
    touchPoint = ccp(touchPoint.x +( -view.position.x),touchPoint.y);
    
    view.position = ccp(view.position.x + (touchPoint.x- startPoint.x), 0);
    
    if (view.position.x < -230*(totalItems-1)) view.position = ccp(-230*(totalItems-1),0);
    if (view.position.x > 0) view.position = ccp(0,0);
}



反正就是繁瑣的算術Orz.......我數學不好算的很痛苦....

基本上已經可以動了。但是還不能點。

在ccTouchesBegan的事件內的最後加上一行:

 if([self hitItemTest:startPoint]) [self hitEvent];


然後宣告在.h檔案內寫上:

-(BOOL) hitItemTest:(CGPoint) point;
-(void)hitEvent;


.m檔案內寫上:

-(BOOL) hitItemTest:(CGPoint) point{
    CCSprite *item = (CCSprite*) [[viewer getChildByTag:2] getChildByTag:nowItem];
    if (CGRectContainsPoint(CGRectMake(item.position.x - item.contentSize.width/2, item.position.y - item.contentSize.height/2, item.contentSize.width, item.contentSize.height), point)) {
        NSLog(@"hit test YES!!! ");
        return YES;
    }else{
        NSLog(@"hit test!!! NO!!");
        return NO;
    }
}

-(void)hitEvent{
    NSLog(@"item %d click!!!!!!!!",nowItem);
}


現在就會出現你想要看見的結果囉~!!

沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT