JS-lufylegend game animation-character movement catalog
Article Directory
Preface
-
Animation is the most basic part of the game
-
Using
LAnimation
class and loop time, you can easily realize the playback of a group of animations -
Prepare a photo containing the actions of the task
Import library
lufylegend
Official website- Development suggestions:
<script src="./lufylegend-1.10.1.min.js">
- Study suggestions:
<script src="./lufylegend-1.10.1.js"></script>
manual
- Import library
- create
<div>
- Use
init
function to initialize work
Function analysis
-
init(speed, divid, width, height, completeFuc, type)
speed
: Game speed settingdivid
:canvas
Incoming thisdiv
internalwidth,height
: Game width and heightcompleteFunc
: After the game is initialized, call this function
-
LAnimation(layer,data,list)
layer
:LSprite
Objectdata
:LBitmapData
Objectlist
: A two-dimensional array of coordinates
-
Array passed
LGlobal.divideCoordinate(width,height,row,col)
width,height
:Width Heightrow,col
: Number of rows, number of columns- This function will split the width and height passed in according to the number of rows and columns to obtain a two-dimensional array
- For example, the picture is
256x256
, split codeLGlobal.divideCoordinate(256,256,4,4)
<div id="legend">Loading...</div>
<script>
init(50, "legend", 500, 350, () => {
let loader = new LLoader();
loader.addEventListener(LEvent.COMPLETE, event => {
let bitmapdata = new LBitmapData(loader.content, 0,0,64,64);
let list = LGlobal.divideCoordinate(287,287,4,4)
let layer = new LSprite();
addChild(layer);
let anime = new LAnimation(layer, bitmapdata, list);
layer.addEventListener(LEvent.ENTER_FRAME, () => {
anime.onframe();
});
});
loader.load("animation.png", "bitmapData");
});
</script>
Code explanation
-
When the character moves, it actually plays the first row of pictures in a loop one by one
-
LAnimation
Kindonframe()
: Add 1 to the column number of the picture being played, and play it in a loop event, and it becomes an animation -
To realize the loop playback of all pictures, you need to use
setAction
-
setAction(rowIndex, colIndex)
: You can change the row number and column number of the picturerowIndex
: Array row numbercolIndex
: Array column number
Mobile sports
<div id="legend">Loading...</div>
<script>
init(50, "legend", 500, 350, () => {
let loader = new LLoader();
loader.addEventListener(LEvent.COMPLETE, event => {
let bitmapdata = new LBitmapData(loader.content, 0,0,64,64);
let list = LGlobal.divideCoordinate(287,287,4,4)
let layer = new LSprite();
addChild(layer);
let anime = new LAnimation(layer, bitmapdata, list);
layer.addEventListener(LEvent.ENTER_FRAME, () => {
let action = anime.getAction();
switch(action[0]) {
case 0:
layer.y += 5;
if(layer.y >= 200) anime.setAction(2);
break;
case 1:
layer.x -= 5;
if(layer.x <= 0) anime.setAction(0);
break;
case 2:
layer.x += 5;
if(layer.x >= 200) anime.setAction(3);
break;
case 3:
layer.y -= 5;
if(layer.y <= 0) anime.setAction(1);
break;
}
anime.onframe();
});
});
loader.load("animation.png", "bitmapData");
});
</script>
Code explanation
getAction
Get the row number and column number of the currently playing animation, the return is an array- [ 1 , 2 , 3 , 4 ] [1,2,3,4] [1,2,3,4 ] represents the 4 directions of "down, left, right, and up" respectively, and then move, and change the direction of movement according to the position reached
layer.y += 5
: Control the rhythm and pace of image movementlayer.y >= 350 - 287/2
: It controls the moving space, and the biggest formula is:
Canvas height − the height of the overall picture divided into the number of lines × the height of a single picture displayed on the canvas × 2 canvas height-\frac{the height of the overall picture}{number of lines divided into} \times the height of a single picture displayed on the canvas\times 2 C A n- V A S a high degree of−Divided into a row numberThe whole body of FIG is a high degree of×Single a FIG at C A n- V A S significantly shown the high degree of×2