code-prettify

2014年10月13日 星期一

Cocos2d-x 3.2 - Chapter 4 - How to Fire some Bullets

Cocos2d-x 3.2 - Chapter 4 - How to Fire some Bullets

Cocos2d-x 3.2 - Step by Step Cocos2dxSimpleGame Series

接著上一篇 Cocos2d-x 3.2 - Chapter 3 - How to Move a sprite,我們增加了敵人之後。
接著,我們試著發射子彈來殺死敵人。

首先,先開啟觸控事件,在 init 函式最後加入下列程式碼:


 //Create a "one by one" touch event listener (processes one touch at a time)
 auto listener1 = EventListenerTouchOneByOne::create();

 // When "swallow touches" is true, then returning 'true' from the onTouchBegan method will "swallow" the touch event, preventing other listeners from using it.
 listener1->setSwallowTouches(true);

 // Example of using a lambda expression to implement onTouchBegan event callback function
 listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
 listener1->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);


然後實作 onTouchBegan 及 onTouchEnded 兩個函式:



bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
 return true;
}

void HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
 // Choose one of the touches to work with
 Point location = touch->getLocationInView();
 location = Director::getInstance()->convertToGL(location);
 addProjectile(location.x, location.y);
}

最後是 addProjectile 函式



void HelloWorld::addProjectile(float x, float y)

{

 // Set up initial location of projectile

 Size winSize = Director::getInstance()->getWinSize();

 Sprite *projectile = Sprite::create("Projectile.png", Rect(0, 0, 20, 20));

 projectile->setPosition(Point(20, winSize.height / 2));



 // Determinie offset of location to projectile

 int offX = x - projectile->getPosition().x;

 int offY = y - projectile->getPosition().y;



 // Bail out if we are shooting down or backwards

 if (offX <= 0) return;



 // Ok to add now - we've double checked position

 this->addChild(projectile);



 // Determine where we wish to shoot the projectile to

 int realX = winSize.width

  + (projectile->getContentSize().width / 2);

 float ratio = (float)offY / (float)offX;

 int realY = (realX * ratio) + projectile->getPosition().y;

 Point realDest = Point(realX, realY);



 // Determine the length of how far we're shooting

 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;



 // Move projectile to actual endpoint

 projectile->runAction(CCSequence::create(

  CCMoveTo::create(realMoveDuration, realDest),

  CCCallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1)), NULL));



 projectile->runAction(CCSequence::create(

  CCMoveTo::create(realMoveDuration, realDest),

  CCCallFuncN::create(std::bind(&HelloWorld::spriteMoveFinished, this, std::placeholders::_1)), NULL));

}

完成圖:



參考資料:
Cocos2d-x
http://www.cocos2d-x.org/

Step by Step Cocos2dxSimpleGame Series (Old Version for cocos2d-x v2.x)
http://www.cocos2d-x.org/wiki/Step_by_Step_Cocos2dxSimpleGame_Series

EventDispatcher Mechanism
http://www.cocos2d-x.org/wiki/EventDispatcher_Mechanism

沒有留言:

張貼留言