Cocos2d入门 <四> fire bullet
2013-01-16 16:24
381 查看
我们需要我们的英雄 来发射子弹消灭敌人。
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event) { // Choose one of the touches to work with CCTouch* touch = (CCTouch*)( touches->anyObject() ); CCPoint location = touch->locationInView(); location = CCDirector::sharedDirector()->convertToGL(location); // Set up initial location of projectile CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *projectile = CCSprite::create("Projectile.png", CCRectMake(0, 0, 20, 20)); projectile->setPosition( ccp(20, winSize.height/2) ); // Determinie offset of location to projectile int offX = location.x - projectile->getPosition().x; int offY = location.y - projectile->getPosition().y; // Bail out if we are shooting down or backwards if (offX <= 0) return; // Ok to add now - we've double checked position this->addChild(projectile); // Determine where we wish to shoot the projectile to int realX = winSize.width + (projectile->getContentSize().width/2); float ratio = (float)offY / (float)offX; int realY = (realX * ratio) + projectile->getPosition().y; CCPoint realDest = ccp(realX, realY); // Determine the length of how far we're shooting int offRealX = realX - projectile->getPosition().x; int offRealY = realY - projectile->getPosition().y; float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY)); float velocity = 480/1; // 480pixels/1sec float realMoveDuration = length/velocity; // Move projectile to actual endpoint projectile->runAction( CCSequence::actions(CCMoveTo::create(realMoveDuration, realDest), CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) ); } // on "init" you need to initialize your instance bool HelloWorld::init() { ...... this->setTouchEnabled(true); // Call game logic about every second this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0 ); return true; }
相关文章推荐
- Git的入门教程<四>
- IOS cocos2d学习笔记-<四>Menu&MenuItem
- Cocos2d入门 <一>建立一个新工程
- Cocos2d入门 <二> 添加一个精灵
- Cocos2d入门 <三>如何移动精灵角色
- Cocos2d入门 <五> 碰撞检测
- Cocos2d入门 <六> 播放声音
- Cocos2d入门 <七> 游戏结束
- cocos2d-x基础<四> Action
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(上)
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(中)
- quick-cocos2d-x的热更新机制实现<四>update包(lua)(下)
- Cocos2d-x3.0模版容器之一:cocos2d::Vector<T>
- 黑马程序员 java高新技术<四>--类加载器、动态代理技术的深入讲解与应用
- 进程控制理论<四>--消息队列和信号量
- ViewPager,RadioGroup,FragmentManager 详解 <四> Fragment,ViewPager 和TabLayout
- C++语言零基础入门教程:2.2 C/C++头文件大揭秘,""和<>有什么区别
- OpenCV学习教程入门篇<一、介绍>
- Windows核心编程<读书笔记四> 进程的概念
- <四>Ioc控制反转——为bean.xml瘦身第一步(附源码)