본문 바로가기

Cocos2d-x v3.17/기본기능

[Cocos2d-x 기본기능]Action 소개 및 By 와 To 의 구별

반응형

Action

 

Action의 기능은 보이는 그대로 액션이다.

Action은 Node의 속성을 변경하고 어떤 동작을 표현하게 한다.

그 동작대상은 실제로 Node의 속성을 변경시키고 어떤 대상이든 Node의 자식이면 변경시킬수 있다.

예를들면 어떤 스프라이트의 위치이동을 할때이다.(Sprite는 Node의 자식 클래스). 

 

MoveTo와 MoveBy두가지 방식이 있다. 


C++ 

// Move sprite to position 50,10 in 2 seconds.

auto moveTo = MoveTo::create(2, Vec2(5010));

mySprite1->runAction(moveTo); 

// Move sprite 20 points to right in 2 seconds

auto moveBy = MoveBy::create(2, Vec2(20,0));

mySprite2->runAction(moveBy); 

 

By 와 To의 구별 

 

보시면 모든 Action은 By와 To 두가지 메서드가 있다.

두가지 방법은 서로 다른 상황에서 쓰인다.

By는 상대적으로 노드의 현재위치 즉 현재위치에서 주어진 값만큼 이동하는것이고 만약 (5,5)위치에서 (10,10)으로 MoveBy메서드를 사용하면 (5,5)만큼 이동하여 (10,10)에 도착하는것이다.

To는 절대위치다.

현재 위치가 어디있는지 고려하지 않고 Node의 상대적 위치로 이동하고싶으면 By를 사용하면 된다.

절대좌표로 사용할것이면 To를 사용하면 된다.예를 한번 보자.


C++ 

auto mySprite = Sprite::create("mysprite.png");

mySprite->setPosition(Vec2(200256)); 

// MoveBy - lets move the sprite by 500 on the x axis over 2 seconds

// MoveBy is relative - since x = 200 + 200 move = x is now 400 after the move

auto moveBy = MoveBy::create(2, Vec2(500, mySprite->getPositionY())); 

// MoveTo - lets move the new sprite to 300 x 256 over 2 seconds

// MoveTo is absolute - The sprite gets moved to 300 x 256 regardless of

// where it is located now.

auto moveTo = MoveTo::create(2, Vec2(300, mySprite->getPositionY())); 

// Delay - create a small delay

auto delay = DelayTime::create(1); 

auto seq = Sequence::create(moveBy, delay, moveTo, nullptr); 

mySprite->runAction(seq); 

예를 보면 x축으로 500인 위치에 도착했다가 300인 위치로 돌아온다. 

출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/actions/


반응형