Phaser path Trajectory Moving picture effect example

Source: Internet
Author: User
Tags min

The idea of the path

Our idea is to define a path and then let an object move in the interface according to the path.

Path principle

Actually the path in the computer is made up of dots, so we need to define a lot of points to draw a path?

Not to define a lot of points, we just need to define two or more points on the base to describe a path through phaser.

Draw a path

The code is as follows Copy Code

<! DOCTYPE html>
<meta charset= "Utf-8" >
<script src= "Js/phaser.min.js" ></script>
<body>
<div id= "Phaser-example" ></div>

<script>
Create a game interface
var game = new Phaser.game (A, Phaser.auto, ' phaser-example ', {

});

var phasergame = function () {
This.bmd = null;
This.points = {
' X ': [32, 600],
' Y ': [240, 240]
};
};

Phasergame.prototype = {
Create:function () {
This.stage.backgroundColor = ' #204090 ';
This.bmd = This.add.bitmapData (This.game.width, this.game.height);
This.bmd.addToWorld ();

           //Generate different Y-position
             var py = this.points.y;
            for (var i = 0; i < py.length i++) {
  ;               Py[i] = This.rnd.between (32 , 432);
           }
            this.bmd.clear ();
            var x = 1/game.width;

To generate a path through the X axis
for (var i = 0; I <= 1; i = = x) {
Linear algorithm Generation Path point
var px = this.math.linearInterpolation (this.points.x, i);
var py = this.math.linearInterpolation (this.points.y, i);

Draw lines
This.bmd.rect (px, py, 2, 2, ' Rgba (255, 255, 255, 1) ');
}

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

};
Game.state.add (' Game ', Phasergame, true);
</script>
</body>

Let the graph move according to the path

The code is as follows Copy Code

<! DOCTYPE html>
<meta charset= "Utf-8" >
<script src= "Js/phaser.min.js" ></script>
<body>
<div id= "Phaser-example" ></div>

<script>
Create a game interface
var game = new Phaser.game (A, Phaser.auto, ' phaser-example ', {

});

var phasergame = function () {

This.bmd = null;

This.points = {
' X ': [32, 128, 256, 384, 512, 608],
' Y ': [240, 240, 240, 240, 240, 240]
};

This.preload = function () {///when preload data is complete, execute create functions
Game.load.image (' opensource ', ' images/opensource.jpg ');
}
};

Phasergame.prototype = {

Create:function () {

This.stage.backgroundColor = ' #204090 ';

This.bmd = This.add.bitmapData (This.game.width, this.game.height);
This.bmd.addToWorld ();

var py = This.points.y;

for (var i = 0; i < py.length; i++)
{
Py[i] = This.rnd.between (32, 432);
}
opensource = game.add.sprite (0, 0, ' opensource ');
Tween = Game.add.tween (opensource). to (
{}, Phaser.Easing.Quartic.In, False, 0, 0)

This.plot ();

},

Plot:function () {

This.bmd.clear ();

var x = 1/game.width;

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);

var px = this.math.bezierInterpolation (this.points.x, i);
var py = this.math.bezierInterpolation (this.points.y, i);

var px = this.math.catmullRomInterpolation (this.points.x, i);
var py = this.math.catmullRomInterpolation (this.points.y, i);

This.bmd.rect (px, py, 1, 1, ' Rgba (255, 255, 255, 1) ');

Console.log (px%2);
if (Px%8 > 6) {
Tween.to (
{x:px,y:py}, Phaser.Easing.Linear.In, False, 0, 0);

}

}
Tween.start ();

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

};

Game.state.add (' Game ', Phasergame, true);
</script>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.