Advertisement
Guest User

Cocos2d-x C++ example

a guest
Aug 2nd, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.95 KB | None | 0 0
  1. Movement.h
  2.  
  3.  
  4. #ifndef __AppNew__Movement__
  5. #define __AppNew__Movement__
  6.  
  7. #include <iostream>
  8.  
  9.  
  10. #include "cocos2d.h"
  11.  
  12. class Movement : public cocos2d::CCLayer
  13. {
  14. public:
  15.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
  16.     virtual bool init();
  17.     int actualYLR;
  18.     int actualXUD;
  19.     int actualYRL;
  20.    
  21.     cocos2d::CCSize size = cocos2d::CCDirector::sharedDirector()->getWinSize();
  22.     // there's no 'id' in cpp, so we recommend to return the class instance pointer
  23.     static cocos2d::CCScene* scene();
  24.     cocos2d::CCLabelTTF* notice;
  25.    
  26.     cocos2d::CCSprite *back;
  27.     //cocos2d::CCSprite *down;
  28.     cocos2d::CCSprite *downButton;
  29.     cocos2d::CCSprite *objDown;
  30.     cocos2d::CCSprite *spriteDU;
  31.     cocos2d::CCSprite *spriteRL;
  32.     //cocos2d::CCArray* array1 = cocos2d::CCArray::create();
  33.    
  34.    
  35.     // a selector callback
  36.     void menuCloseCallback(CCObject* pSender);
  37.     void spriteMoveFinished(CCNode* sender);
  38.     void addObj();
  39.     public: void SpriteSchedule(float time);
  40.     bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
  41.     // preprocessor macro for "static create()" constructor ( node() deprecated )
  42.     CREATE_FUNC(Movement);
  43.    
  44.    
  45. };
  46. #endif /* defined(__AppNew__Movement__) */
  47.  
  48.  
  49. // end movement.h
  50.  
  51.  
  52. Movement.cpp
  53. ---------------------------------------------------
  54. #include "Movement.h"
  55. #include "SimpleAudioEngine.h"
  56.  
  57.  
  58. using namespace cocos2d;
  59. using namespace CocosDenshion;
  60.  
  61. CCScene* Movement::scene()
  62. {
  63.     // 'scene' is an autorelease object
  64.     CCScene *scene = CCScene::create();
  65.    
  66.     // 'layer' is an autorelease object
  67.     Movement *layer = Movement::create();
  68.    
  69.     // add layer as a child to scene
  70.     scene->addChild(layer);
  71.    
  72.     // return the scene
  73.     return scene;
  74. }
  75.  
  76. int DU,UD,LR,RL;
  77.  
  78. // on "init" you need to initialize your instance
  79. bool Movement::init()
  80. {
  81.     //////////////////////////////
  82.     // 1. super init first
  83.     if ( !CCLayer::init() )
  84.     {
  85.         return false;
  86.        
  87.     }
  88.      
  89.     back= CCSprite::create("back.png");
  90.     back->setPosition(ccp(384*(size.width/768),512*(size.height/1024)));
  91.     back->setScaleX(1.7);
  92.     back->setScaleY(3.2);
  93.     this->addChild(back);
  94.    
  95.  
  96.     this->setTouchEnabled(true);
  97.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
  98.     this->schedule(schedule_selector(Movement::SpriteSchedule), 3);
  99.     //CCCallFuncN::create(this, callfuncN_selector(Movement::addObj));
  100.    
  101.    
  102.     return true;
  103.  
  104.     }
  105.  
  106. void Movement::addObj()
  107. {
  108.    
  109.     //DOWN to UP
  110.     objDown = CCSprite::create("sprite2.png");
  111.     int minXDU = (objDown->getContentSize().width/768)+20;
  112.     int maxXDU = (size.width -  objDown->getContentSize().width/1024)-20;
  113.    
  114.     int rangeYDU = maxXDU - minXDU;
  115.     actualXUD = ( arc4random() % rangeYDU ) + minXDU;
  116.     objDown->setPosition(ccp(actualXUD,145*(size.height/1024)) );
  117.     this->addChild(objDown);
  118.     objDown->setTag(2);
  119.    
  120.     DU=objDown->getTag();
  121.    
  122.     int minDurationDU = (int)10.0;
  123.     int maxDurationDU = (int)20.0;
  124.     int rangeDurationDU = maxDurationDU - minDurationDU;
  125.     int actualDU = ( arc4random() % rangeDurationDU ) + minDurationDU;
  126.    
  127.     CCFiniteTimeAction* actionMoveDU = CCMoveTo::create( (float)actualDU,ccp(actualXUD,1024 ) );
  128.    
  129.     CCFiniteTimeAction* actionMoveDoneDU = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));
  130.    
  131.     objDown->runAction( CCSequence::create(actionMoveDU,actionMoveDoneDU, NULL) );
  132.     //----
  133.    
  134.     //UP to DOWN
  135.     objDown = CCSprite::create("sprite10.png");
  136.     int minXUD = (objDown->getContentSize().width/768)+20;
  137.     int maxXUD = (size.width -  objDown->getContentSize().width/1024)-20;
  138.     int rangeYUD = maxXUD - minXUD;
  139.     actualXUD = ( arc4random() % rangeYUD ) + minXUD;
  140.     objDown->setPosition(ccp(actualXUD,1000*(size.height/1024)) );
  141.     this->addChild(objDown);
  142.     objDown->setTag(10);
  143.     UD=objDown->getTag();
  144.     int minDurationUP = (int)10.0;
  145.     int maxDurationUP = (int)15.0;
  146.     int rangeDurationUD = maxDurationUP - minDurationUP;
  147.     int actualDurationUD = ( arc4random() % rangeDurationUD ) + minDurationUP;
  148.    
  149.     CCFiniteTimeAction* actionMoveUD = CCMoveTo::create( (float)actualDurationUD,ccp(actualXUD,148 ) );
  150.     CCFiniteTimeAction* actionMoveDoneUD = CCCallFuncN::create( this,callfuncN_selector(Movement::
  151.                                                                                         spriteMoveFinished));
  152.     objDown->runAction( CCSequence::create(actionMoveUD,actionMoveDoneUD, NULL) );
  153.    
  154.     //----
  155.    
  156.    
  157.    
  158.     //RIGHT to LEFT
  159.     objDown = CCSprite::create("sprite3.png");
  160.     int minYRL = (objDown->getContentSize().height/1024) + 148 ;
  161.     int maxYRL = (size.height -  objDown->getContentSize().height/1024)-20;
  162.    
  163.     int rangeYRL = maxYRL - minYRL;
  164.     actualYLR = ( arc4random() % rangeYRL ) + minYRL;
  165.    
  166.     objDown->setPosition(ccp(768*(size.width/768),actualYLR) );
  167.     objDown->setTag(333);
  168.     this->addChild(objDown);
  169.     objDown->setTag(3);
  170.     RL=objDown->getTag();
  171.     int minDurationRL = (int)10.0;
  172.     int maxDurationRL = (int)20.0;
  173.     int rangeDurationRL = maxDurationRL - minDurationRL;
  174.     int actualDurationRL = ( arc4random() % rangeDurationRL ) + minDurationRL;
  175.    
  176.     CCFiniteTimeAction* actionMoveRL = CCMoveTo::create( (float)actualDurationRL,ccp(0 , actualYLR) );
  177.    
  178.     CCFiniteTimeAction* actionMoveDoneRL = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));
  179.    
  180.     objDown->runAction( CCSequence::create(actionMoveRL,actionMoveDoneRL, NULL) );
  181.    
  182.     //-------
  183.    
  184.    
  185.    
  186.     //LEFT to RIGHT
  187.     objDown = CCSprite::create("sprite1.png");
  188.     int minYLR = (objDown->getContentSize().height/1024)+148;
  189.     int maxYLR = (size.height -  objDown->getContentSize().height/1024)-20;
  190.    
  191.     int rangeYLR = maxYLR - minYLR;
  192.     actualYLR = ( arc4random() % rangeYLR ) + minYLR;
  193.    
  194.     objDown->setPosition(ccp(0*(size.width/768),actualYLR) );
  195.     this->addChild(objDown);
  196.    
  197.     objDown->setTag(1);
  198.    
  199.     LR=objDown->getTag();
  200.     int minDurationLR = (int)10.0;
  201.     int maxDurationLR = (int)20.0;
  202.     int rangeDurationLR = maxDurationLR - minDurationLR;
  203.    
  204.     int actualDurationLR = ( arc4random() % rangeDurationLR ) + minDurationLR;
  205.    
  206.     CCFiniteTimeAction* actionMoveLR = CCMoveTo::create( (float)actualDurationLR,ccp(768*(size.width/768), actualYLR) );
  207.    
  208.     CCFiniteTimeAction* actionMoveDoneLR = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));
  209.    
  210.     objDown->runAction( CCSequence::create(actionMoveLR,actionMoveDoneLR, NULL) );
  211.     CCFadeIn *fade = CCFadeIn::create(1.0f);
  212.     objDown->runAction(fade);
  213.     //-----end
  214.    
  215. }
  216.  
  217. void Movement::spriteMoveFinished(CCNode* sender)
  218. {
  219.     CCSprite *objDown = (CCSprite *)sender;
  220.     this->removeChild(objDown, true);
  221. }
  222.  
  223.  void Movement::SpriteSchedule(float t)
  224. {
  225.    
  226.     this->addObj();
  227. }
  228.  
  229.  
  230. bool Movement::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
  231. {
  232.     CCLog("Touches Began");
  233.     CCPoint touchLocation =pTouch->getLocationInView();
  234.     touchLocation=CCDirector::sharedDirector()->convertToGL(touchLocation);
  235.    
  236.     if(objDown->boundingBox().containsPoint(touchLocation))
  237.     {
  238.        
  239.         this->removeChildByTag(LR);
  240.        
  241.     }
  242.    
  243. //    if(conditionTrue && RL==3)
  244. //    {
  245. //        
  246. //        this->removeChildByTag(RL);
  247. //        
  248. //    }
  249. //    if(conditionTrue && UD==10)
  250. //    {
  251. //        
  252. //        this->removeChildByTag(UD);
  253. //        
  254. //    }
  255. //    
  256. //    if(conditionTrue && DU==2)
  257. //    {
  258. //        
  259. //        this->removeChildByTag(DU);
  260. //        
  261. //    }
  262. //
  263.     return true;
  264.    
  265. }
  266.  
  267.  
  268.  
  269. void Movement::menuCloseCallback(CCObject* pSender)
  270. {
  271.     CCDirector::sharedDirector()->end();
  272.    
  273. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  274.     exit(0);
  275. #endif
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement