积极答复者
cocos2d-x win10项目在win10m 中无法触发触屏事件

问题
-
void OpenGLESPage::CreateInput() { // Register our SwapChainPanel to get independent input pointer events auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^) { // The CoreIndependentInputSource will raise pointer events for the specified device types on whichever thread it's created on. mCoreInput = swapChainPanel->CreateCoreIndependentInputSource( Windows::UI::Core::CoreInputDeviceTypes::Mouse | Windows::UI::Core::CoreInputDeviceTypes::Touch | Windows::UI::Core::CoreInputDeviceTypes::Pen ); // Register for pointer events, which will be raised on the background thread. mCoreInput->PointerPressed += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerPressed); mCoreInput->PointerMoved += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerMoved); mCoreInput->PointerReleased += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerReleased); mCoreInput->PointerWheelChanged += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerWheelChanged); if (GLViewImpl::sharedOpenGLView() && !GLViewImpl::sharedOpenGLView()->isCursorVisible()) { mCoreInput->PointerCursor = nullptr; } // Begin processing input messages as they're delivered. mCoreInput->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit); }); // Run task on a dedicated high priority background thread. mInputLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced); }
以上是cocos2d-x win10项目中OpenGLESPage.xaml.cpp注册触屏事件的代码,调试了下无法触发 PointerPressed 函数;
在子线程代码块中查看了下,Windows::UI::Xaml::Window::Current是NULL;而在子线程代码块外查看Windows::UI::Xaml::Window::Current->CoreWindow是有数据的,初步怀疑跟这个有关,但是当我把线程池异步执行取消,将注册事件的代码放在外面,在执行swapChainPanel->CreateCoreIndependentInputSource时,报了 "从不同的线程访问"错误....swapChainPanel是xaml中创建的控件;
-------------------------------------------------
事实上触屏问题本身,我通过在非子线程中调用
Windows::UI::Xaml::Window::Current->CoreWindow->PointerPressed += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerPressed);
Windows::UI::Xaml::Window::Current->CoreWindow->PointerMoved += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerMoved);
Windows::UI::Xaml::Window::Current->CoreWindow->PointerReleased += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerReleased);
Windows::UI::Xaml::Window::Current->CoreWindow->PointerWheelChanged += ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerWheelChanged);已经处理了,但是在触发以后,cocos2d-x会将每次的事件参数PointerEventArgs,保存到一个列表(此时是在系统触发事件的线程)
随后在cocos2d-x的游戏线程中,会处理所有事件,此时会取PointerEventArgs的CurrentPoint(此时是cocos2d-x游戏线程);
然后就报不支持的操作错误,貌似PointerEventArgs的CurrentPoint属性不能夸线程操作;
----------------------------------------------------------
总结有两个问题:
1,proj.win10项目OpenGLESPage.xaml.cpp 中注册事件不触发,虽然我改用Windows::UI::Xaml::Window::Current->CoreWindow->PointerPressed触发了,但是如果有更好的办法能让cocos2d-x原本的void OpenGLESPage::CreateInput()变得可用,那也许更没隐患
2,cocos库项目cocos2d/cocos/platform/winrt/CCGLViewImpl-winrt.cpp中获取PointerEventArgs的CurrentPoint属性报错,我暂时没解决这问题
我是cocos2d-x 3.10- 已编辑 李冲-LC 2016年5月13日 2:00