【新手】cocos2d-x监听不到触屏动作,求救!

luo4188 2014-04-19 08:13:18
不知道为什么监听不到触屏,是按照教程写的代码。
用的是
ccTouchesBegan(CCTouch *pTouch, CCEvent *pEvent);

开发环境:
VS2013,cocos2d-x v2.2.3

求助好心人帮忙,代码如下;

HelloWorldScene.h

//HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#define CC_ENABLE_BOX2D_INTEGRATION 1
#define CC_ENABLE_CHIPMUNK_INTEGRATION 0

#pragma once
#include "cocos2d.h"
#include "Box2D\Box2D.h"
#include "CCPhysicsSprite.h"

#define RATIO 48.0f
USING_NS_CC;

class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
virtual void ccTouchesBegan(CCTouch *pTouch, CCEvent *pEvent);//----------------------监听单点触屏动作
virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent);//----------------------监听单点触屏动作
// virtual void onEnter();
// virtual void onExit();
virtual void update(float dt);
b2World * world;

cocos2d::extension::CCPhysicsSprite * Bird;

CCSize screenSize;


private:
void addBird();
void addGround();
void initWorld();

};

#endif // __HELLOWORLD_SCENE_H__





HelloWorldScene.cpp


#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();

// 'layer' is an autorelease object
HelloWorld *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 ( !CCLayer::init() )
{
return false;
}

screenSize = CCDirector::sharedDirector()->getVisibleSize();

initWorld();
addBird();
addGround();
scheduleUpdate();
this->setTouchEnabled(true);//----------------------设置触屏可用
return true;
}
void HelloWorld::initWorld(){
world = new b2World(b2Vec2(0, -15));

}
void HelloWorld::update(float dt){
world->Step(dt, 8, 3);
}

void HelloWorld::addBird(){

Bird = cocos2d::extension::CCPhysicsSprite::create("bao.png");

CCSize size = Bird->getContentSize();

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = b2Vec2(screenSize.width / 2 / RATIO, 6);
b2Body *birdBody = world->CreateBody(&bodyDef);

b2PolygonShape birdShape;
birdShape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef BirdFixtureDef;
BirdFixtureDef.shape = &birdShape;
birdBody->CreateFixture(&BirdFixtureDef);

Bird->setPTMRatio(RATIO);
Bird->setB2Body(birdBody);


CCLOG("add bird");
addChild(Bird);
}
void HelloWorld::addGround(){
cocos2d::extension::CCPhysicsSprite *ground = cocos2d::extension::CCPhysicsSprite::create("ground.png");
CCSize size = ground->getContentSize();

b2BodyDef bDef;
bDef.type = b2_staticBody;
bDef.position = b2Vec2(screenSize.width / 2 / RATIO, 1);
b2Body *groundBody = world->CreateBody(&bDef);

b2PolygonShape groundShape;
groundShape.SetAsBox(size.width / 2 / RATIO, size.height / 2 / RATIO);
b2FixtureDef groundFixtureDef;
groundFixtureDef.shape = &groundShape;
groundBody->CreateFixture(&groundFixtureDef);

ground->setB2Body(groundBody);
ground->setPTMRatio(RATIO);
addChild(ground);
CCLOG("add ground");
}
void HelloWorld::ccTouchesBegan(CCTouch *pTouch, CCEvent *pEvent){//----------------------监听触屏动作,但是不成功
Bird->getB2Body()->SetLinearVelocity(b2Vec2(0, 10));
CCLOG("touch");
}
void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent){

}
/*
void HelloWorld::onEnter(){
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
CCLayer::onEnter();
}
void HelloWorld::onExit(){
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
*/
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}





求大神们帮忙!多谢!!
...全文
897 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bianfuba 2015-09-26
  • 打赏
  • 举报
回复
建议重载的函数加上override
A_DAY_IN_MY_LIFT 2015-01-23
  • 打赏
  • 举报
回复
2.0和3.0对于触摸的代码不同。 如果是函数重载,函数名就需要写对。
香蕉裤衩 2014-05-03
  • 打赏
  • 举报
回复
兄弟,我也刚遇到,呵呵。 你看下cocos里TOUCHES的例子就知道了, 现在 需要进行注册监听了。 另外你需要重写下onEnter,onExit.

void Player::onEnter()
{
    Node::onEnter();
    
    // Register Touch Event
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    
    listener->onTouchBegan = CC_CALLBACK_2(Player::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(Player::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(Player::onTouchEnded, this);
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    
}
huminghan 2014-05-02
  • 打赏
  • 举报
回复
我最近也刚学cocos,5楼说的是 单点触摸,所以是bool型的。 但是lz好像要写多点触摸。 大概看了一下,看你在onEnter() 里面写了注册多点触摸 ,但是为什么给注释掉了? 我一般是在init里 就把 注册触摸写了 ()。
I'm Daniel Du 2014-04-27
  • 打赏
  • 举报
回复
引用 5 楼 Daiwood 的回复:
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
+1,还有触摸相关的几个虚函数都要实现,否则会有奇怪的Bug
a87489813 2014-04-25
  • 打赏
  • 举报
回复
auto listener = EventListenerTouchOneByOne::create(); 创建一个触摸监听 listener->ccTouchesBegan (重写下) listener->ccTouchesEnd (重写下) _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,对象); //将触摸监听添加到eventDispacher中去
木头呆但不傻 2014-04-24
  • 打赏
  • 举报
回复
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
allenhiman 2014-04-21
  • 打赏
  • 举报
回复
很久没用cc2d了 记得需要开启touch功能 有个什么函数 你是否忘记了呢
  • 打赏
  • 举报
回复
函数参数改一下 在试试 virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent);
汤圆77 2014-04-21
  • 打赏
  • 举报
回复
触摸开始响应函数写错了: virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); 你重写下这个函数: virtual void registerWithTouchDispatcher(); 试试
luo4188 2014-04-19
  • 打赏
  • 举报
回复
难道说与开发环境有关?只有XCODE能这样写,VS不行吗? 如果是这样的话,要怎么改呢?多谢大神们!

8,303

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧