This article describes in detail the third part of html5 game development, scroll and dialog implementation .,. To learn more about the last two articles, click:
Html5 game development-Zero-basic development RPG games-Open Source lecture (1)
Http://www.html5cn.org/article-1737-1.html
Html5 game development-Zero-basic development RPG games-open-source lecture (2)-start a hero
Http://www.html5cn.org/article-1738-1.html
In the first two articles, the development of RPG has implemented the addition of maps and the addition of game characters. In this article, the implementation of the scroll and dialogue of maps is achieved. The effect is as follows:
This development has updated the library parts to 1.3. Please click the link below to download the library parts version 1.3 or later
Http://code.google.com/p/legendforhtml5programming/downloads/list
Rolling a map
For details about the principle of map scrolling, refer
Follow the instructions to scroll the map. You only need to draw the forthcoming map (yellow in the figure) and then scroll the map, remove the part outside the screen (the green part in the figure)
First, you must add a variable to control whether the map is rolling.
- // Map scrolling
- Var mapmove = false;
Then, when the characters move, determine whether the map needs to be rolled.
- /**
- * Whether the map is rolling
- **/
- Character. prototype. checkMap = function (dir ){
- Var self = this;
- Mapmove = false;
- // If it is not a hero, the map does not need to be rolled.
- If (! Self. isHero) return;
-
- Switch (dir ){
- Case UP:
- If (self. y + charaLayer. y> STEP) break;
- If (mapLayer. y> = 0) break;
- AddMap (0,-1 );
- Mapmove = true;
- Break;
- Case LEFT:
- If (self. x + charaLayer. x> STEP) break;
- If (mapLayer. x> = 0) break;
- AddMap (-1, 0 );
- Mapmove = true;
- Break;
- Case RIGHT:
- If (self. x <480-2 * STEP) break;
- If (480-mapLayer. x> = map [0]. length * STEP) break;
- AddMap (1, 0 );
- Mapmove = true;
- Break;
- Case DOWN:
- If (self. y <288-2 * STEP) break;
- If (288-mapLayer. y> = map. length * STEP) break;
- AddMap (0, 1 );
- Mapmove = true;
- Break;
- }
- };
During the movement process, you can determine whether the map is in the rolling state. If the map is in the rolling state, it will be rolled. Otherwise, the map will be moved.
- /**
- * Start to move
- **/
- Character. prototype. onmove = function (){
- Var self = this;
- // Set the number of moves in a step.
- Var ml_cnt = 4;
- // Calculate the length of one Movement
- Var ml = STEP/ml_cnt;
- // Start moving Based on the moving direction
- Switch (self. direction ){
- Case UP:
- If (mapmove ){
- MapLayer. y + = ml;
- CharaLayer. y + = ml;
- }
- Self. y-= ml;
- Break;
- Case LEFT:
- If (mapmove ){
- MapLayer. x + = ml;
- CharaLayer. x + = ml;
- }
- Self. x-= ml;
- Break;
- Case RIGHT:
- If (mapmove ){
- MapLayer. x-= ml;
- CharaLayer. x-= ml;
- }
- Self. x + = ml;
- Break;
- Case DOWN:
- If (mapmove ){
- MapLayer. y-= ml;
- CharaLayer. y-= ml;
- }
- Self. y + = ml;
- Break;
- }
- Self. moveIndex ++;
- // When the number of moves is equal to the set number of times, start to determine whether to continue moving
- If (self. moveIndex> = ml_cnt ){
- // After a map step is moved, if the map is in the rolling state, remove unnecessary map blocks.
- If (mapmove) delMap ();
- Self. moveIndex = 0;
- // If the move key has been released or the forward direction is an obstacle, stop moving. Otherwise, continue moving.
- If (! IsKeyDown |! Self. checkRoad ()){
- Self. move = false;
- Return;
- } Else if (self. direction! = Self. direction_next ){
- Self. direction = self. direction_next;
- Self. anime. setAction (self. direction );
- }
- // Whether the map is rolling
- Self. checkMap (self. direction );
- }
- };
Finally, expand the map array and terrain to a size greater than the screen size.
- // Map image Array
- Var map = [
- [18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 55, 55, 18, 18],
- [18,18, 18,17, 17,17, 17,17, 17,17, 17,17, 55,55, 17,17, 18],
- [18, 18, 17,17, 17,17, 18,18, 17,17, 17,17, 55,55, 17,17, 18],
- [, 17,17, 18, 18, 18, 18, 18, 18, 17, 17,55, 55,17, 17,17, 18],
- [, 17, 17,18, 23, 23, 24, 18, 17,55, 55,17, 17,17, 18],
- [, 18],
- [, 17,17, 17,10, 11,12, 18,18, 55,55, 17,17, 17,17, 18],
- [, 18],
- [, 17, 17, 16, 16, 16, 21, 17, 17, 17, 17, 18],
- [, 18],
- [18, 18, 18, 18, 18, 18, 18, 18, 18, 55, 55, 18, 18, 18, 18, 18]
- ];
- // Map terrain Array
- Var mapdata = [
- [, 1],
- [, 1],
- [, 0, 1],
- [, 1],
- [, 1],
- [, 1],
- [, 0, 1],
- [, 1],
- [, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [, 1],
- [, 1]
- ];
To implement map scrolling, modify the map adding method and add the yellow map part of the above image according to the parameters.
- // Add a map
- Function addMap (cx, cy ){
- Var I, j, index, indexX, indexY;
- Var bitmapdata, bitmap;
- Var mapX = mapLayer. x/STEP;
- Var mapY = mapLayer. y/STEP;
- Var mx = cx <0? -1:0, my = cy <0? -1:0;
- If (imageArray = null ){
- // Map image data
- Bitmapdata = new LBitmapData (imglist ["map"]);
- // Split the map image to obtain the coordinate array of each small image after the split
- ImageArray = LGlobal. pideCoordinate (bitmapdata. image. width, bitmapdata. image. height, 10, 10 );
- }
- MapLayer. removeAllChild ();
- // Draw a 15x10 small image on the map layer
- For (I = my; I <9 + Math. abs (cy) & I-mapY <map. length; I ++ ){
- For (j = mx; j <15 + Math. abs (cx) & j-mapX <map [0]. length; j ++ ){
- // Obtain the image coordinates of the corresponding position from the map Array
- Index = map [I-mapY] [j-mapX];
- // The vertical coordinate of the small image
- IndexY = Math. floor (index/10 );
- // Horizontal coordinate of the small image
- IndexX = index-indexY * 10;
- // Get a small image
- Bitmapdata = new LBitmapData (imglist ["map"], indexX * 32, indexY * 32, 32 );
- Bitmap = new LBitmap (bitmapdata );
- // Set the position of the small image
- Bitmap. x = j * STEP-mapLayer. x;
- Bitmap. y = I * STEP-mapLayer. y;
- // Display the small image to the map layer
- MapLayer. addChild (bitmap );
- }
- }
- }
- // Remove unnecessary map Blocks
- Function delMap (){
- Var bitmap, I;
- For (I = 0; I
- Bitmap = mapLayer. childList [I];
- If (bitmap. x + mapLayer. x <0 | bitmap. x + mapLayer. x >=480 |
- Bitmap. y + mapLayer. y <0 | bitmap. y + mapLayer. y >=288 ){
- MapLayer. removeChild (bitmap );
- I --;
- }
- }
- }
The effect is as follows:
Character conversation
The implementation of the dialog is added when you click the square button of the control button. Therefore, when you move the cursor up, you can determine whether the square button is clicked.
- Function onup (event ){
- IsKeyDown = false;
- If (event. offsetX> = ctrlLayer. x + 280 & event. offsetX <= ctrlLayer. x + 330 ){
- If (event. offsetY> = ctrlLayer. y + 40 & event. offsetY <= ctrlLayer. y + 100 ){
- // Dialog
- AddTalk ();
- }
- }
- }
When improving the addTalk () method, first prepare the conversation content.
- Var talkScriptList = {
- "Talk1": new Array (
- {Img: "m", name: "Naruto", msg: "I am a Naruto from muye village. Who are you? "},
- {Img: "n", name: "heiyi ninja", msg: "Are you Naruto? Are you still in your body? "}
- ),
- "Talk2": new Array (
- {Img: "n", name: "", msg: "Naruto, I heard that the ninja war is about to begin. "},
- {Img: "m", name: "Naruto", msg: "true? Be sure to find a solution. "}
- )
- };
The number next to the talk in talk1 and talk2, representing the number of the character. The img of each conversation unit is the avatar of the character, the name is the name of the character, and the msg is the content of the conversation.
When adding a dialog, you can click the square button to determine whether there is a person in front of the person. If there is a person, the number of the person will be taken out, obtain the corresponding dialog content from the array above, and then display the corresponding content on the game screen. The specific implementation code is as follows:
- // Dialog content
- Var talkScript;
- Var talkScriptList = {
- "Talk1": new Array (
- {Img: "m", name: "Naruto", msg: "I am a Naruto from muye village. Who are you? "},
- {Img: "n", name: "heiyi ninja", msg: "Are you Naruto? Are you still in your body? "}
- ),
- "Talk2": new Array (
- {Img: "n", name: "", msg: "Naruto, I heard that the ninja war is about to begin. "},
- {Img: "m", name: "Naruto", msg: "true? Be sure to find a solution. "}
- )
- };
- // Dialog no.
- Var talkIndex = 0;
- // Dialog in progress
- Var talking = false;
- /**
- * Add a dialog
- **/
- Function addTalk (){
- // If the dialog content is blank, start to judge whether the dialog can be made
- If (talkScript = null ){
- Var key, tx = player. x, ty = player. y;
- Switch (player. direction ){
- Case UP:
- Ty-= STEP;
- Break;
- Case LEFT:
- Tx-= STEP;
- Break;
- Case RIGHT:
- Tx + = STEP;
- Break;
- Case DOWN:
- Ty + = STEP;
- Break;
- }
- For (key in charaLayer. childList ){
- // Determine whether there is no npc in the front. If yes, start the conversation.
- If (charaLayer. childList [key]. x = tx & charaLayer. childList [key]. y = ty ){
- If (talkScriptList ["talk" + charaLayer. childList [key]. index]) {
- TalkScript = talkScriptList ["talk" + charaLayer. childList [key]. index];
- TalkIndex = 0;
- }
- }
- }
- // If no npc exists, return
- If (talkScript = null) return;
- }
- // Clear the dialog Layer
- TalkLayer. removeAllChild ();
- // When the conversation starts and starts in order
- If (talkIndex <talkScript. length ){
- // Obtain the dialog content
- Var talkObject = talkScript [talkIndex];
- // Dialog background
- Bitmapdata = new LBitmapData (imglist ["talk"]);
- Bitmap = new LBitmap (bitmapdata );
- Bitmap. width = 330;
- Bitmap. height = 70;
- Bitmap. x = 100;
- Bitmap. y = 20;
- Bitmap. alpha = 0.7;
- TalkLayer. addChild (bitmap );
- // Dialog Avatar
- Bitmapdata = new LBitmapData (imglist [talkObject. img]);
- Bitmap = new LBitmap (bitmapdata );
- Bitmap. x = 0;
- Bitmap. y = 0;
- TalkLayer. addChild (bitmap );
- // Dialog character name
- Var name = new LTextField ();
- Name. x = 110;
- Name. y = 30;
- Name. size = "14 ";
- Name. color = "# FFFFFF ";
- Name. text = "[" + talkObject. name + "]";
- TalkLayer. addChild (name );
- // Dialog content
- Var msg = new LTextField ();
- Msg. x = 110;
- Msg. y = 55;
- Msg. color = "# FFFFFF ";
- Msg. text = talkObject. msg;
- TalkLayer. addChild (msg );
- // The dialog content is displayed literally
- Msg. wind ();
- TalkLayer. x = 20;
- TalkLayer. y = 50;
- TalkIndex ++;
- } Else {
- // The conversation ends.
- TalkScript = null;
- }
- }
Effect view
Game demo address
Http://fsanguo.comoj.com/html5/rpg3/index.html
Some modifications have been made elsewhere. For details about the modifications, see the source code. The updated source code is as follows:
Http://legend-demo.googlecode.com/files/rpg3.zip
This article