首页 > 代码库 > cocos2d 主角更随触屏走
cocos2d 主角更随触屏走
bool HelloWorld::init(){ if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); hero= CCSprite::create("1.png"); hero->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(hero, 0); CCDirector* pDirector=CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true); return true;}bool HelloWorld::ccTouchBegan(CCTouch* touch,CCEvent* event){ CCPoint heropos=hero->getPosition(); CCPoint location=touch->getLocationInView(); location=CCDirector::sharedDirector()->convertToGL(location); if(location.x>heropos.x-32&&location.x<heropos.x+32&& location.y>heropos.y-32&&location.y<heropos.y+32) { isControl=true; deltax=location.x-heropos.x; deltay=location.y-heropos.y; } return true;}void HelloWorld::ccTouchMoved(CCTouch* touch,CCEvent* event){ if(isControl) { CCPoint location=touch->getLocationInView(); location=CCDirector::sharedDirector()->convertToGL(location); float x=location.x-deltax; float y=location.y-deltay; hero->setPosition(ccp(x,y)); }}
cocos2d 主角更随触屏走
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。