如何在屏幕边缘改变移动精灵的方向?

时间:2014-10-09 12:53:46

标签: c++ cocos2d-x

我在cocos2d-x中使用此代码在特定方向拍摄,但在屏幕边缘,我的精灵(即我的射击气泡)曾经迷路。当它在屏幕边缘碰撞并改变方向上升时,我需要帮助改变它的角度。

使用cocos2dx的c ++代码: -

 CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
// Determinie offset of location to projectile
float offX = flocationX - m_pOwner->getPosition().x;
float offY = flocationY - m_pOwner->getPosition().y;

// Bail out if we are shooting down or backwards
if (offY <= 0) return;

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


// Determine where we wish to shoot the projectile to
//float realX = origin.x + winSize.width + (m_pOwner->getPosition().x);
float realY = origin.y + winSize.height + (m_pOwner->getPosition().y);
//    float ratio = offY / offX;
float ratio = offX / offY;
//float realY = (realX * ratio) + m_pOwner->getPosition().y;
float realX = (realY * ratio) + m_pOwner->getPosition().x;
CCPoint realDest = ccp(realX, realY);

// Determine the length of how far we're shooting
float offRealX = realX - m_pOwner->getPosition().x;
float offRealY = realY - m_pOwner->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;

// Move projectile to actual endpoint

m_pOwner->runAction( CCSequence::create(
                                        CCMoveTo::create(realMoveDuration, realDest),
                                        CCCallFuncN::create(getOwner()->getParent()->getComponent("SceneController"),
                                                            callfuncN_selector(SceneController::spriteMoveFinished)),
                                        NULL) );

0 个答案:

没有答案