cocos2d.x单点触摸事件

前端之家收集整理的这篇文章主要介绍了cocos2d.x单点触摸事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

#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 instance
bool 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" class="cpp">//setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
//然后利用函数onTouchBagan(<span style="font-family: Arial,Helvetica,sans-serif;">Touch *pTouches,CCEvent *pEvent)</span>
<span style="font-family: Arial,sans-serif;">//利用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} 原文链接:https://www.f2er.com/cocos2dx/342308.html

猜你在找的Cocos2d-x相关文章