简体   繁体   English

如何使HTML5游戏成为障碍?

[英]How do I make HTML5 game obstacles?

I'm making a HTML5 game without any frameworks. 我正在制作没有任何框架的HTML5游戏。 My player can go up, down, left and right, but it can even walk through obstacles. 我的播放器可以上下左右移动,但它甚至可以穿越障碍物。 I edited some things and now it can't get through obstacles, but when my player walks against it, the player can't get back. 我编辑了一些内容,现在它无法克服障碍,但是当我的播放器对着它行走时,播放器无法返回。 He gets stuck. 他被卡住了。

All fixed objects are stored in this.fixed and all sprites are stored in this.sprite . 所有固定对象都存储在this.fixed ,所有子this.fixed都存储在this.sprite Structure of this.sprite is: this.sprite结构是:

  • 0 = x 0 = x
  • 1 = y 1 = y
  • 2 = w 2 = w
  • 3 = h 3 =小时
  • Four and five doesn't matter. 四个和五个没关系。

This is the code: 这是代码:

moveSprite:function( name, x, y, w, h, type)
    {   
        w = typeof w !== 'undefined' ? w : this.sprite[name][2];
        h = typeof h !== 'undefined' ? h : this.sprite[name][3];
        type = typeof type !== 'undefined' ? type : 'rect';

        for ( f in this.fixed )
        {
            if 
            ( 
              // X

              /* Left */ this.sprite[this.fixed[f]][0] - 3 < this.sprite[name][0] + this.sprite[name][2] && 
              /* Right */ this.sprite[this.fixed[f]][0] + this.sprite[this.fixed[f]][2]  > this.sprite[name][0] &&

              // Y

              /* Bottom */ this.sprite[this.fixed[f]][1] < this.sprite[name][1] + this.sprite[name][3] && 
              /* Top */ this.sprite[this.fixed[f]][1]  + this.sprite[this.fixed[f]][3] > this.sprite[name][1]
            )
            {
                return;
            }
        }

        if( type == 'rect' )
        {
            this.context.clearRect(this.sprite[name][0],this.sprite[name][1],this.sprite[name][2],this.sprite[name][3]);
            this.context.fillRect(x,y,w,h);
        }
        else
        {

            var path = this.sprite[name][5];
            this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
            delete this.sprite[name];

            for ( var i in this.sprite )
            {
                if( this.sprite[i][4] == 'rect' )
                {
                    this.context.fillRect(this.sprite[i][0],this.sprite[i][1],this.sprite[i][2],this.sprite[i][3]);
                }
                else if ( this.sprite[i][4] == 'image' )
                {
                    this.addImage( this.sprite[i][5], this.sprite[i][0], this.sprite[i][1], this.sprite[i][2], this.sprite[i][3] );
                }   
            }

            this.addSprite(name, x,y,w,h,'image', path);


        }

    },

I use the function with the name from the player sprite. 我使用带有播放器精灵名称的函数。

What did I tried? 我尝试了什么?

I Google'd for collision detection, now I have collision detection, but my player gets stuck. 我用Google进行碰撞检测,现在有了碰撞检测,但是我的播放器被卡住了。 I also Google'd for HTML5 obstacles but I didn't found anything useful about that. 我也曾为HTML5设置了Google障碍,但没有发现任何有用的方法。

With the comments given, I made this: 通过给出的评论,我做到了:

moveSprite:function( name, x, y, movement, w, h, type)
    {   
        w = typeof w !== 'undefined' ? w : this.sprite[name][2];
        h = typeof h !== 'undefined' ? h : this.sprite[name][3];
        type = typeof type !== 'undefined' ? type : 'rect';

        //console.log('sdf');
        for ( f in this.fixed )
        {
            if ( this.stuck === false )
            {
                if 
                ( 
                  // X

                  /* Left */ this.sprite[this.fixed[f]][0] < this.sprite[name][0] + this.sprite[name][2] && 
                  /* Right */ this.sprite[this.fixed[f]][0] + this.sprite[this.fixed[f]][2]  > this.sprite[name][0] &&

                  // Y

                  /* Bottom */ this.sprite[this.fixed[f]][1] < this.sprite[name][1] + this.sprite[name][3] && 
                  /* Top */ this.sprite[this.fixed[f]][1]  + this.sprite[this.fixed[f]][3] > this.sprite[name][1]
                )
                {
                    this.stuck = movement;
                    return;
                }

            }
            else if ( this.stuck !== false && movement == this.stuck )
            {
                this.block = true;
                return;
            }
            if ( this.stuck !== false && this.block == true && movement != this.stuck )
            {
                this.block = false;
                this.stuck = false;
                movement = null;
                console.log(1);
            }
        }

        if( type == 'rect' )
        {
            this.context.clearRect(this.sprite[name][0],this.sprite[name][1],this.sprite[name][2],this.sprite[name][3]);
            this.context.fillRect(x,y,w,h);
        }
        else
        {

            var path = this.sprite[name][5];
            this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
            delete this.sprite[name];

            for ( var i in this.sprite )
            {
                if( this.sprite[i][4] == 'rect' )
                {
                    this.context.fillRect(this.sprite[i][0],this.sprite[i][1],this.sprite[i][2],this.sprite[i][3]);
                }
                else if ( this.sprite[i][4] == 'image' )
                {
                    this.addImage( this.sprite[i][5], this.sprite[i][0], this.sprite[i][1], this.sprite[i][2], this.sprite[i][3] );
                }   
            }

            this.addSprite(name, x,y,w,h,'image', path);


        }

    },

Now it works, thanks for your help! 现在可以使用了,谢谢您的帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM