上一篇地址:http://www.cocoachina.com/bbs/read.php?tid=197159
- The Shout
吼
吼,你也可以称之为杀敌技能。就像上古卷轴5里的龙裔。现在我们添加一个新的按钮在转换移动和吼叫模式。
添加这些代码删除那些已经移动出瓦片地图外的吼叫。
In the ‘HelloWorldScene.h’:
void projectileMoveFinished(cocos2d::Object *pSender);
```
In the ‘HelloWorldScene.cpp’:
void HelloWorld::projectileMoveFinished(Object *pSender)
{
Sprite *sprite = (Sprite *)pSender;
this->removeChild(sprite);
}
void HelloWorld::onTouchEnded(Touch *touch, Event *unused_event)
{
if (_mode == 0) {
``````
// the original code we have typed
``````
} else {
auto touchLocation = touch->getLocation();
touchLocation = this->convertToNodeSpace(touchLocation);
auto projectile = Sprite::create("bullet.png");
projectile->setPosition(_player->getPosition());
projectile->setScale(0.25);
this->addChild(projectile);
int realX;
auto diff = touchLocation - _player->getPosition();
if (diff.x > 0)
{
realX = (_tileMap->getMapSize().width * _tileMap->getTileSize().width) +
(projectile->getContentSize().width / 2);
}
else {
realX = -(_tileMap->getMapSize().width * _tileMap->getTileSize().width) -
(projectile->getContentSize().width / 2);
}
float ratio = (float)diff.y / (float)diff.x;
int realY = ((realX - projectile->getPosition().x) * ratio) +projectile->getPosition().y;
auto realDest = Point(realX, realY);
int offRealX = realX - projectile->getPosition().x;
int offRealY = realY - projectile->getPosition().y;
float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
float velocity = 480 / 1; // 480pixels/1sec
float realMoveDuration = length / velocity;
auto actionMoveDone =CallFuncN::create(CC_CALLBACK_1(HelloWorld::projectileMoveFinished, this));
projectile->runAction(Sequence::create(MoveTo::create(realMoveDuration,realDest), actionMoveDone, NULL))
_projectiles.pushBack(projectile);
}
}
```
首先我们获得手指碰触的地点。并且创建吼叫的图片精灵。之后,我们决定应该方向,放行时间。
现在我们实现了我们想要的功能:转换的按钮和吼叫。但是我们也能看到问题:即使我们的攻击技能吼叫碰触到了敌人,我们的敌人也没有受损。
The button & Shout
12. Collision detect
碰撞检测
In the ‘HelloWorldScene.h’:
cocos2d::Vector _enemies;
cocos2d::Vector _projectiles;
```
In the ‘HelloWorldScene.cpp’:
Add those codes at the end of the launch projectiles section of ‘onTouchEnded’:
_projectiles.pushBack(projectile);
```
Add those codes at the end of the ‘projectileMoveFinished’:
_projectiles.eraseObject(sprite);
```
Add those codes at the end of the ‘addEnemyAtPos’:
_enemies.pushBack(enemy);
```
void HelloWorld::testCollisions(float dt)
{
Vector projectilesToDelete;
for (Sprite *projectile : _projectiles) {
auto projectileRect = Rect(
projectile->getPositionX() - projectile->getContentSize().width / 2,
projectile->getPositionY() - projectile->getContentSize().height / 2,
projectile->getContentSize().width,
projectile->getContentSize().height);
Vector targetsToDelete;
for (Sprite *target : _enemies) {
auto targetRect = Rect(
target->getPositionX() - target->getContentSize().width / 2,
target->getPositionY() - target->getContentSize().height / 2,
target->getContentSize().width,
target->getContentSize().height);
if (projectileRect.intersectsRect(targetRect)) {
targetsToDelete.pushBack(target);
}
}
for (Sprite *target : targetsToDelete) {
_enemies.eraseObject(target);
this->removeChild(target);
}
if (targetsToDelete.size() > 0) {
// add the projectile to the list of ones to remove
projectilesToDelete.pushBack(projectile);
}
targetsToDelete.clear();
}
for (Sprite *projectile : projectilesToDelete) {
_projectiles.eraseObject(projectile);
this->removeChild(projectile);
}
projectilesToDelete.clear();
}
```
Finally, we type these code in the
bool HelloWorld::init()
{
``````
this->schedule(schedule_selector(HelloWorld::testCollisions));
return true;
}
我们首先获得吼叫图片和敌人图片的大小,然后确认这些图片时候碰撞到了对方。如果碰触到了,就将其加入数组,并将其在这些数组种的对象删除。
Now it should working like this:
Only one left
完~

依然是我


,又带坏小朋友~~~
