Animation PHASER ALTARA与运动路径、敌人ai和动画搏斗

Animation PHASER ALTARA与运动路径、敌人ai和动画搏斗,animation,path,phaser-framework,motion,Animation,Path,Phaser Framework,Motion,基本上,我想让我的精灵跟随一个运动路径,根据它的方向,它将播放一个特定的动画。i、 e.向上移动将显示其背面,向左移动将显示精灵的左侧,依此类推 我已经试了好几个小时了,但没有成功。我有一些运气使用原型,但最终的游戏将使用下面的结构。任何帮助都将不胜感激 /* * initalise Phaser framework with width:960px, height:540px */ var game = new Phaser.Game(960, 540, Phaser.AUTO, 'gam

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

我已经试了好几个小时了,但没有成功。我有一些运气使用原型,但最终的游戏将使用下面的结构。任何帮助都将不胜感激

/*
 * 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');

   }
*/

}
/*
*初始化移相器框架,宽度:960px,高度:540px
*/
var game=new Phaser.game(960540,Phaser.AUTO,'gameContainer',{preload:preload,create:create,update:update,});
/*
*在游戏开始前预加载运行。图像和声音等资产可以在此处预加载。
*加载资源需要Web服务器。
*
*此外,在这个功能中,我们设置了游戏的比例,使其达到整个浏览器的宽度。
*/
函数预加载(){
//设置为缩放到全浏览器宽度
this.scale.scaleMode=Phaser.ScaleManager.SHOW_ALL;
this.scale.parentIsWindow=true;
//设置背景色,以便在浏览器中确认游戏渲染
this.stage.backgroundColor='#4488cc';
this.game.renderer.renderSession.roundPixels=true;
//预加载图像和声音
//game.load.image('key','folder/filename.png');
//this.load.image('nazil','image/nazil.png');
game.load.spritesheet('nazil','images/nazil.png',128,128,6);
this.bmd=null;
this.alien=null;
该模式=0;
//使用此网站设置敌人的移动http://phaser.io/waveforms. 从控制台日志导出保存数据。
此值为0.5分={
“类型”:0,“闭合”:真,“x”:[120120260260200180120],“y”:[368108108308368368]
};
这是0.pi=0;
this.path=[];
}
/*
*在此处添加游戏变量
*/
纳粹党;
/*
*仅在相位器首次加载时创建一次运行
*通过向舞台添加对象来创建游戏场景
*/
函数create(){
bmd=this.add.bitmapData(this.game.width,this.game.height);
bmd.addToWorld();
/*
用于测试
this.alien=this.add.sprite(0,0,'alien');
本.外星人.锚.集(0.5);
*/
this.nazil=this.add.sprite(0,0,'nazil');
本.纳粹.锚.集(0.5);
var py=这个.points.y;
/*原始代码
//定义动画
nazil.animations.add('walk');
//以30fps的速度启动动画
纳粹。动画。播放('walk',3,true);
*/
//定义动画
this.nazil.animations.add('walkDown',[2,3]);
//以30fps的速度启动动画
这个.nazil.animations.play('walkDown',3,true);
//定义动画
this.nazil.animations.add('walkLR',[4,5]);
//以30fps的速度启动动画
这个.nazil.animations.play('walkLR',3,true);
//定义动画
this.nazil.animations.add('walkUp',[0,1]);
//以30fps的速度启动动画
这个.nazil.animations.play('walkUp',3,true);
}
函数图(){
这个.bmd.clear();
this.path=[];
/*轮换代码*/
var ix=0;
/**/
//设置精灵的速度
var x=0.5/游戏宽度;
//循环通过x和y阵列中的打印点
对于(变量i=0;i 0)
{
node.angle=this.math.angleBetweenPoints(this.path[ix-1],node);
}
这个.path.push(节点);
ix++;
/**/
//this.path.push({x:px,y:py});
这个.bmd.rect(px,py,1,1,'rgba(255,255,255,1');
}
对于(var p=0;p=this.path.length)
{
这是0.pi=0;
}
/*
//基于速度翻转播放器图像
如果(物体速度x>0){
//玩家向右移动
1.scale.x=-1;
纳粹。动画。播放('walkLR');
}
否则如果(物体速度x<0){
//玩家正在向左移动
nazil.scale.x=1;//翻转图像
纳粹。动画。播放('walkLR');
}
否则如果(物体速度y<0){
纳粹。动画。播放(“walkUp”);
}
否则,如果(物体速度y>0){
//玩家没有移动
纳粹。动画。播放(‘漫步’);
}
*/
}