cocos2dx v3.2 关于计算两点向量normalize问题

看了cocos2d-x 3.0制作2D横版格斗游戏,里面的摇杆operaterlayer的实现,其中
OperaterLayer.h

 

#ifndef _OPERATE_LAYER_ 
#define _OPERATE_LAYER_ 
#include "cocos2d.h" 

class OperateLayer : public cocos2d::Layer 
{ 
public: 
OperateLayer(); 
~OperateLayer(); 
virtual bool init(); 
CREATE_FUNC(OperateLayer); 

void onTouchesBegan(const std::vector& touches, cocos2d::Event *unused_event); 
void onTouchesMoved(const std::vector& touches, cocos2d::Event *unused_event); 
void onTouchesEnded(const std::vector& touches, cocos2d::Event *unused_event); 

private: 
void showJoystick(cocos2d::Point pos); 
void hideJoystick(); 
void updateJoystick(cocos2d::Point direction, float distance); 
cocos2d::Sprite* m_pJoystick; 
cocos2d::Sprite* m_pJoystickBg; 


}; 
#endif 


```
 

OperaterLyaer.cpp 
 

void OperateLayer::updateJoystick(Point direction, float distance) 
{ 
Point start = m_pJoystickBg->getPosition(); 
if (distance < 33) 
{ 
m_pJoystick->setPosition(start + (direction * distance)); 

}else if (distance>78) 
{ 
m_pJoystick->setPosition(start + (direction * 45)); 

}else 
{ 
m_pJoystick->setPosition(start + (direction * 33)); 

} 
} 

void OperateLayer::onTouchesBegan(const vector& touches, Event *unused_event) 
{ 
Size winSize = Director::getInstance()->getWinSize(); 
vector::const_iterator touchIter = touches.begin(); 
while(touchIter != touches.end()) 
{ 
Touch *pTouch = (Touch*)(*touchIter); 
Point p = pTouch->getLocation(); 
if(p.x <= winSize.width / 2) 
{ 
this->showJoystick(p); 
}else { 

} 

++ touchIter; 
} 
} 

void OperateLayer::onTouchesMoved(const vector& touches, Event *unused_event) 
{ 
Size winSize = Director::getInstance()->getWinSize(); 
std::vector::const_iterator touchIter = touches.begin(); 
Touch *pTouch = (Touch*)(*touchIter); 
Point start = pTouch->getStartLocation(); 
if(start.x > winSize.width / 2) 
{ 
return; 
} 
Point dest = pTouch->getLocation(); 
float distance = start.getDistance(dest); 
Point direction = (dest-start).normalize(); //这点为什么报错啊!!!! 
this->updateJoystick(direction, distance); 
} 

void OperateLayer::onTouchesEnded(const vector& touches, Event *unused_event) 
{ 
this->hideJoystick(); 
} 


```
 

为什么Point direction = (dest-start).normalize(); //这点为什么报错啊!!!! 


1 IntelliSense: 对象包含与成员函数不兼容的类型限定符 
对象类型是: const cocos2d::Vec2 c:\Users\Unity\Desktop\project\PompaDroid\Classes\OperateLayer.cpp 103 20 PompaDroid

有人解答么

暂时我是用 ccpNormalized()来顶替,
虽然说是“被废弃”,但还能作用

3.2 里面可以用么? Point direction = (dest-start).ccpNormalized(); 没有这个函数

可以用getNormalized()

谢谢 !~用你的方法运行可以了,哎 用3.2版本入门cocos2d果然有点难度 呵呵 十分感谢

3.2里point 都用vec2 代替 vec2的 Normalize 能用!!

Vec2 direction = (dest - start);
direction.normalize();