본문 바로가기

포트폴리오

[포트폴리오]Air Hockey Together

Air Hockey Together


시연영상



GITHUB : https://github.com/tooOpen/Airhockeytogether


1. 개요

-삼성전자 Tizen 앱 콘테스트 출품작

-Tizen forum에 올라감.

-2명이 하는 파티게임입니다. 에어하키

2. 스토리

-

3. 게임 플랫폼 및 게임 기본 구성

모바일 Tizen 게임

- 2D 아케이드 게임

- 2명이 동시에 같이 플레이하는 파티게임

4. 개발 툴

- Cocos2D-x 3.5 for Tizen

 

- 클래식 에어하키 룰을 따른다.


5. Memorable Part.

 -씬에서만 터치를 받아서 처리하다 동시에 두개의 멀렛에서 동시에 터치를 처리해야하는 상황이 발생했다.

   생각해보니 Sprite와 Clonable을 상속받은 멀렛 클래스를 정의하여 그 안에 터치리스너를 넣으면 되었다.

   그렇게 했더니 역시 되더라

   Sprite를 상속받을 때 다루어야 되는 문제들

   1. 오버라이드

    getRect() , createWithTextrue(Textrue2D* aTexture), initWithTexture(Texture2D* aTextrue)

    크게 손볼것은 없어도 오버라이드 하면되고

   2. 좌표계

    ConverttoGL : cocos2d-x좌표계와 OpenGL좌표계의 차이로 정확히는 origin차이를 맞추기 위해 월드 좌표를 상대좌표로 변환

 ConvertTouchtoNodeSpaceAR : 월드 좌표인 터치 좌표를 로컬좌표로 변환하는데 AR은 Anchor Relative 앵커 포인트에 맞춰서 변환


Auto Release에 대해 대충 알게됨. 정확히 알야야겠다.


6. 코드

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
 
/*
* Mallet.cpp
*
*  Created on: Aug 15, 2015
*      Author: hangSoon
*/
 
#include "Mallet.h"
 
Mallet::Mallet() {
    // TODO Auto-generated constructor stub
 
}
 
Mallet::~Mallet() {
    // TODO Auto-generated destructor stub
}
 
Rect Mallet::getRect()
{
    auto s = getTexture()->getContentSize();
    return Rect(-s.width / 2-s.height / 2, s.width, s.height);
}
 
Mallet* Mallet::createWithTexture(Texture2D* aTexture)
{
    Mallet* pPaddle = new (std::nothrow) Mallet();
    pPaddle->initWithTexture(aTexture);
    pPaddle->autorelease();
 
    return pPaddle;
}
 
bool Mallet::initWithTexture(Texture2D* aTexture)
{
    if (Sprite::initWithTexture(aTexture))
    {
        _state = kPaddleStateUngrabbed;
    }
 
    return true;
}
 
void Mallet::onEnter()
{
    Sprite::onEnter();
 
    // Register Touch Event
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
 
    listener->onTouchBegan = CC_CALLBACK_2(Mallet::onTouchBegan, this);
    listener->onTouchMoved = CC_CALLBACK_2(Mallet::onTouchMoved, this);
    listener->onTouchEnded = CC_CALLBACK_2(Mallet::onTouchEnded, this);
 
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
 
void Mallet::onExit()
{
    //    auto director = Director::getInstance();
    //    director->getTouchDispatcher()->removeDelegate(this);
    Sprite::onExit();
}
 
bool Mallet::containsTouchLocation(Touch* touch)
{
    return getRect().containsPoint(convertTouchToNodeSpaceAR(touch));
}
 
bool Mallet::onTouchBegan(Touch* touch, Event* event)
{
    CCLOG("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
 
    if (_state != kPaddleStateUngrabbed) return false;
    if (!containsTouchLocation(touch)) return false;
 
    _state = kPaddleStateGrabbed;
    CCLOG("return true");
    return true;
}
 
void Mallet::onTouchMoved(Touch* touch, Event* event)
{
    // If it weren't for the TouchDispatcher, you would need to keep a reference
    // to the touch from touchBegan and check that the current touch is the same
    // as that one.
    // Actually, it would be even more complicated since in the Cocos dispatcher
    // you get Sets instead of 1 UITouch, so you'd need to loop through the set
    // in each touchXXX method.
 
    CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
 
    CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
 
 
    CCPoint touchPoint = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());
 
    // auto touchPoint = touch->getLocation();
    int bposx = 0;
    if (touchPoint.x < 50 || touchPoint.x>425)
    {
        if (touchPoint.x < 50)
            bposx = 50;
        else
            bposx = 425;
    }
    else
        bposx = touchPoint.x;
 
 
 
    setPosition(Vec2(bposx, getPosition().y));
}
 
Mallet* Mallet::clone() const
{
    Mallet* ret = Mallet::createWithTexture(_texture);
    ret->_state = _state;
    ret->setPosition(getPosition());
    ret->setAnchorPoint(getAnchorPoint());
    return ret;
}
 
void Mallet::onTouchEnded(Touch* touch, Event* event)
{
    CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
 
    _state = kPaddleStateUngrabbed;
}
cs





'포트폴리오' 카테고리의 다른 글

[포트폴리오] Shake Coke  (0) 2015.11.10
[포트폴리오]After Earth  (0) 2015.11.10
[포트폴리오]Thinking Snake Game  (0) 2015.11.10
[포트폴리오] Afro Run  (0) 2015.11.10
[포트폴리오] Smart Wash Helper  (0) 2015.11.10