Cocos2d-x Win32鍵盤類比觸摸事件

來源:互聯網
上載者:User

        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,解析度改變後會自動計算改變後對於的座標。

鍵盤對應的觸摸事件如所示:

另外,向右+向上=右上方,類似的可以讓角色進行對角的行走。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.