'cocos2D'에 해당되는 글 11건

  1. 2016.06.08 [Cocos2d-x] Day 1 - KeyInput & Gravity Jump

[Cocos2d-x] Day 1 - KeyInput & Gravity Jump

|


 <DAY> 2016.05.18
 < WORK LIST >
 <Class> Object_Hero.cpp
 - Image output
 - Key Input Control <RIGHT / LEFT / UP / DOWN >
 - GRAVITY JUMP


   1

  Image Outpu


키 입력 확인을 위한 이미지를 임시로 출력.

1
2
3
4
5
6
7
8
9
bool Object_Hero::init()
{
    if!CCSprite::initWithFile("TestImage/boom1.png")) return false;
 
    m_iState        = IDLE;
    this->schedule( schedule_selector( Object_Hero::update ));
    return true;
}
 

cs


   2

  Key Input


키 입력에 따라 달라질 상태를 #define, enum으로 선언한다.

1
2
3
4
5
6
#define JUMPPOWER    700
#define GRAVITY      1500
 
enum State{
    IDLE, WALK, JUMP, DROP
};
cs


키 입력에 따른 변화
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
void Object_Hero::KeyInput(float dt)
{
    CGPoint pos = this->getPosition();
    float MoveSpeed = 300.* dt;
 
    if(GetAsyncKeyState(VK_RIGHT))
    {
        pos.x += MoveSpeed;
        ST::call()->vi.vec.x = 1;
        if( m_iState != JUMP && m_iState != DROP )
            m_iState = WALK; 
    }
    if(GetAsyncKeyState(VK_LEFT))
    {
        ST::call()->vi.vec.x = -1;
        pos.x -= MoveSpeed;
        if( m_iState != JUMP && m_iState != DROP )
            m_iState = WALK; 
    }
    if(GetAsyncKeyState(VK_UP))
    {
        ST::call()->vi.vec.y = 1;
        pos.y += MoveSpeed;
    }
    if(GetAsyncKeyState(VK_DOWN))
    {
        ST::call()->vi.vec.y = -1;
        pos.y -= MoveSpeed;
    }
 
    static bool KeyPress = false;
    if(GetAsyncKeyState('C') && KeyPress == false )
    {
        if( m_iState != DROP && m_iState != JUMP )
        {
            m_iState = JUMP;
            m_fJumpPower = JUMPPOWER;
            KeyPress = true;
        }
    }
    else if(!GetAsyncKeyState('C')) KeyPress = false;
 
 
    Object_Box * Box = ST::call()->BM.CollisionCheck( pos );
 
    if( Box == NULL ) 
    {
        this->setPosition(pos);
    }
    /*else
    {
        pos.y -= Box->getPosition().y;
        if( Box->getPosition().y + 50 > pos.y )
        {
            pos.y = Box->getPosition().y;
            m_iState = IDLE;
            m_fJumpPower = 0;
        }
        this->setPosition(pos);
    }*/
}
 
cs



   3

  Gravity Jump


중력을 임시로 구현.

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
void Object_Hero::Animation(float dt)
{
    CGPoint pos = this->getPosition();
 
    switch(m_iState)
    {
        case IDLE:
            {
                break;
            }
        case JUMP:
            {
                pos.y       += m_fJumpPower * dt;
                m_fJumpPower -= GRAVITY * dt;
 
                if( m_fJumpPower <= 0 )
                {
                    m_iState = DROP;
                    m_fJumpPower = 0;
                }
                break;
            }
        case DROP:
            {
                pos.y        -= m_fJumpPower * dt;
                m_fJumpPower += GRAVITY * dt;
                 
                if( pos.y <= 0 )
                {
                    pos.y = 0;
                    m_iState = IDLE;
                }
                break;
            }
    }
 
    this->setPosition( pos );
}
cs


결과



평 가

 .난이도 : 쉬움

 별   점 : ★ 

 한 마디: 시작을 위한 한 걸음.



And
prev | 1 | 2 | next