和上篇文章里方法1不同的是只要按住屏幕的手不松开,主角会一直往触屏点相对与地图的本地坐标点移动,直到最终走到了触屏点。其实就是类似于开启了个定时器,每次定时一到就去监测是否有新的目标点。
1 // 2 // FightLayer.h 3 // VictoryMarchDemo 4 // 5 // Created by jiading on 13-1-15. 6 // 7 // 8 9 #ifndef __VictoryMarchDemo__FightLayer__ 10 #define __VictoryMarchDemo__FightLayer__ 11 12 #include "cocos2d.h" 13 #include "Player.h" 14 #include "RangeLayer.h" 15 #include "TouchMoveHandle.h" 16 17 USING_NS_CC; 18 19 class FightLayer : public CCLayer 20 { 21 public: 22 FightLayer(); 23 ~FightLayer(); 24 25 CCLayer *mapContainer; 26 CCSprite *backBg; 27 CCSprite *centerBg; 28 29 Player *player; 30 31 CCTouch* currentTouch; 32 33 void showBg(); 34 void showPlayer(); 35 36 virtual void onEnter(); 37 virtual void registerWithTouchDispatcher(); 38 virtual void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent); 39 virtual bool ccTouchBegan(CCTouch* touch,CCEvent* event); 40 virtual void ccTouchEnded(CCTouch* touch,CCEvent* event); 41 42 CREATE_FUNC(FightLayer); 43 private: 44 CCPoint selfTargetPos; 45 46 TouchMoveHandle *currentMoveHadle; 47 48 void touchScheduleHandler(); 49 50 }; 51 52 53 #endif /* defined(__VictoryMarchDemo__FightLayer__) */
1 // 2 // FightLayer.cpp 3 // VictoryMarchDemo 4 // 5 // Created by jiading on 13-1-15. 6 // 7 // 8 9 #include "FightLayer.h" 10 #include "Enemy.h" 11 12 FightLayer::FightLayer(): 13 currentMoveHadle(NULL) 14 { 15 16 } 17 18 FightLayer::~FightLayer() 19 { 20 21 } 22 23 void FightLayer::onEnter() 24 { 25 CCLayer::onEnter(); 26 } 27 28 29 30 void FightLayer::registerWithTouchDispatcher() 31 { 32 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 33 } 34 35 bool FightLayer::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event) 36 { 37 currentTouch = touch; 38 39 this->schedule(schedule_selector(FightLayer::touchScheduleHandler), 0.3f); 40 41 42 selfTargetPos = ccp(touch->getLocation().x + abs(mapContainer->getPositionX()),touch->getLocation().y); 43 if(currentMoveHadle) 44 { 45 currentMoveHadle->destroy(); 46 currentMoveHadle->release(); 47 currentMoveHadle = NULL; 48 } 49 50 currentMoveHadle = TouchMoveHandle::create(); 51 currentMoveHadle->retain(); 52 currentMoveHadle->handle(player, backBg, centerBg, mapContainer, selfTargetPos); 53 return true; 54 55 } 56 57 void FightLayer::touchScheduleHandler() 58 { 59 60 selfTargetPos = ccp(currentTouch->getLocation().x + abs(mapContainer->getPositionX()),currentTouch->getLocation().y); 61 if(currentMoveHadle) 62 { 63 currentMoveHadle->destroy(); 64 currentMoveHadle->release(); 65 currentMoveHadle = NULL; 66 } 67 68 currentMoveHadle = TouchMoveHandle::create(); 69 currentMoveHadle->retain(); 70 currentMoveHadle->handle(player, backBg, centerBg, mapContainer, selfTargetPos); 71 72 } 73 74 void FightLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) 75 { 76 77 78 } 79 80 void FightLayer::ccTouchEnded(CCTouch *touch, cocos2d::CCEvent *event) 81 { 82 // CCLog("ccTouchEnded"); 83 this->unschedule(schedule_selector(FightLayer::touchScheduleHandler)); 84 if(currentMoveHadle) 85 { 86 currentMoveHadle->destroy(); 87 currentMoveHadle->release(); 88 currentMoveHadle = NULL; 89 } 90 } 91 92 void FightLayer::showPlayer() 93 { 94 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 95 96 //npc 97 Player *npc = Player::create(); 98 npc->setScale(1.5); 99 npc->setPosition(ccp(850,75)); 100 npc->playAction(Player::state_attack); 101 mapContainer->addChild(npc); 102 103 104 //标出原点 105 player = Player::create(); 106 player->setScale(1.5); 107 player->setPosition(ccp(winSize.width/2,95)); 108 player->playAction(Player::state_idle); 109 mapContainer->addChild(player); 110 111 //enemy 112 Enemy *enemy = Enemy::create(); 113 enemy->setScale(1.5); 114 enemy->setPosition(ccp(centerBg->getContentSize().width * centerBg->getScaleX() - 100,95)); 115 enemy->playAction(Enemy::state_idle); 116 mapContainer->addChild(enemy); 117 118 enemy->runTo(ccp(100,95)); 119 } 120 121 void FightLayer::showBg() 122 { 123 setTouchEnabled(true); 124 125 mapContainer = CCLayer::create(); 126 127 128 129 130 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 131 centerBg = CCSprite::create("map/zhongjing.png"); 132 CCSize mapSize = centerBg->getContentSize(); 133 float mapScale = winSize.height / mapSize.height; 134 centerBg->setAnchorPoint(CCPointZero); 135 centerBg->setPosition(CCPointZero); 136 137 centerBg->setScale(mapScale); 138 139 backBg = CCSprite::create("map/beijing.png"); 140 CCSize backMapSize = backBg->getContentSize(); 141 backBg->setAnchorPoint(CCPointZero); 142 backBg->setScale(mapScale); 143 backBg->setPosition(ccp(0,winSize.height - (backMapSize.height * mapScale))); 144 145 addChild(backBg); 146 addChild(mapContainer); 147 mapContainer->addChild(centerBg); 148 149 RangeLayer *range = RangeLayer::create(); 150 CCSize size = CCSizeMake(centerBg->getContentSize().width * centerBg->getScale(), winSize.height); 151 range->setContentSize(size); 152 range->setLinePox(winSize.width/2, centerBg->getContentSize().width*centerBg->getScale()- winSize.width/2); 153 mapContainer->addChild(range); 154 }
1 // 2 // TouchMoveHandle.h 3 // PlayerActionTest 4 // 5 // Created by 嘉定 on 13-1-3. 6 // 7 // 此类负责地图点击移动的执行 8 // 主要处理人物和背景的移动,都是横向移动,不包含y方向上的移动。 9 10 #ifndef __PlayerActionTest__TouchMoveHandle__ 11 #define __PlayerActionTest__TouchMoveHandle__ 12 13 #include "cocos2d.h" 14 #include "Player.h" 15 16 USING_NS_CC; 17 18 class TouchMoveHandle : public CCObject 19 { 20 public: 21 22 TouchMoveHandle(); 23 virtual ~TouchMoveHandle(); 24 25 typedef enum 26 { 27 leftToCenter = 1, 28 centerToLeft = 2, 29 rightToCenter = 3, 30 centerToRight = 4, 31 behaviorUnKnow = 5 32 }PlayerCrossBehavior; 33 34 void handle(Player *_player, CCSprite *_backBg,CCSprite *_centerBg,CCLayer *_container,CCPoint touchPoint); 35 36 37 virtual bool init(); 38 39 PlayerCrossBehavior moveBehavior; 40 41 void destroy(); 42 43 44 CREATE_FUNC(TouchMoveHandle); 45 46 private: 47 48 float winWidth; 49 Player *player; 50 CCSprite *backBg; 51 CCSprite *centerBg; 52 CCLayer *container; 53 54 55 float backBgWidth; 56 float centerBgWidth; 57 58 CCPoint targetPoint; 59 //左侧判断移动边界 60 float leftSideX; 61 //右侧判断移动边界 62 float rightSideX; 63 64 CCPoint getCrossSidePoint1(); 65 66 void moveContainer1(CCPoint target); 67 void playerCrollSideLineHandler(); 68 }; 69 70 #endif /* defined(__PlayerActionTest__TouchMoveHandle__) */
1 // 2 // TouchMoveHandle.cpp 3 // PlayerActionTest 4 // 5 // Created by 嘉定 on 13-1-3. 6 // 7 // 8 9 #include "TouchMoveHandle.h" 10 11 12 TouchMoveHandle::TouchMoveHandle(): 13 winWidth(0), 14 player(NULL), 15 backBg(NULL), 16 centerBg(NULL), 17 backBgWidth(0), 18 centerBgWidth(0), 19 leftSideX(0), 20 rightSideX(0), 21 moveBehavior(behaviorUnKnow) 22 { 23 24 } 25 26 TouchMoveHandle::~TouchMoveHandle() 27 { 28 CCLog("~TouchMoveHandle dispose"); 29 } 30 31 bool TouchMoveHandle::init() 32 { 33 CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(TouchMoveHandle::playerCrollSideLineHandler),"playerIdle", NULL); 34 35 return true; 36 } 37 38 void TouchMoveHandle::destroy() 39 { 40 CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, "playerIdle"); 41 container->stopAllActions(); 42 backBg->stopAllActions(); 43 player->stopAllActions(); 44 player->playAction(Player::state_idle); 45 46 } 47 48 void TouchMoveHandle::handle(Player *_player, CCSprite *_backBg,CCSprite *_centerBg,CCLayer *_container,CCPoint touchPoint) 49 { 50 player = _player; 51 winWidth = CCDirector::sharedDirector()->getWinSize().width; 52 backBg = _backBg; 53 centerBg = _centerBg; 54 container = _container; 55 backBgWidth = backBg->getContentSize().width * backBg->getScaleX(); 56 centerBgWidth = centerBg->getContentSize().width * centerBg->getScaleX(); 57 this->targetPoint = ccp(touchPoint.x,player->getPosition().y); 58 leftSideX = winWidth/2; 59 rightSideX = centerBgWidth - winWidth/2; 60 61 float playerCurrentX = player->getPosition().x; 62 63 //玩家在地图左侧 64 if(playerCurrentX <= leftSideX) 65 { 66 //targetPoint也在左侧,就玩家动 67 if(targetPoint.x <= leftSideX) 68 { 69 moveBehavior = behaviorUnKnow; 70 player->runTo(targetPoint); 71 72 } 73 else //touchPoint在中间,先玩家跑到边界,过界后背景和玩家一起动 74 { 75 moveBehavior = leftToCenter; 76 player->runTo(getCrossSidePoint1()); 77 } 78 } 79 else if(playerCurrentX > leftSideX && playerCurrentX < rightSideX)//玩家在中间 80 { 81 //targetPoint在左侧,先玩家背景一起动,然后再只玩家动 82 if(targetPoint.x <= leftSideX) 83 { 84 CCPoint temp = getCrossSidePoint1(); 85 player->runTo(temp); 86 moveContainer1(temp); 87 moveBehavior = centerToLeft; 88 } 89 else if(targetPoint.x > leftSideX && targetPoint.x < rightSideX)//targetPoint在中间,一起动 90 { 91 player->runTo(targetPoint); 92 moveContainer1(targetPoint); 93 moveBehavior = behaviorUnKnow; 94 } 95 else if(targetPoint.x >= rightSideX)//targetPoint在右,先玩家跑到边界,过界后背景和玩家一起动 96 { 97 CCPoint temp = ccp(rightSideX,player->getPositionY()); 98 player->runTo(temp); 99 moveContainer1(temp); 100 moveBehavior = centerToRight; 101 } 102 } 103 else if(playerCurrentX >= rightSideX)//玩家在右侧 104 { 105 if(targetPoint.x < rightSideX)//targetPoint在中间 106 { 107 CCPoint temp = ccp(rightSideX,player->getPositionY()); 108 player->runTo(temp); 109 moveBehavior = rightToCenter; 110 } 111 else if(targetPoint.x >= rightSideX) 112 { 113 player->runTo(targetPoint); 114 moveBehavior = behaviorUnKnow; 115 } 116 } 117 118 119 } 120 121 void TouchMoveHandle::playerCrollSideLineHandler() 122 { 123 if(moveBehavior == leftToCenter) 124 { 125 moveBehavior = behaviorUnKnow; 126 player->runTo(targetPoint); 127 moveContainer1(targetPoint); 128 } 129 else if(moveBehavior == centerToLeft) 130 { 131 moveBehavior = behaviorUnKnow; 132 container->stopAllActions(); 133 container->setPositionX(0); 134 backBg->stopAllActions(); 135 backBg->setPositionX(0); 136 player->runTo(targetPoint); 137 } 138 else if(moveBehavior == centerToRight) 139 { 140 moveBehavior = behaviorUnKnow; 141 container->stopAllActions(); 142 container->setPositionX(winWidth-centerBgWidth); 143 backBg->stopAllActions(); 144 backBg->setPositionX(winWidth-backBgWidth); 145 player->runTo(targetPoint); 146 } 147 else if(moveBehavior == rightToCenter) 148 { 149 moveBehavior = behaviorUnKnow; 150 player->runTo(targetPoint); 151 moveContainer1(targetPoint); 152 } 153 154 } 155 156 CCPoint TouchMoveHandle::getCrossSidePoint1() 157 { 158 CCPoint crossPoint = ccp(leftSideX,player->getPositionY()); 159 return crossPoint; 160 } 161 162 void TouchMoveHandle::moveContainer1(CCPoint target) 163 { 164 container->stopAllActions(); 165 backBg->stopAllActions(); 166 167 const float SPEED = 12; 168 float currentPlayerPosX = player->getPosition().x; 169 float disX = target.x - currentPlayerPosX; 170 float DIS_X = abs(disX); 171 float containerTartgetX = 0; 172 if(disX < 0) 173 { 174 containerTartgetX = container->getPositionX() + DIS_X; 175 } 176 else 177 { 178 containerTartgetX = container->getPositionX() - DIS_X; 179 } 180 float playerMoveTime = (DIS_X / SPEED) / 60; 181 182 CCAction *action = CCSequence::create( 183 CCMoveTo::create(playerMoveTime, ccp(containerTartgetX,0)), 184 NULL); 185 container->runAction(action); 186 187 //背景层 188 //中景层从最大横坐标到最小横坐标之间的距离 189 float maxCenterBgDis = centerBgWidth - winWidth; 190 //背景层的移动距离 191 float maxBackBgDis = backBgWidth - winWidth; 192 float backBgMoveScale = maxBackBgDis / maxCenterBgDis; 193 float backBgTargetPosx = containerTartgetX * backBgMoveScale; 194 CCAction *backBgAction = CCSequence::create(CCMoveTo::create(playerMoveTime, ccp(backBgTargetPosx,backBg->getPosition().y)),NULL); 195 backBg->runAction(backBgAction); 196 }