SlideShare a Scribd company logo
1 of 26
CH6. 게임 오브젝트 움직여보기
이준환
6. 게임 오브젝트 움직여보기

• 배경화면 로딩

• 사용자의 입력

• 오브젝트 배치

• 오브젝트 이동
6.1 터치 인터페이스 구현
//GameLayer.cpp
6.1.1 화면 터치
//GameLayer.cpp

• CCTouch = 하나의 입력에 대한 정보를 가진 클래스
• pTouches = 입력정보를 저장하는 컨테이너
6.1.1 화면 터치
//GameLayer.cpp

• pTouches컨테이너에 가장 첫번째 오브젝트를 가져옴
6.1.1 화면 터치
//GameLayer.cpp

• getLocationInView로 좌표값을 얻어와서 CCPoint 구조체에 저장
• 화면상의 (0, 0)은 좌측 상단
• Coco2d-x상의 (0, 0)은 좌측 하단
• LogCat으로 좌표log 확인 가능
6.2 게임 오브젝트 이동
6.2.1 게임 오브젝트 좌표 찾기
//GameLayer.cpp

• 화면상의 좌표(좌측상단)를 보드상의 좌표로 변경
6.2.1 게임 오브젝트 좌표 찾기
//GameLayer.cpp

• 화면상의 좌표를 보드상의 좌표로 변경
6.2.1 게임 오브젝트 좌표 찾기

• GameObject가 저장된 m_pBoard배열에서 boardX, boardY값을 가져옴
• isVisible() = 오브젝트의 가시성을 결정(CCSprite 객체 위에서)
6.2.2 이동 판별 및 교환

• CCSprite를 상속받은 GameObject 클래스를
사용하도록 GameObject 클래스 선언
6.2.2 이동 판별 및 교환

• m_pBoard배열의 타입을 CCSprite*타입에서
GameObject*타입으로 변경
• 사용자의 터치시작을 판단
• 최초로 터치한 보드좌표(X,Y) 저장
6.2.2 이동 판별 및 교환

• m_bTouchStarted 변수가 false일 경우 이동을 인식할 준비
• 화면상의 좌표를 저장하고 추가적인 터치가 작동하지 않도록
m_bTouchStarted=true로 변경
6.2.2 이동 판별 및 교환

• m_bTouchStarted 변수는 false로 들어오므로 터치를 받고 터치가 끝날때에

도 m_bTouchStarted 변수는 false로 바꾸어주어서 터치를 계속 받도록 함
6.2.2 이동 판별 및 교환(드래그)

• getLocationInView()로 터치된 화면상의 좌표를 가져온 후에 이동중인 곳의

보드상의 좌표값을 boardX, boardY에 저장
6.2.2 이동 판별 및 교환(드래그)

• ccTouchesBegan에서 저장해 두었던 보드상의 좌표값이 바뀌었다면 오브젝트
가 이동했다는 뜻이므로 SwapObjetcs로 오브젝트의 위치를 바꾸어 줌

• 오브젝트를 바꿀 때 인접한 오브젝트인지 확인하고 바꾸어줌
• m_bTouchStarted를 false로 해주어서 다음 입력을 받음
6.2.2 이동 판별 및 교환(드래그)

• 오차로 인한 Swap을 방지하기 위해 인접되었는지 확인함
• 0=같은 위치
• 1=인접한 오브젝트(이때만 Swap가능)
• 2=대가선 방향 위치
6.2.2 이동 판별 및 교환(드래그)

• 임시 변수에 첫번째 오브젝트를 저장 하고 교환함
• m_pBoard 배열에 저장된 오브젝트를 교환함
• 화면상에서는 보드상의 좌표를 화면상의 좌표로 변환한 후에 setPosition으로 바
꿈
6.3 액션(Action)의 추가
6.3.1 액션이란?
• 한 지점(각도)에서 특정 지점(각도)까지 지정된 시간 동안 이동하는 액션
• 특수한효과로 깜빡거리기, 페이드인/아웃
• 연속적인 액션사용을 위해 시퀀스개념을 도입
• 시퀀스를 이용하면 이동하면서 동시에 회전도 같이 하는 액션이 가능
• 게임오브젝트를 그래그해서 교환할 때 특정 지점에서 특정 지점까지 지정한 시
간동안 이동하는 액션을 추가함
6.3.2 이동 액션

• 단순 setPosition코드를 삭제
• SetTargetBoardX 함수로 이동할 보
드상의 좌표를 설정
• ProcessSliding 함수는 오브젝트를
움직이게 하는 함수
6.3.2 이동 액션

• GameObject.h에 TargetBoardX(Y)의

Getter/Setter 선언
• ProcessSliding 함수 선언
6.3.2 이동 액션

• 이동하기 전의 좌표를 저장해 놓음
• m_prevBoardY에서 MAX_ROW_COUNT=10에서 Y값을 빼주는 것은 터치 함수
에서 좌표는 좌측 상단이 (0, 0)이고 coco2d-x는 좌측 하단이 (0, 0)이므로
6.3.2 이동 액션

• 이전 위치를 저장한 후에 targetPosition에 목표지점의 좌표를 계산해서 저장

• CCMoveBy 액션 클래스를 사용해서 현재 위치에서 (x, y)의 값만큼 이동하는 액
션(0.1초간격)
• runAction(pMoveBy);
6.3.3.2 콜백 함수
6.3.3.2 콜백 함수

• CCCallFunc 클래스의 create함수로 콜백 액션을 생성
• Thist 객체의 SlidingCOmpleteHandler를 호출하겠다고 지정한 것

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Cocos2d x-6장 이준환

  • 1. CH6. 게임 오브젝트 움직여보기 이준환
  • 2. 6. 게임 오브젝트 움직여보기 • 배경화면 로딩 • 사용자의 입력 • 오브젝트 배치 • 오브젝트 이동
  • 3. 6.1 터치 인터페이스 구현 //GameLayer.cpp
  • 4. 6.1.1 화면 터치 //GameLayer.cpp • CCTouch = 하나의 입력에 대한 정보를 가진 클래스 • pTouches = 입력정보를 저장하는 컨테이너
  • 5. 6.1.1 화면 터치 //GameLayer.cpp • pTouches컨테이너에 가장 첫번째 오브젝트를 가져옴
  • 6. 6.1.1 화면 터치 //GameLayer.cpp • getLocationInView로 좌표값을 얻어와서 CCPoint 구조체에 저장 • 화면상의 (0, 0)은 좌측 상단 • Coco2d-x상의 (0, 0)은 좌측 하단 • LogCat으로 좌표log 확인 가능
  • 8. 6.2.1 게임 오브젝트 좌표 찾기 //GameLayer.cpp • 화면상의 좌표(좌측상단)를 보드상의 좌표로 변경
  • 9. 6.2.1 게임 오브젝트 좌표 찾기 //GameLayer.cpp • 화면상의 좌표를 보드상의 좌표로 변경
  • 10. 6.2.1 게임 오브젝트 좌표 찾기 • GameObject가 저장된 m_pBoard배열에서 boardX, boardY값을 가져옴 • isVisible() = 오브젝트의 가시성을 결정(CCSprite 객체 위에서)
  • 11. 6.2.2 이동 판별 및 교환 • CCSprite를 상속받은 GameObject 클래스를 사용하도록 GameObject 클래스 선언
  • 12. 6.2.2 이동 판별 및 교환 • m_pBoard배열의 타입을 CCSprite*타입에서 GameObject*타입으로 변경 • 사용자의 터치시작을 판단 • 최초로 터치한 보드좌표(X,Y) 저장
  • 13. 6.2.2 이동 판별 및 교환 • m_bTouchStarted 변수가 false일 경우 이동을 인식할 준비 • 화면상의 좌표를 저장하고 추가적인 터치가 작동하지 않도록 m_bTouchStarted=true로 변경
  • 14. 6.2.2 이동 판별 및 교환 • m_bTouchStarted 변수는 false로 들어오므로 터치를 받고 터치가 끝날때에 도 m_bTouchStarted 변수는 false로 바꾸어주어서 터치를 계속 받도록 함
  • 15. 6.2.2 이동 판별 및 교환(드래그) • getLocationInView()로 터치된 화면상의 좌표를 가져온 후에 이동중인 곳의 보드상의 좌표값을 boardX, boardY에 저장
  • 16. 6.2.2 이동 판별 및 교환(드래그) • ccTouchesBegan에서 저장해 두었던 보드상의 좌표값이 바뀌었다면 오브젝트 가 이동했다는 뜻이므로 SwapObjetcs로 오브젝트의 위치를 바꾸어 줌 • 오브젝트를 바꿀 때 인접한 오브젝트인지 확인하고 바꾸어줌 • m_bTouchStarted를 false로 해주어서 다음 입력을 받음
  • 17. 6.2.2 이동 판별 및 교환(드래그) • 오차로 인한 Swap을 방지하기 위해 인접되었는지 확인함 • 0=같은 위치 • 1=인접한 오브젝트(이때만 Swap가능) • 2=대가선 방향 위치
  • 18. 6.2.2 이동 판별 및 교환(드래그) • 임시 변수에 첫번째 오브젝트를 저장 하고 교환함 • m_pBoard 배열에 저장된 오브젝트를 교환함 • 화면상에서는 보드상의 좌표를 화면상의 좌표로 변환한 후에 setPosition으로 바 꿈
  • 20. 6.3.1 액션이란? • 한 지점(각도)에서 특정 지점(각도)까지 지정된 시간 동안 이동하는 액션 • 특수한효과로 깜빡거리기, 페이드인/아웃 • 연속적인 액션사용을 위해 시퀀스개념을 도입 • 시퀀스를 이용하면 이동하면서 동시에 회전도 같이 하는 액션이 가능 • 게임오브젝트를 그래그해서 교환할 때 특정 지점에서 특정 지점까지 지정한 시 간동안 이동하는 액션을 추가함
  • 21. 6.3.2 이동 액션 • 단순 setPosition코드를 삭제 • SetTargetBoardX 함수로 이동할 보 드상의 좌표를 설정 • ProcessSliding 함수는 오브젝트를 움직이게 하는 함수
  • 22. 6.3.2 이동 액션 • GameObject.h에 TargetBoardX(Y)의 Getter/Setter 선언 • ProcessSliding 함수 선언
  • 23. 6.3.2 이동 액션 • 이동하기 전의 좌표를 저장해 놓음 • m_prevBoardY에서 MAX_ROW_COUNT=10에서 Y값을 빼주는 것은 터치 함수 에서 좌표는 좌측 상단이 (0, 0)이고 coco2d-x는 좌측 하단이 (0, 0)이므로
  • 24. 6.3.2 이동 액션 • 이전 위치를 저장한 후에 targetPosition에 목표지점의 좌표를 계산해서 저장 • CCMoveBy 액션 클래스를 사용해서 현재 위치에서 (x, y)의 값만큼 이동하는 액 션(0.1초간격) • runAction(pMoveBy);
  • 26. 6.3.3.2 콜백 함수 • CCCallFunc 클래스의 create함수로 콜백 액션을 생성 • Thist 객체의 SlidingCOmpleteHandler를 호출하겠다고 지정한 것