Follow 在地图中使地图和人物一起运动

Follow  在地图中使地图和人物一起运动

 1 bool HelloWorld::init()
 2 {
 3     //////////////////////////////
 4     // 1. super init first
 5     if ( !Layer::init() )
 6     {
 7         return false;
 8     }
 9 
10     Size visibleSize = Director::getInstance()->getVisibleSize();
11     Vec2 origin = Director::getInstance()->getVisibleOrigin();
12 
13     // add "HelloWorld" splash screen"
14     auto sprite = Sprite::create("HelloWorld.png");
15 
16     // position the sprite on the center of the screen
17     sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
18 
19     // add the sprite as a child to this layer
20     this->addChild(sprite, 0);
21 
22 
23     ///////// Follow  在地图中使地图和人物一起运动  //////////
24 
25 
26     Sprite* sprite1 = Sprite::create("button.png");
27     sprite1->setPosition(Vec2(visibleSize.width * 0.2, visibleSize.height * 0.5));
28     this->addChild(sprite1);
29 
30     Sprite* sprite2 = Sprite::create("CloseNormal.png");
31     sprite2->setPosition(Vec2(visibleSize.width * 0.8, visibleSize.height * 0.8));
32     this->addChild(sprite2);
33 
34     ActionInterval *move = MoveTo::create(2, Vec2(visibleSize.width * 0.4, visibleSize.height * 0.5));
35 
36     // 参数(跟随的节点,跟随的范围)(sprite1:跟随精灵, CCRectZero:一直跟随);
37     Follow *follow = Follow::create(sprite1, CCRectZero);
38 
39     log("sprite1  %f, %f", sprite1->getPositionX(), sprite1->getPositionY());
40     log("sprite2  %f, %f", sprite2->getPositionX(), sprite2->getPositionY());
41 
42     ActionInstant *funcN = CallFuncN::create(this, callfuncN_selector(HelloWorld::funcNCallBack));
43     //sprite1->setTag(111);
44     sprite1->runAction(Sequence::create(move ,funcN,nullptr));
45 
46     sprite2->runAction(follow);
47 
48     ///////// follow  在地图中使地图和人物一起运动  //////////
49 
50     return true;
51 }
52 
53 
54 // 获取精灵的回调函数;精灵变大 3 倍
55 void HelloWorld::funcNCallBack(Node *pSender){
56 
57     Sprite* sprite = (Sprite*)pSender;
58     ActionInterval *scale = ScaleTo::create(1.0f, 3.0f);
59     sprite->runAction(scale);
60 
61     log("sprite  %f, %f", sprite->getPositionX(), sprite->getPositionY());
62 
63 }

控制台显示内容

sprite1  192.000000, 320.000000
sprite2  768.000000, 512.000000

sprite1  384.000000, 320.000000

原文地址:https://www.cnblogs.com/dudu580231/p/4555657.html