Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Movement.h
- #ifndef __AppNew__Movement__
- #define __AppNew__Movement__
- #include <iostream>
- #include "cocos2d.h"
- class Movement : public cocos2d::CCLayer
- {
- public:
- // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
- virtual bool init();
- int actualYLR;
- int actualXUD;
- int actualYRL;
- cocos2d::CCSize size = cocos2d::CCDirector::sharedDirector()->getWinSize();
- // there's no 'id' in cpp, so we recommend to return the class instance pointer
- static cocos2d::CCScene* scene();
- cocos2d::CCLabelTTF* notice;
- cocos2d::CCSprite *back;
- //cocos2d::CCSprite *down;
- cocos2d::CCSprite *downButton;
- cocos2d::CCSprite *objDown;
- cocos2d::CCSprite *spriteDU;
- cocos2d::CCSprite *spriteRL;
- //cocos2d::CCArray* array1 = cocos2d::CCArray::create();
- // a selector callback
- void menuCloseCallback(CCObject* pSender);
- void spriteMoveFinished(CCNode* sender);
- void addObj();
- public: void SpriteSchedule(float time);
- bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
- // preprocessor macro for "static create()" constructor ( node() deprecated )
- CREATE_FUNC(Movement);
- };
- #endif /* defined(__AppNew__Movement__) */
- // end movement.h
- Movement.cpp
- ---------------------------------------------------
- #include "Movement.h"
- #include "SimpleAudioEngine.h"
- using namespace cocos2d;
- using namespace CocosDenshion;
- CCScene* Movement::scene()
- {
- // 'scene' is an autorelease object
- CCScene *scene = CCScene::create();
- // 'layer' is an autorelease object
- Movement *layer = Movement::create();
- // add layer as a child to scene
- scene->addChild(layer);
- // return the scene
- return scene;
- }
- int DU,UD,LR,RL;
- // on "init" you need to initialize your instance
- bool Movement::init()
- {
- //////////////////////////////
- // 1. super init first
- if ( !CCLayer::init() )
- {
- return false;
- }
- back= CCSprite::create("back.png");
- back->setPosition(ccp(384*(size.width/768),512*(size.height/1024)));
- back->setScaleX(1.7);
- back->setScaleY(3.2);
- this->addChild(back);
- this->setTouchEnabled(true);
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
- this->schedule(schedule_selector(Movement::SpriteSchedule), 3);
- //CCCallFuncN::create(this, callfuncN_selector(Movement::addObj));
- return true;
- }
- void Movement::addObj()
- {
- //DOWN to UP
- objDown = CCSprite::create("sprite2.png");
- int minXDU = (objDown->getContentSize().width/768)+20;
- int maxXDU = (size.width - objDown->getContentSize().width/1024)-20;
- int rangeYDU = maxXDU - minXDU;
- actualXUD = ( arc4random() % rangeYDU ) + minXDU;
- objDown->setPosition(ccp(actualXUD,145*(size.height/1024)) );
- this->addChild(objDown);
- objDown->setTag(2);
- DU=objDown->getTag();
- int minDurationDU = (int)10.0;
- int maxDurationDU = (int)20.0;
- int rangeDurationDU = maxDurationDU - minDurationDU;
- int actualDU = ( arc4random() % rangeDurationDU ) + minDurationDU;
- CCFiniteTimeAction* actionMoveDU = CCMoveTo::create( (float)actualDU,ccp(actualXUD,1024 ) );
- CCFiniteTimeAction* actionMoveDoneDU = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));
- objDown->runAction( CCSequence::create(actionMoveDU,actionMoveDoneDU, NULL) );
- //----
- //UP to DOWN
- objDown = CCSprite::create("sprite10.png");
- int minXUD = (objDown->getContentSize().width/768)+20;
- int maxXUD = (size.width - objDown->getContentSize().width/1024)-20;
- int rangeYUD = maxXUD - minXUD;
- actualXUD = ( arc4random() % rangeYUD ) + minXUD;
- objDown->setPosition(ccp(actualXUD,1000*(size.height/1024)) );
- this->addChild(objDown);
- objDown->setTag(10);
- UD=objDown->getTag();
- int minDurationUP = (int)10.0;
- int maxDurationUP = (int)15.0;
- int rangeDurationUD = maxDurationUP - minDurationUP;
- int actualDurationUD = ( arc4random() % rangeDurationUD ) + minDurationUP;
- CCFiniteTimeAction* actionMoveUD = CCMoveTo::create( (float)actualDurationUD,ccp(actualXUD,148 ) );
- CCFiniteTimeAction* actionMoveDoneUD = CCCallFuncN::create( this,callfuncN_selector(Movement::
- spriteMoveFinished));
- objDown->runAction( CCSequence::create(actionMoveUD,actionMoveDoneUD, NULL) );
- //----
- //RIGHT to LEFT
- objDown = CCSprite::create("sprite3.png");
- int minYRL = (objDown->getContentSize().height/1024) + 148 ;
- int maxYRL = (size.height - objDown->getContentSize().height/1024)-20;
- int rangeYRL = maxYRL - minYRL;
- actualYLR = ( arc4random() % rangeYRL ) + minYRL;
- objDown->setPosition(ccp(768*(size.width/768),actualYLR) );
- objDown->setTag(333);
- this->addChild(objDown);
- objDown->setTag(3);
- RL=objDown->getTag();
- int minDurationRL = (int)10.0;
- int maxDurationRL = (int)20.0;
- int rangeDurationRL = maxDurationRL - minDurationRL;
- int actualDurationRL = ( arc4random() % rangeDurationRL ) + minDurationRL;
- CCFiniteTimeAction* actionMoveRL = CCMoveTo::create( (float)actualDurationRL,ccp(0 , actualYLR) );
- CCFiniteTimeAction* actionMoveDoneRL = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));
- objDown->runAction( CCSequence::create(actionMoveRL,actionMoveDoneRL, NULL) );
- //-------
- //LEFT to RIGHT
- objDown = CCSprite::create("sprite1.png");
- int minYLR = (objDown->getContentSize().height/1024)+148;
- int maxYLR = (size.height - objDown->getContentSize().height/1024)-20;
- int rangeYLR = maxYLR - minYLR;
- actualYLR = ( arc4random() % rangeYLR ) + minYLR;
- objDown->setPosition(ccp(0*(size.width/768),actualYLR) );
- this->addChild(objDown);
- objDown->setTag(1);
- LR=objDown->getTag();
- int minDurationLR = (int)10.0;
- int maxDurationLR = (int)20.0;
- int rangeDurationLR = maxDurationLR - minDurationLR;
- int actualDurationLR = ( arc4random() % rangeDurationLR ) + minDurationLR;
- CCFiniteTimeAction* actionMoveLR = CCMoveTo::create( (float)actualDurationLR,ccp(768*(size.width/768), actualYLR) );
- CCFiniteTimeAction* actionMoveDoneLR = CCCallFuncN::create( this,callfuncN_selector(Movement::spriteMoveFinished));
- objDown->runAction( CCSequence::create(actionMoveLR,actionMoveDoneLR, NULL) );
- CCFadeIn *fade = CCFadeIn::create(1.0f);
- objDown->runAction(fade);
- //-----end
- }
- void Movement::spriteMoveFinished(CCNode* sender)
- {
- CCSprite *objDown = (CCSprite *)sender;
- this->removeChild(objDown, true);
- }
- void Movement::SpriteSchedule(float t)
- {
- this->addObj();
- }
- bool Movement::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- CCLog("Touches Began");
- CCPoint touchLocation =pTouch->getLocationInView();
- touchLocation=CCDirector::sharedDirector()->convertToGL(touchLocation);
- if(objDown->boundingBox().containsPoint(touchLocation))
- {
- this->removeChildByTag(LR);
- }
- // if(conditionTrue && RL==3)
- // {
- //
- // this->removeChildByTag(RL);
- //
- // }
- // if(conditionTrue && UD==10)
- // {
- //
- // this->removeChildByTag(UD);
- //
- // }
- //
- // if(conditionTrue && DU==2)
- // {
- //
- // this->removeChildByTag(DU);
- //
- // }
- //
- return true;
- }
- void Movement::menuCloseCallback(CCObject* pSender)
- {
- CCDirector::sharedDirector()->end();
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement