Cocos2d-x 单点触摸--让精灵随手指移动起来

本文介绍了如何使用Cocos2d-x框架实现单点触摸功能,让精灵随着用户的触摸点在屏幕上移动。通过开启触摸事件、注册触摸调度器并重写触摸开始、移动和结束的回调函数,可以实现这一交互效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处:http://blog.youkuaiyun.com/oyangyufu/article/details/25656673

效果图:


 

CCTouch类装载了触摸点的信息,包括触摸点的横纵坐标值和触摸点的ID号,如获取触摸点转GL坐标:

	CCPoint point = pTouch->getLocationInView();
	point = CCDirector::sharedDirector()->convertToGL(point);


创建触摸事件流程:首先开启setTouchEnabled(true), 然后重写registerWithTouchDispatcher调用触摸代理函数addTargetedDelegate允许布景层接收触摸事件,再重写ccTouchBegan、ccTouchMoved、ccTouchEnded、ccTouchCancelled函数

 

程序代码:

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);

	setTouchEnabled(true);
	sp1 = CCSprite::create("cpp1.png");
	sp1->setScale(0.5f);
	sp1->setPosition(ccp(100, 200));
	this->addChild(sp1);


    return true;
}

//触摸移动
void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event)
{	
	if (iscontrol)
	{
		CCPoint location = touch->getLocationInView();
		location = CCDirector::sharedDirector()->convertToGL(location);
		CCLOG("ccTouchMoved...x:%f y:%f", location.x, location.y);

		//移动时重新设置sprite坐标
		float x = location.x-deltax;
		float y = location.y-deltay;
		sp1->setPosition(ccp(x, y));
	}

}
	
//触摸开始点击,计算该点坐标与sprite坐标差值
bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	
	
	CCPoint pos = sp1->getPosition();
	CCPoint location = touch->getLocationInView();
	location = CCDirector::sharedDirector()->convertToGL(location);//openGL
	CCLOG("ccTouchBegan...x:%f y:%f", location.x, location.y);
	
	if (location.x > 0 && location.x <960 &&
		location.y >0 && location.y < 640)//触摸的矩形区域
	{
		iscontrol = true;
		//计算触摸点与sprite的坐标差值
		deltax = location.x-pos.x;
		deltay = location.y-pos.y;
	}
	
	return true;

}
//触摸结束
void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
	CCLOG("ccTouchEnded...");
	//iscontrol = false;

}

//注册触摸事件
void HelloWorld::onEnter()
{
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
	CCLayer::onEnter();
}

void HelloWorld::onExit()
{
	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->removeDelegate(this);
	CCLayer::onExit();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值