cocos2d.x单点触摸事件

cocos2d.x单点触摸事件,第1张

概述今天在调简单的单点触摸出发人物发子d的时候,发现教程里面的代码不能使用。 #include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){ // 'scene' is an autorelease object auto scene = Scene::create(); /

今天在调简单的单点触摸出发人物发子d的时候,发现教程里面的代码不能使用。

#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease object    auto layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first	if ( !Layercolor::initWithcolor(ccc4(255,255,255)) )//红 绿 蓝 Alpha(透明度)    {        return false;    }#if 0        Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->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    auto closeItem = MenuItemImage::create(                                           "Closenormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));    	closeItem->setposition(Vec2(origin.x + visibleSize.wIDth - closeItem->getContentSize().wIDth/2,origin.y + closeItem->getContentSize().height/2));    // create menu,it's an autorelease object    auto menu = Menu::create(closeItem,NulL);    menu->setposition(Vec2::ZERO);    this->addChild(menu,1);    /////////////////////////////    // 3. add your codes below...    // add a label shows "Hello World"    // create and initialize a label        auto label = Label::createWithTTF("Hello World","Fonts/Marker Felt.ttf",24);        // position the label on the center of the screen    label->setposition(Vec2(origin.x + visibleSize.wIDth/2,origin.y + visibleSize.height - label->getContentSize().height));    // add the label as a child to this layer    this->addChild(label,1);    // add "HelloWorld" splash screen"    auto sprite = Sprite::create("HelloWorld.png");    // position the sprite on the center of the screen    sprite->setposition(Vec2(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));    // add the sprite as a child to this layer    this->addChild(sprite,0); #endif  	CCSize screenSize=CCDirector::sharedDirector()->getVisibleSize();//在导演类中获取屏幕高度    CCSprite* player=CCSprite::create("Player.png");	player->setposition(ccp(0+20,screenSize.height/2)); 	this->addChild(player);	#if 0	CcmenuItemImage* item=CcmenuItemImage::create("button2.png","button2.png",this,menu_selector(HelloWorld::responseFunc));	item->setposition(ccp(30,30));	Ccmenu* menu=Ccmenu::create(item,NulL);	this->addChild(menu);#endif	this->schedule(schedule_selector(HelloWorld::gameLogic),2);	this->settouchEnabled(true);	settouchMode(touch::dispatchMode::ONE_BY_ONE);//原本这一部分是没有的	return true;}bool HelloWorld::ontouchBegan(touch *ptouches,CCEvent *pEvent)//教程里面使用的是voID HelloWorld::cctouchesEnded这个函数,不过我在使用的时候
//发现该cctouchesEnded已经是Layer类里面已经定义的了,然后我就直接把他改成voID Layer::cctouchesEnded,但是发现他出现的错误是说cctouchesEnded已经
//有主体,所以我又翻了一下书,发现可以直接先使用<pre name="code" >//settouchMode(touch::dispatchMode::ONE_BY_ONE);
//然后利用函数ontouchBagan(<span >touch *ptouches,CCEvent *pEvent)</span>
<span >//利用getLocation()获取触摸点的坐标。</span>
{//touch* touch=(touch*)ptouches->getLocation();Vec2 location=ptouches->getLocation();//Point LocinVIEw=touch->getLocationInVIEw();//Point loc=Director::sharedDirector()->convertToGL(LocinVIEw);if(location.x<=20){return 1;}CCSize screenSize=CCDirector::sharedDirector()->getVisibleSize();CCSprite* proj=CCSprite::create("Projectile.png");proj->setposition(ccp(20,screenSize.height/2.0));this->addChild(proj);double dx=location.x-20;double dy=location.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;double endy=ratio*dy+screenSize.height/2.0;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);}voID HelloWorld::gameLogic(float dt){this->createTarget();}voID HelloWorld::createTarget(){CCSize screenSize=CCDirector::sharedDirector()->getVisibleSize();CCSprite* target=CCSprite::create("Target.png");int y=rand()%(int)screenSize.height;target->setposition(ccp(screenSize.wIDth-20,y));this->addChild(target);CCMoveto* move=CCMoveto::create(2,ccp(0,y));CCCallFuncN* disappear=CCCallFuncN::create(this,disappear,NulL);target->runAction(actions);}voID HelloWorld::responseFunc(Ref* obj){//cclOG("menu item clicked");CCMoveto* move=CCMoveto::create(2,40));CCCallFuncN* disappear=CCCallFuncN::create(this,NulL);target->runAction(actions);}voID HelloWorld::mydefine(Node* who){//who->setposition(ccp(10,10));//who->setScale(2);who->removeFromParentAndCleanup(true);}voID HelloWorld::menuCloseCallback(Ref* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)MessageBox("You pressed the close button. windows Store Apps do not implement a close button.","Alert"); return;#endif Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif} 总结

以上是内存溢出为你收集整理的cocos2d.x单点触摸事件全部内容,希望文章能够帮你解决cocos2d.x单点触摸事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063872.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存