Home  >  Article  >  Web Front-end  >  HTML5 game framework cnGameJS development record-game scene object

HTML5 game framework cnGameJS development record-game scene object

黄舟
黄舟Original
2017-03-24 15:58:242058browse

1. When is the sceneobject needed?

Scene objects are different from the map objects introduced in the previous article. They are used in different types of games. Previous map objects were used in grid games, such as Sokoban and Tank Battle. The scene objects introduced in this section are suitable for games with specific scenes, such as Super Mario, Dinosaur Kombat, etc. This type of game usually controls a player object in a 2D scene. As the player moves, the scene moves with it.

2. Scene example:

Effect: (left and right keys control Super Mario’s movement)

HTML5 game framework cnGameJS development record-game scene object


Code:


请使用支持canvas的浏览器查看

3. Code implementation:
To construct a scene, you first need a background that is wide enoughPictureWhen the player moves to the right, the player is always at the midpoint of the background, and the player's speed is converted to the background in the opposite direction The speed of movement. First look at the initialization function:

/**
         *初始化
        **/
        init:function(options){
            /**
             *默认对象
            **/
            var defaultObj={
                width:cg.width,
                height:cg.height,
                imgWidth:cg.width,
                imgHeight:cg.height,
                x:0,
                y:0
                
            }
            options=options||{};
            options=cg.core.extend(defaultObj,options);
            this.player=options.player;
            this.width=options.width;
            this.height=options.height;
            this.imgWidth=options.imgWidth;
            this.imgHeight=options.imgHeight;
            this.centerX=this.width/2;
            this.src=options.src;
            this.x=options.x;
            this.y=options.y;
            this.insideArr=[];
            this.isLoop=false;;
            this.isCenterPlayer=false;
            this.onEnd=options.onEnd;
            
        },

In addition to xy and size, the parameters passed in by the user also have three parameters. One parameter is to set whether to place the player object in the center and only move the background. without moving the player. If you want to achieve the above background moving effect, this parameter must be set to true. Another parameter is to set whether to loop. If set to loop, after the background moves to the extreme point, it will return to the original position. The last parameter is onEnd. If it is set to acyclic, the callback function will be triggered after the background moves to the extreme.

The focus of the scene object is the update method:

/**
         *背景移动时的更新
        **/
        update:function(spritelist){//传入所有sprite的数组
            if(this.isCenterPlayer){
                if(this.player.x>this.centerX){
                    if(this.x

This method first determines whether the player object has exceeded the center of the scene. If it has exceeded, calculate the exceeded distance and fix the player In the center of the scene, the excess distance is set to the distance between the background moving in the opposite direction and the distance other sprites except the player move in the opposite direction. In this case, only the background moves and other sprite objects move, and the player is fixed. If it is a loop, reset the x coordinate of the background and other sprites after exceeding the movement range. If it is not a loop, the onEnd callback function is called after the movement ends. In addition, if you need to restrict the player to always be within the display area, you can also call the insideView method.

Attach all the codes of the scene object:

/**
 *
 *场景
 *
**/
cnGame.register("cnGame",function(cg){
    
    /**
     *使指定对象在可视区域view内
    **/
    var inside=function(sprite){
        var dir=sprite.insideDir;
        if(dir!="y"){
            if(sprite.x<0){
                sprite.x=0;
            }
            else if(sprite.x>this.width-sprite.width){
                sprite.x=this.width-sprite.width;
            }
        }
        if(dir!="x"){
            if(sprite.y<0){
                sprite.y=0;
            }
            else if(sprite.y>this.height-sprite.height){
                sprite.y=this.height-sprite.height;
            }
        }
            
    }
    
    var view=function(options){
        this.init(options);
        
    }
    view.prototype={
    
        /**
         *初始化
        **/
        init:function(options){
            /**
             *默认对象
            **/
            var defaultObj={
                width:cg.width,
                height:cg.height,
                imgWidth:cg.width,
                imgHeight:cg.height,
                x:0,
                y:0
                
            }
            options=options||{};
            options=cg.core.extend(defaultObj,options);
            this.player=options.player;
            this.width=options.width;
            this.height=options.height;
            this.imgWidth=options.imgWidth;
            this.imgHeight=options.imgHeight;
            this.centerX=this.width/2;
            this.src=options.src;
            this.x=options.x;
            this.y=options.y;
            this.insideArr=[];
            this.isLoop=false;;
            this.isCenterPlayer=false;
            this.onEnd=options.onEnd;
            
        },
        /**
         *使player的位置保持在场景中点之前的移动背景
        **/
        centerPlayer:function(isLoop){
            isLoop=isLoop||false;
            this.isLoop=isLoop;
            this.isCenterPlayer=true;
        },
        /**
         *使对象的位置保持在场景内
        **/
        insideView:function(sprite,dir){//dir为限定哪个方向在view内,值为x或y,不传则两个方向皆限定
            if(cg.core.isArray(sprite)){
                for(var i=0,len=sprite.length;ithis.centerX){
                    if(this.x

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

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