phaser实现一个跳跃类小游戏代码

代码语言:html

所属分类:游戏

代码描述:phaser实现一个跳跃类小游戏代码,按住键盘上下左右键控制跳跃及方向。

代码标签: phaser 跳跃 游戏

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">






</head>

<body>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/phaser.2.0.5.js"></script>
    <script>
        var Jumper = function() {};
        Jumper.Play = function() {};
        
        Jumper.Play.prototype = {
        
          preload: function() {
            this.load.image( 'hero', '//repo.bfw.wiki/bfwrepo/images/phaser/dude.png' );
            this.load.image( 'pixel', '//repo.bfw.wiki/bfwrepo/images/phaser/pixel_1.png' );
          },
        
          create: function() {
            // background color
            this.stage.backgroundColor = '#6bf';
        
            // scaling
            this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
            this.scale.maxWidth = this.game.width;
            this.scale.maxHeight = this.game.height;
            this.scale.pageAlignHorizontally = true;
            this.scale.pageAlignVertically = true;
            this.scale.setScreenSize( true );
        
            // physics
            this.physics.startSystem( Phaser.Physics.ARCADE );
        
            // camera and platform tracking vars
            this.cameraYMin = 99999;
            this.platformYMin = 99999;
        
            // create platforms
            this.platformsCreate();
        
            // create hero
            this.heroCreate();
        
            // cursor controls
            this.cursor = this.input.keyboard.createCursorKeys();
          },
        
          update: function() {
            // this is where the main magic happens
            // the y offset and the height of the world are adjusted
            // to match the highest point the hero has reached
            this.world.setBounds( 0, -this.hero.yChange, this.world.width, this.game.height + this.hero.yChange );
        
            // the built in camera follow methods won't work for our needs
            // this is a custom follow style that will not ever move down, it only moves up
            this.cameraYMin = Math.min( this.cameraYMin, this.hero.y - this.game.height + 130 );
            this.camera.y = this.cameraYMin;
        
            // hero collisions and movement
            this.physics.arcade.collide( this.hero, this.platforms );
            this.heroMove();
        
            // for each plat form, find out which is the highest
            // if one goes below the camera view, then create a new one at a distance from the highest one
            // these are pooled so they are very performant
            this.platforms.forEachAlive( function( elem ) {
              this.platformYMin = Math.min( this.platformYMin, elem.y );
              if( elem.y > this.camera.y + this.game.height ) {
                elem.kill();
                this.platformsCreateOne( this.rnd.integerInRange( 0, this.world.width - 50 ), this.platformYMin - 100, 50 );
              }
            }, this );
          },
        
          shutdown: function() {
            // reset everything, or the world will be messed up
            this.world.setBounds( 0, 0, this.game.width, this.game.height );
            this.cursor = null;
            this.hero.destroy();
            this.hero = null;
            this.platforms.d.........完整代码请登录后点击上方下载按钮下载查看

网友评论0