第1课的地址:
http://blog.csdn.net/xingxiliang/article/details/18557631
书接上回:今天我们让我们的主角可以发射飞镖。
avi版本可以方便的在手机,pc上查看。下载地址(只有20M左右,下载很快):http://pan.baidu.com/s/1ELk78
this->setTouchEnabled(true); // 在HelloWorld::init函数中 void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) // 触摸事件响应函数 { CCTouch* touch = (CCTouch*)pTouches->anyObject(); CCPoint locInView = touch->getLocationInView(); CCPoint loc = CCDirector::sharedDirector()->convertToGL(locInView); //从UI坐标变换到cococs2d中的坐标 if (loc.x <= 20)// 触摸点在出发点左侧不处理 { return ; } CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize(); CCSprite* proj = CCSprite::create("Projectile.png"); proj->setPosition(ccp(20, screenSize.height / 2.0)); this->addChild(proj); //创建飞镖精灵 double dx = loc.x - 20; double dy = loc.y - screenSize.height / 2.0; double d = sqrt(dx * dx + dy * dy); // 触摸点到出发点的距离 double D = sqrt(screenSize.width * screenSize.width + screenSize.height * screenSize.height); // 总的运行距离 double ratio = D / d; double endx = ratio * dx + 20; // 最终点的x坐标 double endy = ratio * dy + screenSize.height / 2.0;// 最终点的y坐标 CCMoveTo* move = CCMoveTo::create(D / 320, ccp(endx, endy)); CCCallFuncN* moveFinish = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::myDefine)); CCSequence* actions = CCSequence::create(move, moveFinish, NULL); proj->runAction(actions); }
转载请注明出处:http://blog.csdn.net/xingxiliang/article/details/18599871