HTML5 + lufylegend implements the scroll in the game, html5lufylegend
Lufylegend is an open-source HTML5 engine that enables HTML5 development using the syntax similar to ActionScript3.0, including LSprite, LBitmapData, LBitmap, LLoader, LURLLoader, and LTextField, LEvent and other classes familiar to AS developers. It supports Google Chrome, Firefox, Opera, IE9, IOS, Android, and other popular environments. With lufylegend, you can easily use object-oriented programming and use Box2dWeb to create a physical game. In addition, it also has built-in LTweenLite easing and other very useful functions. Now let's start using it, it allows you to enter the HTML5 world faster!
What is a scroll?
Those who have played RPG or horizontal Combat games should know that when the figure goes to the center of the screen, the map will be moved because the map is too large, and the figure will be relatively static. This is the legendary scroll. For example, the scroll in my game "Three Kingdoms:
With the above introduction, you should understand what a scroll is. To put it bluntly, the camera follows the main character. Next, we will use the lufylegend. js game engine to achieve this effect.
Principles
In fact, the key to achieving this effect is how to make the characters static, when to move the map, and how to move the map. Before exploring these two problems, we should first create a well-structured stage layer (and an LSprite object) for future operations. The stage structure is as follows:
+-Stage Layer
|
+-Map layer
|
+-Character Layer
It can be seen that the stage layer is the parent element of the map layer and the character layer, and the character layer is above the map layer. After all, the character is standing on the map. We know that the coordinates of sub-objects are relative to the parent object, so moving the parent object will follow. This must be understood first.
How can we hold people static? When will the map be moved? How can I move a map? You may think, first use if (xxx ){...} to determine whether the coordinates of a person reach the center of the screen. If yes, move the map object. If not, move the person object. This is troublesome. In fact, there are simpler methods:
Scroll/No scroll, our characters are moving, but if the character reaches the center of the screen, it will start to scroll, our stage layer moves in the opposite direction and the same size as the velocity of the character, so the displacement of the character against the canvas is offset, and it looks static, the map moves in the opposite direction with the parent class. This is similar to a costume movie, where two people talk while riding a horse. If people and horses are moving forward and cameras are shooting at the same speed, the picture is that the characters are not moving, and the scenery behind the characters is moving.
Next, let's look at the implementation code.
Implementation Code
The following code contains detailed Annotations:
Copy XML/HTML Code to clipboard
- LInit (30, 'mydemo', 700,480, main );
- // The direction of movement. null indicates no movement.
- Var direction = null;
- // Bird, stage layer, background object
- Var bird, stageLayer, bg;
- // The length of each movement
- Var step = 5;
- Function main (){
- // Resource list
- Var loadList = [
- {Name: 'bird ', path:'./bird.png '},
- {Name: 'bg ', path:'./bg.jpg '}
- ];
- // Load Resources
- LLoadManage. load (loadList, null, demoInit );
- }
- Function demoInit (result ){
- // Initialize the stage Layer
- StageLayer = new LSprite ();
- AddChild (stageLayer );
- // Add background
- Bg = new LBitmap (new LBitmapData (result ['bg ']);
- Bg. y =-100;
- StageLayer. addChild (bg );
- // Add a bird
- Bird = new LBitmap (new LBitmapData (result ['bird']);
- Bird. x = 100;
- Bird. y = 150;
- StageLayer. addChild (bird );
- // Add a mouse-down event
- StageLayer. addEventListener (LMouseEvent. MOUSE_DOWN, onDown );
- // Add a mouse bullet event
- StageLayer. addEventListener (LMouseEvent. MOUSE_UP, onUp );
- // Add a timeline event
- StageLayer. addEventListener (LEvent. ENTER_FRAME, onFrame );
- }
- Function onDown (e ){
- /** Set the direction of movement based on the click position */
- If (e. offsetX> LGlobal. width/2 ){
- Direction = 'right ';
- } Else {
- Direction = 'left ';
- }
- }
- Function onUp (){
- // If the direction is set to no direction, it indicates that the data is not moved.
- Direction = null;
- }
- Function onFrame (){
- Var _ step, minX, maxX;
- /** Mobile bird */
- If (direction = 'right '){
- _ Step = step;
- } Else if (direction = 'left '){
- _ Step =-step;
- } Else {
- Return;
- }
- Bird. x + = _ step;
- /** Control the bird's moving range */
- MinX = 0,
- MaxX = bg. getWidth ()-bird. getWidth ();
- If (bird. x <minX ){
- Bird. x = minX;
- } Else if (bird. x> maxX ){
- Bird. x = maxX;
- }
- /** Mobile stage */
- StageLayer. x = LGlobal. width/2-bird. x;
- /** Control the moving range of the stage */
- MinX = LGlobal. width-stageLayer. getWidth (),
- MaxX = 0;
- If (stageLayer. x <minX ){
- StageLayer. x = minX;
- } Else if (stageLayer. x> maxX ){
- StageLayer. x = maxX;
- }
- }
Running result:
You can view the Online Demo here. Click the left half of the screen to control the bird moving to the left, click the right half of the screen, control the bird moving to the right. When the bird reaches the center of the screen, the scroll starts.