stbyruby 发表于 2015-12-30 14:45:38

MAC OS 下学习 cocos2d-x (2)

  参考地址
  http://blog.csdn.net/akof1314/article/details/8287885
  http://www.raywenderlich.com/25791/rotating-turrets-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-2
  用到的资源文件
  http://cdn4.raywenderlich.com/downloads/Cocos2DSimpleGameResourcePack2.zip
  退出XCODE后,我复制了第一份的代码,文件夹名改为SimpleGame2
  先下载资源文件,是四个png文件,分别是炮台 炮台高清子弹 子弹高清图片,把他们加入到resources 文件夹
  现在开始修改代码:
  把原来在HelloWorldScene.cpp文件,init函数加载的图片改成 player2.png



1 CCSprite *player = CCSprite::create("player2.png",CCRectMake(0, 0, 27, 70));
  然后在ccTouchesEnded函数,修改创建子弹精灵:



1 CCSprite *projectile = CCSprite::create("projectile2.png");
  很简单,就是改下加载的图片名
  运行,忍者已经替换为炮台,飞镖也替换成了子弹,但是炮台还不会旋转.接下来让炮台可以旋转
  在HelloWorldScene.h文件中,添加 炮台精灵全局变量 代码:



1   cocos2d::CCSprite *_player;
  修改HelloWorldScene.cpp文件中的init函数,如下:



    _player = CCSprite::create("player2.png");
//设定精灵的位置为 左边正中间
_player->setPosition(ccp(_player->getContentSize().width / 2, size.height / 2));
//将精灵加入场景
this->addChild(_player);
  计算炮塔旋转的角度的数学公式
  数学的知识就是,tan(angle) = 对边 / 邻边,利用反正切angle = arctan(对边 / 邻边),这时计算出的是弧度,用CC_RADIANS_TO_DEGREES宏转换成角度。另外在数学中,逆时针为正,在Cocos2D-x中,顺时针为正,
  需要将最后计算出的角度乘以-1。在ccTouchesEnded函数里,添加如下代码在projectile精灵runAction之前,代码int offRealY = realY - projectile->getPosition().y; 之后
  



1   
2   //计算炮台旋转角度
3   float angleRadians = atanf((float)offRealY / (float)offRealX);
4   float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
5   float cocosAngle = -1 * angleDegrees;
6   _player->setRotation(cocosAngle);
  现在编译运行,炮台已经可以旋转设计了.
  旋转再射击。炮塔的旋转是瞬间完成的,这不符合现实,需要让它有个动作移动炮塔的方向。在HelloWorldScene.h文件中,添加如下声明:



1 cocos2d::CCSprite *_nextProjectile;
  在构造函数HelloWorld::HelloWorld()里添加如下:



1 _player = NULL;
2 _nextProjectile = NULL;
  
  添加finishShoot方法,



1 void HelloWorld::finishShoot()
2 {
3   this->addChild(_nextProjectile);
4   _projectiles->addObject(_nextProjectile);
5
6   _nextProjectile->release();
7   _nextProjectile = NULL;
8 }
  
  修改ccTouchesEnded函数,代码如下:
  这里主要就是CCRotateTo旋转的使用



1 void HelloWorld::ccTouchesEnded(cocos2d::CCSet* pTouches, cocos2d::CCEvent * pEvent)
2 {
3   if (_nextProjectile != NULL) {
4   return;
5   }
6   
7   //得到触摸点
8   CCTouch *touch = (CCTouch*)pTouches->anyObject();
9   CCPoint location = this->convertTouchToNodeSpace(touch);
10   
11   CCSize size = CCDirector::sharedDirector()->getWinSize();
12   
13   //创建一个飞镖精灵
14   _nextProjectile= CCSprite::create("projectile2.png");
15   _nextProjectile->retain();
16   _nextProjectile->setPosition(ccp(20, size.height / 2));
17
18   
19   CCPoint offset = ccpSub(location, _nextProjectile->getPosition());
20   if (offset.x <= 0) {
21   return;
22   }
23
24   //各种计算
25   int realX = size.width + _nextProjectile->getContentSize().width / 2;
26   float ratio = (float)offset.y / (float)offset.x;
27   int realY = realX * ratio + _nextProjectile->getPosition().y;
28   CCPoint realDest = ccp(realX, realY);
29   
30   int offRealX = realX - _nextProjectile->getPosition().x;
31   int offRealY = realY - _nextProjectile->getPosition().y;
32   
33
34   
35   
36   //计算炮台旋转角度
37   float angleRadians = atanf((float)offRealY / (float)offRealX);
38   float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
39   float cocosAngle = -1 * angleDegrees;
40   
41   //旋转的速度
42   float rotateDegreesPerSecond = 180 / 0.5;
43   float degreesDiff = _player->getRotation() - cocosAngle;
44   float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond);
45
46
47   
48   //用三角攻击算出飞镖的移动距离
49   float length = sqrtf(offRealX * offRealX + offRealY * offRealY);
50   float velocity = 480 / 1;
51   float realMoveDuration = length / velocity;
52   //指定飞镖的移动轨迹,为忍着和触摸点之前的一条线,移动距离
53
54
55   //炮台执行一个旋转动作,并在旋转动作完成之后,执行一个finishShoot函数把子弹加入数组
56   _player->runAction(CCSequence::create(CCRotateTo::create(rotateDuration, cocosAngle),
57                                       CCCallFunc::create(this, callfunc_selector(HelloWorld::finishShoot)),NULL));
58   
59   _nextProjectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, realDest),
60                                          CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL));
61   
62   _nextProjectile->setTag(2);
63   
64   CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.caf");
65   
66   
67 }
  执行程序,完成.
  
页: [1]
查看完整版本: MAC OS 下学习 cocos2d-x (2)