步骤如下:
1.使用上一篇的工程;
2.下载本游戏所需的资源,将资源放置" Resources"目录下:
删除旧的资源 player.png和 projectile.png;
3.在 HelloWorldScene.cpp文件, init函数,修改创建玩家精灵:
1
|
|
CCSprite *player = CCSprite::create( "player2.png");
|
1
|
|
CCSprite *projectile = CCSprite::create( "projectile2.png");
|
5.接下去,就是让炮塔可以旋转射击。在 HelloWorldScene.h文件中,添加如下代码:
1
|
|
cocos2d::CCSprite *_player;
|
1
2 3 |
|
_player = CCSprite::create( "player2.png");
_player->setPosition(ccp(_player->getContentSize().width / 2, winSize.height / 2)); this->addChild(_player); |
数学的知识就是,tan(angle) = 对边 / 邻边,利用反正切angle = arctan(对边 / 邻边),这时计算出的是弧度,用CC_RADIANS_TO_DEGREES宏转换成角度。另外在数学中,逆时针为正,在Cocos2D-x中,顺时针为正,就如下图所示:
需要将最后计算出的角度乘以-1。在 ccTouchesEnded函数里,添加如下代码在projectile精灵runAction之前:
1
2 3 4 |
|
float angleRadians = atanf(( float)offRealY / ( float)offRealX);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); float cocosAngle = - 1 * angleDegrees; _player->setRotation(cocosAngle); |
8.旋转再射击。炮塔的旋转是瞬间完成的,这不符合现实,需要让它有个动作移动炮塔的方向。在 HelloWorldScene.h文件中,添加如下声明:
1
|
|
cocos2d::CCSprite *_nextProjectile;
|
1
2 |
|
_player = NULL;
_nextProjectile = NULL; |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{ if (_nextProjectile != NULL) { return; } CCTouch *touch = (CCTouch*)pTouches->anyObject(); CCPoint location = this->convertTouchToNodeSpace(touch); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); _nextProjectile = CCSprite::create( "projectile2.png"); _nextProjectile->retain(); _nextProjectile->setPosition(ccp( 20, winSize.height / 2)); CCPoint offset = ccpSub(location, _nextProjectile->getPosition()); if (offset.x <= 0) { return; } int realX = winSize.width + _nextProjectile->getContentSize().width / 2; float ratio = ( float)offset.y / ( float)offset.x; int realY = realX * ratio + _nextProjectile->getPosition().y; CCPoint realDest = ccp(realX, realY); int offRealX = realX - _nextProjectile->getPosition().x; int offRealY = realY - _nextProjectile->getPosition().y; float length = sqrtf(offRealX * offRealX + offRealY * offRealY); float velocity = 480 / 1; float realMoveDuration = length / velocity; float angleRadians = atanf(( float)offRealY / ( float)offRealX); float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); float cocosAngle = - 1 * angleDegrees; float rotateDegreesPerSecond = 180 / 0. 5; float degreesDiff = _player->getRotation() - cocosAngle; float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond); _player->runAction(CCSequence::create(CCRotateTo::create(rotateDuration, cocosAngle), CCCallFunc::create( this, callfunc_selector(HelloWorld::finishShoot)), NULL)); _nextProjectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, realDest), CCCallFuncN::create( this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL)); _nextProjectile->setTag( 2); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect( "pew-pew-lei.wav"); } void HelloWorld::finishShoot() { this->addChild(_nextProjectile); _projectiles->addObject(_nextProjectile); _nextProjectile->release(); _nextProjectile = NULL; } |
9.编译运行,可以看到炮塔可以在旋转后进行射击了,如下图所示: