Home > Web Front-end > H5 Tutorial > HTML5 game framework cnGameJS development record-realizing animation principles

HTML5 game framework cnGameJS development record-realizing animation principles

黄舟
Release: 2017-03-24 16:08:51
Original
2420 people have browsed it

 

In the game, the animation effect of the game character is an essential part of the game. In this section, we take the construction of Super Mario characters as an example to explain the implementation of animation in cnGameJS.

1. Principle:

If an animation wants to implement a series of actions, we can keep the snapshot of each action and put it in a big picture Above, and then every time the frame is updated, it loops and displays between the snapshots of each action, and finally gets an animation . So we first need to prepare a picture similar to the following:

Do you see it? Place each action in a different position of the picture, and then you can achieve animation effects by changing the display position.

When cnGameJS calls the start method to start the game, it will call the incoming gameObj's initialize method for initialization and generate a game loop. Each time in the loop, the update and gameObj's up

date

are called. draw method. Therefore, we can put the initialization of the animation in the initialize of gameObj, and place the update and draw in the update and draw of gameObj respectively to realize animation playback.

Effect:

HTML5 game framework cnGameJS development record-realizing animation principles

Code:

<body>
<canvas id="gameCanvas">请使用支持canvas的浏览器查看</canvas>
</body>
<script src="http://files.cnblogs.com/Cson/cnGame_v1.0.js"></script>
<script>
var Src="http://pic002.cnblogs.com/images/2012/273330/2012021312050269.png";
/* 初始化 */
cnGame.init(&#39;gameCanvas&#39;,{width:50,height:60});
var gameObj={
    initialize:function(){
        this.marie=cnGame.SpriteSheet("marie",Src,{frameSize:[50,60],width:150,height:60,loop:true});
    },
    update:function(){
        this.marie.update();
    },
    draw:function(){
        this.marie.draw();
    }

}
cnGame.loader.start([Src],gameObj);
</script>
Copy after login

2. Implementation

As seen above, we only need to use a small amount of code to realize the playback of a frame animation. Next, we will introduce how the frame animation in cnGameJS is encapsulated. You can easily find that cnGameJS follows a specific pattern, divides the object

phases into three: initialize (initialization), update (frame update) and draw (draw)

. In this way, we can easily write codes for different functions in corresponding stages. spriteSheet frame animation is no exception, and is also written according to this pattern.  

Initialization:

The user sets some necessary information.

spriteSheet.prototype={
        /**
         *初始化
        **/
        init:function(id,src,options){
            
            /**
             *默认对象
            **/    
            var defaultObj={
                x:0,
                y:0,
                width:120,
                height:40,
                frameSize:[40,40],
                frameDuration:100,
                direction:"right",    //从左到右
                beginX:0,
                beginY:0,
                loop:false,
                bounce:false        
            };
            options=options||{};
            options=cg.core.extend(defaultObj,options);
            this.id=id;                                    //spriteSheet的id
            this.src=src;                                //图片地址
            this.x=options.x;                            //动画X位置
            this.y=options.y;                            //动画Y位置
            this.width=options.width;                    //图片的宽度
            this.height=options.height;                    //图片的高度
            this.image=cg.loader.loadedImgs[this.src]; //图片对象
            this.frameSize=options.frameSize;            //每帧尺寸
            this.frameDuration=options.frameDuration;    //每帧持续时间
            this.direction=options.direction;            //读取帧的方向(从做到右或从上到下)
            this.currentIndex=0;                        //目前帧索引
            this.beginX=options.beginX;                    //截取图片的起始位置X
            this.beginY=options.beginY;                    //截图图片的起始位置Y
            this.loop=options.loop;                        //是否循环播放
            this.bounce=options.bounce;                    //是否往返播放
            this.onFinsh=options.onFinsh;                //播放完毕后的回调函数
            this.frames=caculateFrames(options);        //帧信息集合
            this.now=new Date().getTime();                //当前时间
            this.last=new Date().getTime();            //上一帧开始时间
        },
Copy after login

There are many parameters above, all of which are presets for frame animation attributes. It should be noted that we called the private method caculateFrames to calculate the information of each frame and save it to frames to prepare for frame drawing.  

Frame update:

During the update process of each frame, we first obtain the current time as the start time of the frame, and compare it with the start time of the previous frame Subtract to get the time taken for the last frame.

If the time exceeds the previously set time per frame, the frame update can be performed

. Then determine whether to loop or play the animation back and forth, and update the corresponding frame index as appropriate. After the frame index is finally determined, the frame information can be obtained from the frames array and returned.

/**
         *更新帧
        **/    
        update:function(){
            
            this.now=new Date().getTime();
            var frames=this.frames;
            if((this.now-this.last)>this.frameDuration){//如果间隔大于帧间间隔,则update
                var currentIndex=this.currentIndex;
                var length=this.frames.length;
                this.last=this.now;
                
                if(currentIndex>=length-1){
                    if(this.loop){    //循环
                        return frames[this.currentIndex=0];    
                    }
                    else if(!this.bounce){//没有循环并且没有往返滚动,则停止在最后一帧
                        this.onFinsh&&this.onFinsh();
                        this.onFinsh=undefined;
                        return frames[currentIndex];
                    }
                }
                if((this.bounce)&&((currentIndex>=length-1&&path>0)||(currentIndex<=0&&path<0))){    //往返
                    path*=(-1);
                }
                this.currentIndex+=path;
                
            }
            return frames[this.currentIndex];
        },
Copy after login

Frame drawing:

After the frame is updated, the index of the current frame has been obtained, so the draw method can obtain the current frame from the frames that save all frame information. Frame information (including the starting position of image interception, etc.), thereby intercepting a large picture at a specified position and drawing an image of the picture area:

/**
         *在特定位置绘制该帧
        **/
        draw:function(){
            
            var currentFrame=this.getCurrentFrame();
            var width=this.frameSize[0];
            var height=this.frameSize[1];
            cg.context.drawImage(this.image,currentFrame.x,currentFrame.y,width,height,this.x,this.y,width,height);
        }
Copy after login

Finally, methods such as jumping to a specific frame are also provided.

###All source code of animation module: ###
/**
     *包含多帧图像的大图片
    **/    
    spriteSheet=function(id,src,options){
        if(!(this instanceof arguments.callee)){
            return new arguments.callee(id,src,options);
        }
        this.init(id,src,options);
    }
    spriteSheet.prototype={
        /**
         *初始化
        **/
        init:function(id,src,options){
            
            /**
             *默认对象
            **/    
            var defaultObj={
                x:0,
                y:0,
                width:120,
                height:40,
                frameSize:[40,40],
                frameDuration:100,
                direction:"right",    //从左到右
                beginX:0,
                beginY:0,
                loop:false,
                bounce:false        
            };
            options=options||{};
            options=cg.core.extend(defaultObj,options);
            this.id=id;                                    //spriteSheet的id
            this.src=src;                                //图片地址
            this.x=options.x;                            //动画X位置
            this.y=options.y;                            //动画Y位置
            this.width=options.width;                    //图片的宽度
            this.height=options.height;                    //图片的高度
            this.image=cg.loader.loadedImgs[this.src]; //图片对象
            this.frameSize=options.frameSize;            //每帧尺寸
            this.frameDuration=options.frameDuration;    //每帧持续时间
            this.direction=options.direction;            //读取帧的方向(从做到右或从上到下)
            this.currentIndex=0;                        //目前帧索引
            this.beginX=options.beginX;                    //截取图片的起始位置X
            this.beginY=options.beginY;                    //截图图片的起始位置Y
            this.loop=options.loop;                        //是否循环播放
            this.bounce=options.bounce;                    //是否往返播放
            this.onFinsh=options.onFinsh;                //播放完毕后的回调函数
            this.frames=caculateFrames(options);        //帧信息集合
            this.now=new Date().getTime();                //当前时间
            this.last=new Date().getTime();            //上一帧开始时间
        },
        /**
         *更新帧
        **/    
        update:function(){
            
            this.now=new Date().getTime();
            var frames=this.frames;
            if((this.now-this.last)>this.frameDuration){//如果间隔大于帧间间隔,则update
                var currentIndex=this.currentIndex;
                var length=this.frames.length;
                this.last=this.now;
                
                if(currentIndex>=length-1){
                    if(this.loop){    //循环
                        return frames[this.currentIndex=0];    
                    }
                    else if(!this.bounce){//没有循环并且没有往返滚动,则停止在最后一帧
                        this.onFinsh&&this.onFinsh();
                        this.onFinsh=undefined;
                        return frames[currentIndex];
                    }
                }
                if((this.bounce)&&((currentIndex>=length-1&&path>0)||(currentIndex<=0&&path<0))){    //往返
                    path*=(-1);
                }
                this.currentIndex+=path;
                
            }
            return frames[this.currentIndex];
        },
        /**
         *跳到特定帧
        **/
        index:function(index){
            this.currentIndex=index;
            return this.frames[this.currentIndex];    
        },
        /**
         *获取现时帧
        **/
        getCurrentFrame:function(){
            return this.frames[this.currentIndex];    
        },
        /**
         *在特定位置绘制该帧
        **/
        draw:function(){
            
            var currentFrame=this.getCurrentFrame();
            var width=this.frameSize[0];
            var height=this.frameSize[1];
            cg.context.drawImage(this.image,currentFrame.x,currentFrame.y,width,height,this.x,this.y,width,height);
        }
        
    }
    this.SpriteSheet=spriteSheet;
                                        
});
Copy after login

The above is the detailed content of HTML5 game framework cnGameJS development record-realizing animation principles. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template