[Cocos2d-x For WP8]点击移动精灵

点击移动是游戏里面常用的操作,那么在Cocos2d-x里面可以通过setTouchEnabled(true)方法来设置接收屏幕的点击事件,然后添加ccTouchesEnded(CCSet* touches, CCEvent* event)方法处理点击后的操作,在方法体里面就可以获取点击的位置,然后通过动画的方式来移动精灵。

代码示例:

//在init方法里面初始化当前的实例
bool HelloWorld::init()
{
bool bRet = false; do
{
//CCLayer进行初始化,初始化失败跳出循环
if ( !CCLayer::init() )
{
break;
}
//获取手机屏幕的大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* sprite = CCSprite::create("cat.png");
CCLayer* layer = CCLayerColor::create(ccc4(,,,));//();
addChild(layer, -);
addChild(sprite, , );
sprite->setPosition( CCPointMake(,) );
//跳的动画
sprite->runAction( CCJumpTo::create(, CCPointMake(,), , ) );
//页面颜色闪动的动画
layer->runAction( CCRepeatForever::create(
(CCActionInterval*)( CCSequence::create(
CCFadeIn::create(),
CCFadeOut::create(),
NULL) )
) );
//接收界面的的触摸事件
setTouchEnabled(true);
bRet = true;
} while ();
//返回成功
return bRet;
} void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
CCSetIterator it = touches->begin();
CCTouch* touch = (CCTouch*)(*it);
//获取点击的位置
CCPoint location = touch->getLocationInView();
//把UIkit坐标转化为GL坐标
CCPoint convertedLocation = CCDirector::sharedDirector()->convertToGL(location);
//获取节点
CCNode* s = getChildByTag();
//移除所有的动画
s->stopAllActions();
//启动移动动画
s->runAction( CCMoveTo::create(, CCPointMake(convertedLocation.x, convertedLocation.y) ) );
float o = convertedLocation.x - s->getPosition().x;
float a = convertedLocation.y - s->getPosition().y;
float at = (float) CC_RADIANS_TO_DEGREES( atanf( o/a) ); if( a < )
{
if( o < )
at = + fabs(at);
else
at = - fabs(at);
}
//转动动画
s->runAction( CCRotateTo::create(, at) );
}

运行的效果:

[Cocos2d-x For WP8]点击移动精灵 [Cocos2d-x For WP8]点击移动精灵

上一篇:navicat for mysql远程连接ubuntu服务器的mysql数据库


下一篇:2014--9=17 软工二班 MyEclipse blue==3