>  기사  >  웹 프론트엔드  >  HTML5 게임 프레임워크 cnGameJS 개발 기록을 실현하는 애니메이션 원리

HTML5 게임 프레임워크 cnGameJS 개발 기록을 실현하는 애니메이션 원리

黄舟
黄舟원래의
2017-03-24 16:08:512322검색

게임에서 게임 캐릭터의 애니메이션 효과는 게임의 필수적인 부분입니다. 이 섹션에서는 슈퍼마리오 캐릭터 구성을 예로 들어 cnGameJS의 애니메이션 구현을 설명합니다.

1. 원리:

애니메이션이 일련의 액션을 구현하려는 경우 각 액션의 스냅샷을 보관하고 큰 이미지에 넣을 수 있습니다. 그림 위의 프레임이 업데이트될 때마다 각 동작의 스냅샷이 순환되어 표시되고, 최종적으로 애니메이션이 생성됩니다. 그래서 먼저 다음과 같은 사진을 준비해야 합니다.

보이시나요? 각 동작을 그림의 다른 위치에 배치한 다음 표시 위치를 변경하여 애니메이션 효과를 얻을 수 있습니다.

cnGameJS가 게임을 시작하기 위해 start 메소드를 호출하면 들어오는 gameObj의 초기화 메소드를 호출하여 게임 루프를 초기화하고 생성합니다. 루프에서 update가 발생합니다. 및 그리기 방법. 따라서 애니메이션 초기화를 gameObj의 초기화에 넣고 업데이트와 그리기를 각각 gameObj의 업데이트와 그리기에 배치하여 애니메이션 재생을 실현할 수 있습니다.

효과:

HTML5 게임 프레임워크 cnGameJS 개발 기록을 실현하는 애니메이션 원리

코드:

<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>

2. 구현

위에서 본 것처럼 프레임 애니메이션 재생을 구현하려면 약간의 코드만 사용하면 됩니다. 다음으로 cnGameJS의 프레임 애니메이션이 캡슐화되는 방법을 소개하겠습니다.

cnGameJS가 특정 패턴을 따르는 것을 쉽게 알 수 있습니다. 객체 단계를 초기화(초기화), 업데이트(프레임 업데이트) 및 그리기(그리기)의 세 단계로 나눕니다. 이러한 방식으로 해당 단계에서 다양한 기능에 대한 코드를 쉽게 작성할 수 있습니다. spriteSheet 프레임 애니메이션도 예외는 아니며 이 패턴에 따라 작성됩니다.

 초기화: 사용자가 필요한 정보를 설정합니다.

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();            //上一帧开始时间
        },

위에는 프레임 애니메이션 속성에 대한 사전 설정인 많은 매개변수가 있습니다. 각 프레임의 정보를 계산하고 프레임 그리기를 준비하기 위해 이를 프레임에 저장하기 위해 개인 메소드 caculateFrames를 호출했다는 점에 유의해야 합니다.

프레임 업데이트:

각 프레임의 업데이트 프로세스 중에 먼저 현재 시간을 프레임의 시작 시간으로 구하고 시작 시간과 비교합니다. 이전 프레임에서 빼면 마지막 프레임에 걸린 시간을 얻습니다. 소요 시간이 이전에 설정한 프레임당 시간을 초과하는 경우 프레임 업데이트를 수행할 수 있습니다. 그런 다음 애니메이션을 앞뒤로 반복할지 또는 재생할지 결정하고 해당 프레임 인덱스를 적절하게 업데이트합니다. 프레임 인덱스가 최종적으로 결정된 후 프레임 배열에서 프레임 정보를 가져와 반환할 수 있습니다.

/**
         *更新帧
        **/    
        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];
        },

프레임 그리기:

프레임이 업데이트된 후 현재 프레임의 인덱스를 얻었으므로 그리기 메서드는 프레임에서 현재 프레임을 가져올 수 있습니다. 모든 프레임 정보를 저장하는 프레임 정보(이미지 가로채기 시작 위치 등 포함)를 통해 지정된 위치에서 큰 그림을 가로채서 그림 영역의 이미지를 그리는 방법:

/**
         *在特定位置绘制该帧
        **/
        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);
        }

마지막으로 다음과 같은 방법이 있습니다. 특정 프레임으로 점프하는 기능도 제공됩니다.

애니메이션 모듈 전체 소스 코드:

/**
     *包含多帧图像的大图片
    **/    
    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;
                                        
});

위 내용은 HTML5 게임 프레임워크 cnGameJS 개발 기록을 실현하는 애니메이션 원리의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.