Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101

Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101

Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":3254,"date":"2017-12-21T05:26:41","date_gmt":"2017-12-21T05:26:41","guid":{"rendered":"https:\/\/phasergames.com\/?p=3254"},"modified":"2017-12-21T05:29:09","modified_gmt":"2017-12-21T05:29:09","slug":"phaser-snow","status":"publish","type":"post","link":"https:\/\/phasergames.com\/phaser-snow\/","title":{"rendered":"Phaser Snow"},"content":{"rendered":"

Let There Be Snow!<\/h1>\n

I’ll admit I’m not anywhere near snow while writing this, but I love it as an effect in a game, and since it is seasonal to Christmas time, I was inspired to put this together.<\/p>\n

The original\u00a0code<\/h3>\n

Originally I had placed several flakes, which was a simple Phaser graphic and looping through the group with a forEach<\/p>\n

this.snowGroup.forEach(function(flake){\r\n\tflake.y++;\r\n}.bind(this))<\/pre>\n

This worked fine at first, but the problem came when I tried to increase the number of flakes from 50 to 200, and then as a test up to 500. The code started going sluggish, and then I remembered an old interview question about a starfield. I have to admit I failed that interview question but looked the answer up later after becoming\u00a0obsessed with the problem.<\/p>\n

A linked list<\/h2>\n

A linked list works by each child of a group having a reference to the child before or after it, or sometimes both. Once you have the reference you call a function of the first or last child in the group, and that function then calls the same function in the next or previous child, and it starts a chain reaction. This can be incredibly fast! I implement this in the move function of the flake class below.<\/p>\n

 <\/p>\n

\"\"<\/a><\/p>\n

Click the image to see the result<\/p>\n

How to use<\/p>\n

\/\/change width and height if you only want to use a\r\n\/\/small area\r\nvar snow=new Snow(game.width,game.height);<\/pre>\n

The Code<\/h2>\n

The Snow Class<\/h3>\n
class Snow extends Phaser.Group {\r\n    constructor(w = 200, h = 200) {\r\n        super(game);\r\n        this.w = w;\r\n        this.h = h;\r\n        this.lastFlake = null;\r\n        \/\/\r\n        \/\/\r\n        \/\/\r\n        this.makeFlakes();\r\n    }\r\n    makeFlakes() {\r\n        for (var i = 0; i < 250; i++) {\r\n            var flake = new Flake(this.w, this.h);\r\n            \/\/if the last flake exists \r\n            \/\/place it as the prev flake\r\n            if (this.lastFlake != null) {\r\n                flake.prevFlake = this.lastFlake;\r\n            }\r\n            this.lastFlake = flake;\r\n        }\r\n    }\r\n    update() {\r\n    \t\/\/move the last flake\r\n    \t\/\/this will start a chain reaction\r\n        this.lastFlake.move();\r\n    }\r\n}<\/pre>\n

The Flake Class<\/h3>\n
class Flake extends Phaser.Group {\r\n    constructor(w, h) {\r\n        super(game);\r\n\r\n        \/\/make the graphic\r\n        var f = game.add.graphics();\r\n        f.beginFill(0xffffff, 1);\r\n        f.drawCircle(0, 0, 10);\r\n        f.endFill();\r\n        this.add(f);\r\n        \/\/\r\n        \/\/\r\n        this.w=w;\r\n        this.h=h;\r\n        \/\/\r\n        \/\/\r\n        \/\/init properties\r\n        \r\n        this.y = game.rnd.integerInRange(0, h);\r\n        this.reset();\r\n    }\r\n    reset()\r\n    {\r\n    \t\/\/re-init properites\r\n    \tthis.x = game.rnd.integerInRange(0, this.w);\r\n    \tthis.drift = game.rnd.integerInRange(-1, 1) * (.05 + Math.random() * .1);\r\n        this.fallSpeed = 1 + Math.random() * 10;\r\n        this.scale.x = .1 + Math.random();\r\n        this.scale.y = this.scale.x;\r\n        this.alpha = .1 + Math.random();\r\n    }\r\n    move()\r\n    {\r\n    \tthis.x+=this.drift;\r\n    \tthis.y+=this.fallSpeed;\r\n    \tif (this.y>this.h)\r\n    \t{\r\n    \t\t\/\/take back to top\r\n    \t\tthis.y=-10;\r\n    \t\tthis.reset();\r\n    \t}\r\n    \tif (this.prevFlake)\r\n    \t{\r\n    \t\t\/\/move the previous flake\r\n    \t\tthis.prevFlake.move();\r\n    \t}\r\n    }\r\n}<\/pre>\n

Happy Holidays Looking forward to more Phaser in 2018!<\/p>\n\t

\n\n\t\t\n\t\t
\n\t\t\t