Lame frog note 19-cocos2d-x-3.2 screen lock genie Movement
Previous review
In the previous article, he learned how to use OrbitCamera and learned about it. Although the parameters in the OrbitCamera: create Function are difficult to understand, we can explain them in an abstract way. Haha, this time I learned how to lock the running of the genie with the screen.
Introduction
First, let's look at the inheritance relationship as follows.
Follow directly inherits from the Action class and looks quite old-qualified. It is also old-qualified. Who puts people in front of each other. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + icagicagicagwoc1xls0vai6r8r9ysejuw.vcd4kphrhymxligjvcmrlcj0 = "0" cellpadding = "0">
StaticFollow* Create
(
Node*
FollowedNode,
Const Rect &
Rect=Rect::ZERO
)
In the parameter, followedNode indicates the Node to be followed. Rect indicates the boundary, whenrect
If it is Rect: ZERO, there is no boundary.
Procedure
We still find it in the HelloWorld. cpp File
Add the following code to the bool HelloWorld: init () function.
Auto grossini = Sprite: create ("grossini.png ");
AddChild (grossini, 0, 2 );
Auto s = Director: getInstance ()-> getWinSize ();
Grossini-> setPosition (Vec2 (0, s. height/2 ));
Auto move = MoveBy: create (2, Vec2 (s. width * 3, 0 ));
Auto move_back = move-> reverse ();
Auto seq = Sequence: create (move, move_back, nullptr );
Auto rep = RepeatForever: create (seq );
Grossini-> runAction (rep );
This-> runAction (Follow: create (grossini, Rect (0, 0, s. width * 3, s. height )));
After compilation and running, what did my friends find?
That's right. It's where the genie moves and where our screens are tracked. Isn't it so cool?
Is it like making a movie? Too feels that this skill is great. We can work with it to prevent our leading role from going out of the screen.
Code explanation
The following describes the meaning of the Code:
Auto grossini = Sprite: create ("grossini.png"); // Add an genie
AddChild (grossini, 0, 2); // Add to the HelloWorld Layer
Auto s = Director: getInstance ()-> getWinSize (); // get the window size
Grossini-> setPosition (Vec2 (0, s. height/2); // you can specify a position.
Auto move = MoveBy: create (2, Vec2 (s. width * 3, 0); // create a moveby action
Auto move_back = move-> reverse (); // opposite action for creating moveby
Auto seq = Sequence: create (move, move_back, nullptr); // create an action string
Auto rep = RepeatForever: create (seq); // create repeated actions
Grossini-> runAction (rep); // The genie runs this action.
This-> runAction (Follow: create (grossini, Rect (0, 0, s. width * 3, s. height); // this is the following function,
It indicates that the HelloWorld layer calls this follow. When the following object is followed, the grossini genie has a rectangle in the range and four fixed points are (0, 0, s. width * 3, s. height ). If it exceeds the specified range, it will not be tracked again.
Summary
The basic usage of Follow is recorded this time. You can try it. This Follow class is quite interesting.