Cocos2d技巧之用戶交互-觸碰,按住和拖曳操作

Cocos2d技巧之用戶交互-觸碰,按住和拖曳操作

 

觸碰,按住和拖曳操作是應用和遊戲中最常使用的交互技術。這三種操作是用戶界面交互的基石,同時也是和遊戲中物體交互的基礎。

在這個示例中,我們創建了一個CCSprite的分類,從而可以讓精靈對象處理觸摸事件,並維持某種特定的狀態。此外還添加了部分邏輯讓用戶可以觸碰,按住和拖曳該精靈對象。

示例項目請參考附件(Xcode4.6+iOS6.1.2+cocos2dv2.1 rc0a)

 http://vdisk.weibo.com/s/tlzvl

Step1.在Xcode中創建一個新項目,命名爲TapHoldDrag

Step2.將resources文件夾中的文件拖到項目中,確保選中”copy….into…”,並勾選項目名稱。

Step3.在Xcode中切換到HelloWorld.m,在文件頂部添加以下代碼:

#import "ColorTouchSprite.h"

#import "Helpers.h"

然後在@implementationHelloWorldLayer後添加實例變量:

@implementation HelloWorldLayer{

   

    ColorTouchSprite *colorTouchSprite;

   

    CCLabelBMFont *message;

   

}

 

 

注意,這是iOS5之後的編碼新規範,也即實例變量聲明不需要再放在@interface部分,而是放在@implementation部分。

 

在init方法前添加以下方法:

-(void) step {

    //Changecolor and message depending on ColorTouchSprite state

    if(colorTouchSprite.touchedState ==TS_NONE){

       

      [message setColor:ccc3(255,255,255)];

      [message setString:@"Tap, hold or drag the square."];

      [colorTouchSprite setColor:ccc3(255,255,255)];

       

    }else if(colorTouchSprite.touchedState ==TS_TAP){

       

      [message setColor:ccc3(255,0,0)];

      [message setString:@"Tap."];

      [colorTouchSprite setColor:ccc3(255,0,0)];

       

    }else if(colorTouchSprite.touchedState ==TS_HOLD){

       

      [message setColor:ccc3(0,255,0)];

      [message setString:@"Hold."];

      [colorTouchSprite setColor:ccc3(0,255,0)];

       

    }else if(colorTouchSprite.touchedState ==TS_DRAG){

       

      [message setColor:ccc3(0,0,255)];

      [message setString:@"Drag."];

      [colorTouchSprite setColor:ccc3(0,0,255)];

       

    }

}

 

/* Process touch events */

-(void)ccTouchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event {

   

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView: [touch view]];

    point = [[CCDirector sharedDirector] convertToGL: point];

   

    if(pointIsInRect(point, [colorTouchSpriterect])){

      [colorTouchSprite ccTouchesBegan:touches withEvent:event];

    }

}

-(void)ccTouchesMoved:(NSSet *)toucheswithEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView: [touch view]];

    point = [[CCDirector sharedDirector] convertToGL: point];

   

    if(pointIsInRect(point, [colorTouchSpriterect])){

      [colorTouchSprite ccTouchesMoved:touches withEvent:event];

    }

}

 

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView: [touch view]];

    point = [[CCDirector sharedDirector] convertToGL: point];

   

    if(pointIsInRect(point, [colorTouchSpriterect])){

      [colorTouchSprite ccTouchesEnded:touches withEvent:event];

    }

}

 

然後更改init方法中的代碼如下:

// on "init" you need to initialize your instance

-(id) init

{

    //always call "super" init

    // Applerecommends to re-assign "self" with the "super's" returnvalue

    if( (self=[superinit]) ) {

       

        self.touchEnabled =YES;

       

        //Our message sprite

        message = [CCLabelBMFontlabelWithString:@"Tap, hold or drag the square."fntFile:@"eurostile_30.fnt"];

        message.position =ccp(240,260);

        message.scale =0.75f;

        [self addChild:message];

       

        //Init the ColorTouchSprite

        colorTouchSprite = [ColorTouchSpritespriteWithFile:@"blank.png"];

        colorTouchSprite.position =ccp(240,160);

        [colorTouchSprite setTextureRect:CGRectMake(0,0,100,100)];

        [self addChild:colorTouchSprite];

       

        [self schedule:@selector(step)];

       

        return self;

 

       

       

    }

    return self;

}

 

這裏最重要的就是可重用的ColorTouchSprite這個CCSprite的子類,我們保存了不同的狀態變量,以區分觸碰,按住和拖曳的幾個不同狀態。還指定了一個-(CGRect)rect方法來判斷精靈是否被觸碰。至於幾個觸摸的標準方法這裏就不再贅述了。

 

當然,除了這種方法,還可以使用Cocos2d的CCTargetedTouchDelegate協議,來指定一個協議對象自動處理觸摸事件,大家可以自己嘗試。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章