K
K
Karmen Sahakyan2017-04-11 17:56:23
Game development
Karmen Sahakyan, 2017-04-11 17:56:23

How to create collision in mario game left and right?

function Player(){
//baxum sharjum
  this.dynamic = $(".dynamic");
    this.directions = {};
    this.walls = [$(".pat2"),$(".pat3"),$(".pat4"),$(".pat5"),$(".pat6"),$(".pat7"),$(".pat8"),$(".pat9"),$(".pat10"),$(".pat11"),$(".pat12"),$(".pat13"),$(".pat14"),$(".pat15"),$(".pat16"),$(".pat17"),$(".pat18"),$(".pat19"),$(".pat20"),$(".pat21"),$(".pat22"),$(".pat23"),$(".pat24"),$(".pat25"),$(".pat26"),$(".pat27"),$(".pat28"),$(".pat29"),$(".pat30"),$(".pat31"),$(".pat32"),$(".pat33"),$(".pat34"),$(".pat35"),$(".pat36"),$(".pat37"),$(".pat38"),$(".pat39"),$(".pat40"),$(".pat42"),$(".pat43")];
    this.speedX = 4;
    this.speedY = 0;
    this.gravity = 0.2;
    this.jumpState = true;

    this.STATIC_HOR_CENTER;
    this.STATIC_VER_CENTER;

    this.WIDTH_DIFFERENCE;
    this.HEIGHT_DIFFERENCE;

    this.DYNAMIC_HOR_CENTER;
    this.DYNAMIC_VER_CENTER; 
   



    this.charMovement = function (e)
     {
        that.directions[e.which] = true;
        
     };
      this.stop =function(e)
       {
        delete that.directions[e.which];
         };
    

    this.checkCollisionup=function () {
        for (var i in this.walls) {
            this.STATIC_HOR_CENTER = this.walls[i].position().left+$(".home").position().left + this.walls[i].width()/2;
            this.STATIC_VER_CENTER = this.walls[i].position().top;

            this.WIDTH_DIFFERENCE = this.dynamic.width()/2 +this.walls[i].width()/2;
            
            this.DYNAMIC_HOR_CENTER = this.dynamic.position().left + this.dynamic.width()/2;
            this.DYNAMIC_VER_CENTER = this.dynamic.position().top + this.dynamic.height();
            if (Math.abs(this.DYNAMIC_HOR_CENTER - this.STATIC_HOR_CENTER) <= this.WIDTH_DIFFERENCE && Math.abs(this.DYNAMIC_VER_CENTER - this.STATIC_VER_CENTER) <= 5) {
                
                this.jumpState = false;
                this.dynamic.css("top", this.walls[i].position().top - this.dynamic.height());
                this.speedY = 9;
                break;

            }
          

            else {
                
                if (this.jumpState == false) {
                    this.speedY = 0;
                    this.jumpState = true;
                }
            }
        }
    }
    this.checkCollisiondown = function(){
        for (var i in this.walls) {
            this.STATIC_HOR_CENTER = this.walls[i].position().left+$(".home").position().left + this.walls[i].width()/2;
            this.STATIC_VER_CENTER = this.walls[i].position().top+this.dynamic.height()/2;

            this.WIDTH_DIFFERENCE = this.dynamic.width()/2 +this.walls[i].width()/2;
            this.HEIGHT_DIFFERENCE = this.dynamic.height()/2 +this.walls[i].height()/2;
            
            this.DYNAMIC_HOR_CENTER = this.dynamic.position().left + this.dynamic.width()/2;
            this.DYNAMIC_VER_CENTER = this.dynamic.position().top + this.dynamic.height()/2;
          if (Math.abs(this.DYNAMIC_HOR_CENTER - this.STATIC_HOR_CENTER) <= this.WIDTH_DIFFERENCE && Math.abs(this.DYNAMIC_VER_CENTER - this.STATIC_VER_CENTER) <= 5)
              {   
                
                this.speedX = 4;
               
            }        
      
            }
    }
    
    var that = this;

    this.move=function (e) {
    	for (var i in this.directions) {
    		if (this.dynamic.position().left > 0 && i == 37)
             {
    			if(this.dynamic.position().left <= 400 && $(".home").position().left<=0){
    				$(".home").css("left", ($(".home").position().left + this.speedX) + "px");
    			}
    			else {
    				this.dynamic.css("left", (this.dynamic.position().left - this.speedX) + "px");

    			}

    		}
    		if (this.dynamic.position().left < ($(".home").width() - this.dynamic.width()) && i == 39) {
    			if(this.dynamic.position().left >= 800){
    				$(".home").css("left", ($(".home").position().left - this.speedX) + "px");
    			}
    			else {
    				this.dynamic.css("left", (this.dynamic.position().left + this.speedX) + "px");

    			}
    		}

    		if (this.dynamic.position().top > 0 && i == 38 && this.jumpState == false) {
    			this.jumpState = true;
                this.dynamic.css("top", this.dynamic.position().top -6);
    		}
    		
    	}
 

    }
    
    this.jump =function () {
        if (this.jumpState) {
            if (this.speedY > -9) {
                this.speedY -= this.gravity;
            }
            this.dynamic.css("top", this.dynamic.position().top - this.speedY);
        }
    }

};


// ------------------------------Tshnami----------------------------------------------------

function Enemy(){

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question