Reputation: 12719
I am having one problem while working on touches on the screen. I have two modules attached on the screen, on the Left there is a Joystick which has its own touches, and on the right side I have set of Buttons which have its own actions or, touches, both doesn't work together. I have tried to setup touches instead of callbacks on buttons, and tried replacing the buttons with sprites without any luck.
See my requirement below
Any help or, suggestion is highly appreciated.
Thanks.
See my code below what I have tried
I am using SneakyJoystick
for the joystick like below
void Game::prepareJoystick(){
Size winSize = Director::getInstance()->getWinSize();
Rect joystickBaseDimensions;
joystickBaseDimensions = Rect(0, 0, 160.0f, 160.0f);
Point joystickBasePosition;
joystickBasePosition = Vec2(winSize.width*0.08, winSize.height*0.15);
SneakyJoystickSkinnedBase *joystickBase = new SneakyJoystickSkinnedBase();
joystickBase->init();
joystickBase->setPosition(joystickBasePosition);
joystickBase->setBackgroundSprite(CCSprite::create("iphone-joystick.png"));
joystickBase->setThumbSprite(CCSprite::create("iphone-joystick_cursor.png"));
SneakyJoystick *aJoystick = new SneakyJoystick();
aJoystick->initWithRect(joystickBaseDimensions);
aJoystick->autorelease();
joystickBase->setJoystick(aJoystick);
joystickBase->setPosition(joystickBasePosition);
leftJoystick = joystickBase->getJoystick();
leftJoystick->retain();
this->addChild(joystickBase);
joystickBase->setScale(GameConfig::controlScale());
}
And for controls on the right hand side I am using cocos2d::ui::Buttons with their callbacks like below
Button *button=Button::create(imageName);
button->setTouchEnabled(true);
button->setPressedActionEnabled(true);
button->addClickEventListener(callback);
button->setPosition(position);
Both are added on the same scene like I shown in the image graphics. Now both doesn't work together. One blocks another, I tried to add touches delegates and try see my code below
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(Game::onTouchesBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
and handling like below
void GameScene::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event){
for(int i=0;i<touches.size();i++){
Vec2 touchLocation=touches.at(i)->getLocation();
if(btnPunch->getBoundingBox().containsPoint(touchLocation)){
printf("\nshould punch");
punchCallbackAction(this);
}
}
}
None of above method works for me.
Update 2
Just tested the code below returns always 1
void GameScene::onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event){
printf("\nTouches = %d",(int)touches.size());
}
Update 3
Multitouch
is enabled for cocos2dx see below
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
convertAttrs();
CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
pixelFormat: (NSString*)_pixelFormat
depthFormat: _depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
[eaglview setMultipleTouchEnabled:YES];
_screenSize.width = _designResolutionSize.width = [eaglview getWidth];
_screenSize.height = _designResolutionSize.height = [eaglview getHeight];
// _scaleX = _scaleY = [eaglview contentScaleFactor];
_eaglview = eaglview;
return true;
}
Update 3 GLView initialization
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("MyGame");
director->setOpenGLView(glview);
}
There is no Multitouch property
for above initialization, do I need to do it in different way ?
Upvotes: 0
Views: 136
Reputation: 456
If testing on iOS, The multipleTouchEnabled
property needs to be set to YES
before the view can accept multitouch.
In your AppController.mm
, look for function didFinishLaunchingWithOptions
and add following line
[eaglView setMultipleTouchEnabled: true];
Upvotes: 2