The procedure is as follows:
1. Use the previous project;
2. Download the Resources required for this game and place the Resources under the Resources directory:
Delete old resources player.pngand projectile.png;
3. In the HelloWorldScene. cpp file, modify the init function to create a gamer genie:
_player = Sprite::create(player2.png);
In the onTouchesEnded function, modify and create the bullet genie:
Sprite* projectile = Sprite::create(projectile2.png);
4. Compile and run the task. You can see that the turret has shot a bullet, but it is strange that when shooting, the turret is not in that direction, as shown in:
5. Calculate the turret rotation angle. See the figure below:
The mathematical knowledge is that tan (angle) = the opposite side/adjacent side, use the arc tangent angle = arctan (the opposite side/adjacent side), then calculate the radian, and use the CC_RADIANS_TO_DEGREES macro to convert the angle. In mathematics, the Clockwise is positive, in the Cocos2D-x, clockwise is positive, as shown in:
Multiply the final calculated angle by-1. In the onTouchesEnded function, add the following code before the projectile genie runAction:
float angleRadians = atanf((float)offRealY / (float)offRealX); float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); float cocosAngle = -1 * angleDegrees; _player->setRotation(cocosAngle);
7. Compile and run the task. Now you can see the turret rotation and shooting. As shown in:
8. Rotate before shooting. The turret rotation is completed in an instant, which is not in line with reality and requires an action to move the turret. In the HelloWorldScene. h file, add the following statement:
Sprite* _nextProjectile;
In the constructor, add the following:
_player = NULL; _nextProjectile = NULL;
Modify the onTouchesEnded function and add
FinishShootThe Code is as follows:
void HelloWorld::onTouchesEnded(const std::vector
&touches, cocos2d::Event *event){ if (_nextProjectile != NULL) { return; } Touch* touch = touches.front(); Point location = this->convertTouchToNodeSpace(touch); Size winSize = Director::getInstance()->getWinSize(); _nextProjectile = Sprite::create(projectile2.png); _nextProjectile->retain(); _nextProjectile->setScale(2); _nextProjectile->setPosition(Point(20, winSize.height / 2)); Point 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; Point realDest = Point(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); RotateTo* rotate = RotateTo::create(rotateDuration, cocosAngle); CallFunc* callFc = CallFunc::create(std::bind(&HelloWorld::finishShoot, this)); _player->runAction(Sequence::create(rotate,callFc, NULL)); _nextProjectile->runAction(Sequence::create(MoveTo::create(realMoveDuration, realDest), CallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this,_nextProjectile)), NULL)); _nextProjectile->setTag(2); }void HelloWorld::finishShoot(){ this->addChild(_nextProjectile); _projectiles->addObject(_nextProjectile); _nextProjectile->release(); _nextProjectile = NULL;}
Check the _ nextProjectile variable at the beginning of the function. If it is not empty, the turret is being rotated. Do not add _ nextProjectile to the scenario immediately, and wait until the rotation is complete before joining the scenario. The rotation speed of the turret. The rotation speed is half a circle in half a second, and the time required to calculate the rotation angle.
9. Compile and run the task. You can see that the turret is shot after rotation, as shown in:
References:
1. How To Make A Simple iPhone Game with Cocos2D 2.X Part 2 http://www.raywenderlich.com/25791/rotating-turrets-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-2
2. How to Use cocos2d to develop a simple iphone game: rotating turret. Http://www.cnblogs.com/zilongshanren/archive/2011/03/28/1997820.html (Part 2)
Thank you very much for the above materials. In this example, additional resources are added to the source code.
: Http://pan.baidu.com/s/16Bp7h
If there is an error in the article, please point it out for correction. You are welcome to leave a message to discuss the technology!