Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // StoreScene.h
- #include "cocos2d.h"
- class StoreScene : public cocos2d::CCLayer {
- private:
- cocos2d::CCLayerColor * mScrollLayer;
- cocos2d::CCMenu * mMenu;
- cocos2d::CCPoint mTouchStartPos;
- public:
- static cocos2d::CCScene * scene();
- virtual void init();
- CREATE_FUNC( StoreScene ) // LAYER_CREATE_FUNC is deprecated in cocos2d-x 2.0.4
- void drawButtons();
- virtual bool ccTouchBegan( cocos2d::CCTouch * touch, cocos2d::CCEvent * event );
- virtual void ccTouchMoved( cocos2d::CCTouch * touch, cocos2d::CCEvent * event );
- virtual void ccTouchEnded( cocos2d::CCTouch * touch, cocos2d::CCEvent * event );
- }
- // StoreScene.cpp
- #include "StoreScene.h"
- // GraphicMacros.h contains functions like getWinWize() and the like.
- #include "GraphicMacros.h"
- USING_NS_CC;
- /** cocos2d-x common functions
- * - Functions like create() and scene() here.
- */
- bool StoreScene::init() {
- if ( !CCLayer::init() ) return false;
- // Create the scrolling layer
- mScrollLayer = CCLayerColor::create( ccc4( 255, 255, 255, 255 ), getWinSize().width, getWinSize().height );
- this -> addChild( mScrollLayer );
- // Create CCMenu object
- mMenu = CCMenu::create();
- mMenu -> setPosition( CCPointZero );
- this -> addChild( mMenu );
- // Fill the scroll layer with items
- this -> drawButtons();
- // Enable touch
- this -> setTouchEnabled( true );
- CCDirector::sharedDirector() -> getTouchDispatcher() -> addTargetedDelegate( this, 0, true );
- // Report success.
- return true;
- }
- void StoreScene::drawButtons() {
- // Draw a bunch of CCMenuItemImage objects.
- for ( int i = 0; i <= 10; i++ ) {
- CCMenuItemImage * menu = CCMenuItemImage::create( "btn_on.png", "btn_off.png", this, menu_selector( StoreScene::doPlay );
- menu -> setTag( i );
- mMenu -> addChild( menu );
- }
- }
- bool StoreScene::ccTouchBegan( CCTouch * touch, CCEvent * event ) {
- // Save touch position
- mTouchStartPos = this -> convertTouchToNodeSpace( touch );
- // Swallow touch
- return true;
- }
- void StoreScene::ccTouchMoved( CCTouch * touch, CCEvent * event ) {
- // Calculate difference in touch
- CCPoint touchLoc = this -> convertTouchToNodeSpace( touch );
- CCPoint oldLoc = this -> convertToNodeSpace( touch -> getPreviousLocationInView() );
- CCPoint gap = ccpSub( touchLoc, oldLoc );
- // Drag mMenu towards touch location, but not changing y-position
- CCPoint newLoc = ccpAdd( mMenu -> getPosition(), gap );
- mMenu -> setPosition( CCPointMake( newLoc.x, mMenu -> getPosition().y ) );
- }
- void StoreScene::ccTouchEnded( CCTouch * touch, CCEvent * event ) {
- // Calculate touch length (along x-axis )
- CCPoint touchLoc = this -> convertTouchToNodeSpace( touch );
- CCPoint gap = ccpSub( touchLoc, mTouchStartPos );
- // Check gap length
- if ( gap.x < -200 ) {
- // Touch is towards the left. Do something about it.
- } else if ( gap.x > 200 ) {
- // Touch is towards the right. Do something about it.
- } else {
- // Touch is not long enough. Reset mMenu position. ( assume original position is CCPointZero )
- mMenu -> runAction( CCMoveTo::create( 0.20f, CCPointZero ) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement