Advertisement
lance-gray

Touch Practice

Jan 22nd, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. // StoreScene.h
  2. #include "cocos2d.h"
  3.  
  4. class StoreScene : public cocos2d::CCLayer {
  5. private:
  6.    cocos2d::CCLayerColor * mScrollLayer;
  7.    cocos2d::CCMenu * mMenu;
  8.    cocos2d::CCPoint mTouchStartPos;
  9.  
  10. public:
  11.    static cocos2d::CCScene * scene();
  12.    virtual void init();
  13.    CREATE_FUNC( StoreScene ) // LAYER_CREATE_FUNC is deprecated in cocos2d-x 2.0.4
  14.  
  15.    void drawButtons();
  16.  
  17.    virtual bool ccTouchBegan( cocos2d::CCTouch * touch, cocos2d::CCEvent * event );
  18.    virtual void ccTouchMoved( cocos2d::CCTouch * touch, cocos2d::CCEvent * event );
  19.    virtual void ccTouchEnded( cocos2d::CCTouch * touch, cocos2d::CCEvent * event );
  20. }
  21.  
  22. // StoreScene.cpp
  23. #include "StoreScene.h"
  24.  
  25. // GraphicMacros.h contains functions like getWinWize() and the like.
  26. #include "GraphicMacros.h"
  27. USING_NS_CC;
  28.  
  29. /** cocos2d-x common functions
  30.  *    - Functions like create() and scene() here.
  31.  */
  32.  
  33. bool StoreScene::init() {
  34.    if ( !CCLayer::init() ) return false;
  35.  
  36.    // Create the scrolling layer
  37.    mScrollLayer = CCLayerColor::create( ccc4( 255, 255, 255, 255 ), getWinSize().width, getWinSize().height );
  38.    this -> addChild( mScrollLayer );
  39.  
  40.    // Create CCMenu object
  41.    mMenu = CCMenu::create();
  42.    mMenu -> setPosition( CCPointZero );
  43.    this -> addChild( mMenu );
  44.  
  45.    // Fill the scroll layer with items
  46.    this -> drawButtons();
  47.  
  48.    // Enable touch
  49.    this -> setTouchEnabled( true );
  50.    CCDirector::sharedDirector() -> getTouchDispatcher() -> addTargetedDelegate( this, 0, true );
  51.  
  52.    // Report success.
  53.    return true;
  54. }
  55.  
  56. void StoreScene::drawButtons() {
  57.    // Draw a bunch of CCMenuItemImage objects.
  58.    for ( int i = 0; i <= 10; i++ ) {
  59.       CCMenuItemImage * menu = CCMenuItemImage::create( "btn_on.png", "btn_off.png", this, menu_selector( StoreScene::doPlay );
  60.       menu -> setTag( i );
  61.       mMenu -> addChild( menu );
  62.    }
  63. }
  64.  
  65. bool StoreScene::ccTouchBegan( CCTouch * touch, CCEvent * event ) {
  66.    // Save touch position
  67.    mTouchStartPos = this -> convertTouchToNodeSpace( touch );
  68.  
  69.    // Swallow touch
  70.    return true;
  71. }
  72.  
  73. void StoreScene::ccTouchMoved( CCTouch * touch, CCEvent * event ) {
  74.    // Calculate difference in touch
  75.    CCPoint touchLoc = this -> convertTouchToNodeSpace( touch );
  76.    CCPoint oldLoc = this -> convertToNodeSpace( touch -> getPreviousLocationInView() );
  77.    CCPoint gap = ccpSub( touchLoc, oldLoc );
  78.  
  79.    // Drag mMenu towards touch location, but not changing y-position
  80.    CCPoint newLoc = ccpAdd( mMenu -> getPosition(), gap );
  81.    mMenu -> setPosition( CCPointMake( newLoc.x, mMenu -> getPosition().y ) );
  82. }
  83.  
  84. void StoreScene::ccTouchEnded( CCTouch * touch, CCEvent * event ) {
  85.    // Calculate touch length (along x-axis )
  86.    CCPoint touchLoc = this -> convertTouchToNodeSpace( touch );
  87.    CCPoint gap = ccpSub( touchLoc, mTouchStartPos );
  88.  
  89.    // Check gap length
  90.    if ( gap.x < -200 ) {
  91.       // Touch is towards the left. Do something about it.
  92.    } else if ( gap.x > 200 ) {
  93.       // Touch is towards the right. Do something about it.
  94.    } else {
  95.       // Touch is not long enough. Reset mMenu position. ( assume original position is CCPointZero )
  96.       mMenu -> runAction( CCMoveTo::create( 0.20f, CCPointZero ) );
  97.    }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement