How to use cc.follow for camera tracking in CocosCreator

How to use cc.follow for camera tracking in CocosCreator

Cocos Creator version: 2.3.4

Demo download: https://files-cdn.cnblogs.com/files/gamedaybyday/cocos2.3.4_ccfollow.7z

Let's first use the follow code that comes with cocoscreator, cc.follow.

Use of CC.Follow

1. Set the map adapter container leftNode

Let's say this is a horizontal parkour game. The map size is 1500x1500, so the height and width of leftNode are 1500x1500, and the widget is aligned to the lower left.

2. Set the map container map

The map container is also 1500x1500. The difference from leftNode is that it does not have an adapter component widget. If you use a widget, you cannot move the map. This is why an extra layer of leftNode is used to adapt to the screen.

There is a role in the map. (A normal cc.Button button)

There are red buttons around the map to identify the map edges.

Add the Follow.ts component to the map container and set the target role to Role. Role is a button cc.Button, and we treat it as a moving role here.

The code of Follow.ts is as follows:

const {ccclass, property} = cc._decorator;
 
@ccclass
export default class Follow extends cc.Component {
 
    @property(cc.Node)
    target:cc.Node = null;
 
    start () {
        var follow = cc.follow(this.target, cc.rect(0,0, 1500,1500));
        this.node.runAction(follow);
    }
}

3. Virtual joystick controls character movement

Virtual joystick reference: https://www.cnblogs.com/gamedaybyday/p/13061387.html

It doesn't matter if you don't know, as long as you can use other methods to control the character's movement and change the character's x and y.

4. Actual Demonstration Effect

You can see that when the character moves, the "camera" automatically follows the character and is limited to a range of 1500x1500, not exceeding the boundary where the red button is placed.

Source code of CC.Follow

The source code is in CC.Action.js

The above is the details of how to use cc.follow for camera following in CocosCreator. For more information about camera following in CocosCreator, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Unity3D realizes camera lens movement and limits the angle
  • Detailed explanation of how to use several timers in CocosCreator
  • CocosCreator learning modular script
  • How to use physics engine joints in CocosCreator
  • How to use JSZip compression in CocosCreator
  • CocosCreator Getting Started Tutorial: Making Your First Game with TS
  • Interpretation of CocosCreator source code: engine startup and main loop
  • CocosCreator general framework design resource management
  • How to make a List in CocosCreator
  • How to use http and WebSocket in CocosCreator
  • Analysis of CocosCreator's new resource management system

<<:  A preliminary study on composite primary key and joint primary key in SQL statements

>>:  Detailed explanation of the usage of two types of temporary tables in MySQL

Recommend

Linux uses lsof command to check file opening status

Preface We all know that in Linux, "everythi...

Why the CSS attribute value clear:right does not work in detail

Using the clear property to clear floats is a comm...

50 Super Handy Tools for Web Designers

Being a web designer is not easy. Not only do you...

Steps for Vue3 to use mitt for component communication

Table of contents 1. Installation 2. Import into ...

MySQL 8.0.2 offline installation and configuration method graphic tutorial

The offline installation method of MySQL_8.0.2 is...

Introduction to the use of http-equiv attribute in meta tag

meta is an auxiliary tag in the head area of ​​htm...

Vue Basics Listener Detailed Explanation

Table of contents What is a listener in vue Usage...

iframe adaptive size implementation code

Page domain relationship: The main page a.html bel...

Comprehensive summary of mysql functions

Table of contents 1. Commonly used string functio...

An elegant way to handle WeChat applet authorization login

Preface When the WeChat mini program project invo...

Docker Machine in-depth explanation

Differences between Docker and Docker Machine Doc...

Detailed explanation of crontab scheduled execution command under Linux

In LINUX, periodic tasks are usually handled by t...

How to Enable or Disable Linux Services Using chkconfig and systemctl Commands

This is an important (and wonderful) topic for Li...

Detailed explanation of the use of Vue.js draggable text box component

Table of contents Registering Components Adding C...

js realizes the function of clicking to switch cards

This article example shares the specific code of ...