PHASER ALTARA在运动路径,敌人ai和动画中苦苦挣扎

时间:2015-09-27 06:13:23

标签: animation path phaser-framework motion

基本上我想让我的精灵跟随一个运动路径,并根据它的方向,它将播放一个特定的动画。即向上移动将显示其背面,向左移动将显示精灵的左侧,依此类推。

我已经尝试了好几个小时,但试图让这项工作无济于事。我有一些运气使用原型,但最终的游戏将使用下面的结构。任何帮助将不胜感激。

/*
 * initalise Phaser framework with width:960px, height:540px
 */
var game = new Phaser.Game(960, 540, Phaser.AUTO, 'gameContainer', { preload: preload, create: create, update: update, });
/*
 * Preload runs before the game starts. Assets such as images and sounds such be preloaded here.
 * A webserver is required to load assets.
 *
 * Also in this function we set game scale so it full browser width.
 */
function preload() {
    // set to scale to full browser width
    this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    this.scale.parentIsWindow = true;
    //set the background color so can confirm the game renders in the browser
    this.stage.backgroundColor = '#4488cc';
    this.game.renderer.renderSession.roundPixels = true;

    //preload images & sounds

    //game.load.image('key', 'folder/filename.png');
    //this.load.image('nazi', 'image/nazi.png');
    game.load.spritesheet('nazi', 'images/nazi.png', 128, 128, 6);

    this.bmd = null;
    this.alien = null;
    this.mode = 0;
    //Use this website to set enemy movements http://phaser.io/waveforms. Export save data from the console log.
    this.points = {
        "type":0,"closed":true,"x":[120,120,260,260,200,180,120],"y":[368,108,108,308,308,368,368]
    };
    this.pi = 0;
    this.path = [];
}

/*
* Add game variables here
*/
var nazi;
/*
 * Create runs once only when Phaser first loads
 * create the game scene by adding objects to the stage
 */
function create() {
    bmd = this.add.bitmapData(this.game.width, this.game.height);
    bmd.addToWorld();
    /*
    For testing
    this.alien = this.add.sprite(0, 0, 'alien');
    this.alien.anchor.set(0.5);
    */
    this.nazi = this.add.sprite(0, 0, 'nazi');
    this.nazi.anchor.set(0.5);

    var py = this.points.y;


    /*Original Code
    //define the animation
    nazi.animations.add('walk');
    //start the animation at 30fps
    nazi.animations.play('walk', 3, true);
    */
    //define the animation
    this.nazi.animations.add('walkDown', [2, 3]);
    //start the animation at 30fps
    this.nazi.animations.play('walkDown', 3, true);
    //define the animation
    this.nazi.animations.add('walkLR', [4, 5]);
    //start the animation at 30fps
    this.nazi.animations.play('walkLR', 3, true);
    //define the animation
    this.nazi.animations.add('walkUp', [0, 1]);
    //start the animation at 30fps
    this.nazi.animations.play('walkUp', 3, true);

}
function plot() {
    this.bmd.clear();
    this.path = [];
    /*ROTATION CODE*/
    var ix = 0;
    /**/

    //Sets the speed of the sprite
    var x = 0.5 / game.width;
    //looping through plotting points from x and y array
    for (var i = 0; i <= 1; i += x) {
        var px = this.math.linearInterpolation(this.points.x, i);
        var py = this.math.linearInterpolation(this.points.y, i);
        /* ROTATION CODE to follow direction of path*/
        var node = { x: px, y: py, angle: 0 };
            if (ix > 0)
            {
            node.angle = this.math.angleBetweenPoints(this.path[ix - 1], node);
            }
        this.path.push(node);
        ix++;
        /**/
        //this.path.push( { x: px, y: py });
        this.bmd.rect(px, py, 1, 1, 'rgba(255, 255, 255, 1)');
        }
    for (var p = 0; p < this.points.x.length; p++) {
        this.bmd.rect(this.points.x[p]-3, this.points.y[p]-3, 6, 6, 'rgba(255, 0, 0, 1)');
        }
}
/*
 * Update runs continuously. Its the game loop function.
 * Add collision detections and control events here
 */
function update() {
    plot();
     //  Reset the players velocity (movement)

    //this.nazi = 'nazi';
            /* For Testing
            this.alien.x = this.path[this.pi].x;
            this.alien.y = this.path[this.pi].y;

            //ROTATION CODE: 
            this.alien.rotation = this.path[this.pi].angle;
            */
            this.nazi.x = this.path[this.pi].x;
            this.nazi.y = this.path[this.pi].y;
            //this.nazi.rotation = this.path[this.pi].angle;

            this.pi++;
            if (this.pi >= this.path.length)
            {
                this.pi = 0;
            }
            /*
            // Flipping the player image based on the velocity
    if(nazi.body.velocity.x > 0){
       //player is moving right
       nazi.scale.x = -1;
       nazi.animations.play('walkLR');
   }
   else if(nazi.body.velocity.x < 0){
        //player is moving left
       nazi.scale.x = 1; //flip the image
       nazi.animations.play('walkLR');
   }
   else if (nazi.body.velocity.y < 0){
       nazi.animations.play('walkUp');

       }
   else if(nazi.body.velocity.y > 0){
       //player is not moving
       nazi.animations.play('walkDown');

   }
*/

}

0 个答案:

没有答案