您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x Win32键盘模拟触摸事件

2013-06-05 18:44 453 查看
​Cocos2d-x支持在Win32下鼠标的单击进行模拟响应触摸事件,也即只支持模拟单点触摸。在之前的文章《如何***一个横版格斗过关游戏 Cocos2d-x 2.0.4》中带有方向键和攻击键,鼠标的单点触摸已经不能满足在Win32下进行测试的要求,在这里进行扩展让键盘同时模拟响应触摸事件。

Cocos2d-x版本:2.1.3

修改proj.win32文件夹的main.cpp,改后文件内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

#include "main.h"

#include "AppDelegate.h"

#include "CCEGLView.h"

USING_NS_CC;

LRESULT myWndProcHook(UINT message, WPARAM wParam, LPARAM lParam, BOOL* pProcessed);

// uncomment below line, open debug console

// #define USE_WIN32_CONSOLE

int APIENTRY _tWinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

int nCmdShow)

{

UNREFERENCED_PARAMETER(hPrevInstance);

UNREFERENCED_PARAMETER(lpCmdLine);

#ifdef USE_WIN32_CONSOLE

AllocConsole();

freopen("CONIN$", "r", stdin);

freopen("CONOUT$", "w", stdout);

freopen("CONOUT$", "w", stderr);

#endif

// create the application instance

AppDelegate app;

CCEGLView* eglView = CCEGLView::sharedOpenGLView();

eglView->setFrameSize(480, 320);

//eglView->setFrameSize(960, 640);

//eglView->setFrameSize(2048, 1536);

//eglView->setFrameZoomFactor(0.4f);

eglView->setWndProc(myWndProcHook); //增加这一句

int ret = CCApplication::sharedApplication()->run();

#ifdef USE_WIN32_CONSOLE

FreeConsole();

#endif

return ret;

}

LRESULT myWndProcHook(UINT message, WPARAM wParam, LPARAM lParam, BOOL* pProcessed)

{

switch (message)

{

case WM_KEYDOWN:

case WM_KEYUP:

{

//以FrameSize == DesignResolutionSize时的坐标设定

CCPoint pt = CCPointZero;

switch (wParam)

{

case 'A': //向左

{

pt = ccp(25, 255);

}

break;

case 'D': //向右

{

pt = ccp(95, 256);

}

break;

case 'W': //向上

{

pt = ccp(63, 214);

}

break;

case 'S': //向下

{

pt = ccp(62, 290);

}

break;

case 'J': //攻击

{

pt = ccp(367, 277);

}

break;

case 'K': //跳跃

{

pt = ccp(445, 247);

}

break;

default:

return 0;

}

if (GetKeyState('D') & 0x8000)

{

if (GetKeyState('W') & 0x8000) //右上角

{

pt = ccp(91, 227);

}

else if (GetKeyState('S') & 0x8000) //右下角

{

pt = ccp(91, 284);

}

}

else if (GetKeyState('A') & 0x8000)

{

if (GetKeyState('W') & 0x8000) //左上角

{

pt = ccp(36, 227);

}

else if (GetKeyState('S') & 0x8000) //左下角

{

pt = ccp(36, 284);

}

}

CCEGLView* eglView = CCEGLView::sharedOpenGLView();

CCSize originalDesignResolutionSize = CCSizeMake(480, 320); //原始的设计分辨率大小

ResolutionPolicy eResolutionPolicy = kResolutionFixedWidth; //分辨率策略

CCSize obDesignResolutionSize = eglView->getDesignResolutionSize();

int offsetWidth = obDesignResolutionSize.width - originalDesignResolutionSize.width;

int offsetheight = obDesignResolutionSize.height - originalDesignResolutionSize.height;

CCSize obScreenSize = eglView->getFrameSize();

int offsetWidth2 = obScreenSize.width - originalDesignResolutionSize.width;

int offsetheight2 = obScreenSize.height - originalDesignResolutionSize.height;

switch (eResolutionPolicy)

{

case kResolutionExactFit:

{

//...

}

break;

case kResolutionNoBorder:

{

//...

}

break;

case kResolutionShowAll:

{

pt.x += offsetWidth2 / 2;

pt.y += offsetheight2 / 2;

}

break;

case kResolutionFixedHeight:

{

//...

}

break;

case kResolutionFixedWidth:

{

pt.y += offsetheight;

}

break;

}



pt.x *= eglView->getScaleX();

pt.y *= eglView->getScaleY();

int id = wParam;

if (message == WM_KEYDOWN)

{

eglView->handleTouchesBegin(1, &id, &pt.x, &pt.y);

}

else

{

eglView->handleTouchesEnd(1, &id, &pt.x, &pt.y);

}

*pProcessed = TRUE;

}

break;

}

return 0;

}
从上面代码可以看出,并不直接调用逻辑函数,而是通过调用触摸事件。坐标是以窗口为设计分辨率时来计算的,例如这里的480x320,分辨率改变后会自动计算改变后对于的坐标。



键盘对应的触摸事件如下图所示:



另外,向右+向上=右上角,类似的可以让角色进行对角的行走。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: