{"id":628,"date":"2021-11-01T00:59:50","date_gmt":"2021-11-01T00:59:50","guid":{"rendered":"http:\/\/mipython.magwebdesigns.net\/WP\/?p=628"},"modified":"2021-11-01T00:59:54","modified_gmt":"2021-11-01T00:59:54","slug":"mi-python-phaser-js-tutorial-two-extending-classes-and-sprite-movement","status":"publish","type":"post","link":"http:\/\/mipython.magwebdesigns.net\/WP\/2021\/11\/01\/mi-python-phaser-js-tutorial-two-extending-classes-and-sprite-movement\/","title":{"rendered":"MI PYTHON | Phaser JS Tutorial Two | Extending Classes and Sprite Movement"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Overview:<\/h1>\n\n\n\n<p>This lesson focuses on sprite movement within Phaser.  The Phaser game engine gives us different choices for movement styles.  We have keyboard \/ joystick and mouse follow type movement today. We will focus on these two movement methods below.  Before we do this we will also start organizing our game objects into a class based structure.  This will help us in scaling up the game down the road. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Extending Phaser Classes:<\/h2>\n\n\n\n<p>In order to better keep track of our game objects. We are going to implement an Object Oriented class based structure.  This will help us to keep track of all our game objects and instance&#8217;s in the future.  Lets start with the below code for our GameObj class.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/><\/div><\/td><td><div class=\"text codecolorer\">class GameObj extends Phaser.Physics.Arcade.Sprite &nbsp;{ &nbsp;<br \/>\n<br \/>\n&nbsp; &nbsp; constructor(scene,name,description,image, x, y) {<br \/>\n&nbsp; &nbsp; super(scene); &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; this.scene = scene;<br \/>\n&nbsp; &nbsp; this.name = name;<br \/>\n&nbsp; &nbsp; this.description = description;<br \/>\n&nbsp; &nbsp; \/\/\/ &nbsp; SET INTERACTIVE() FOR COLLISIONS<br \/>\n&nbsp; &nbsp; this.avatar = this.scene.physics.add.image(x,y, image).setInteractive();<br \/>\n&nbsp; &nbsp; this.x = x;<br \/>\n&nbsp; &nbsp; this.y = y;<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; status() {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(this.name);<br \/>\n&nbsp; &nbsp; &nbsp; console.log(this.description);<br \/>\n&nbsp; &nbsp; }<br \/>\n}<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>It&#8217;s ok if you don&#8217;t completely understand what&#8217;s happening here.  It will become clearer as we move along.  In the above code example.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Invoke our class GameObj extending from Phaser.Physics.Arcade.Sprite.<\/li><li>Initialize our GameObj class with the game scene, name, description, sprite image, x position, and y position as arguments.<\/li><li>The\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"text codecolorer\">super<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\nkeyword is used to call the constructor of its parent class to access the parent&#8217;s properties and methods.  We pass super into our class constructor to give our class access to the physics and other methods of Phaser.<\/li><li>This.scene is our game scene within Phaser.<\/li><li>This.name gives each instance an individual name.<\/li><li>this.description gives each instance and individual description.<\/li><li>this.avatar defines our GameObj&#8217;s sprite to display within the Phaser game canvas.  The physics process&#8217;s are actually running on this object within our class instance.<\/li><li>this.x defines the instance&#8217;s  avatar&#8217;s x coordinate position on the canvas<\/li><li>this.y defines the instance&#8217;s  avatar&#8217;s y coordinate position on the canvas<\/li><li>Lastly we have a status() method that displays the instance&#8217;s name and description.<\/li><\/ul>\n\n\n\n<p>After we initialize our GameObj class ,lets make a seperate class for our Player.  In our Player class we are going to include a few more methods then our GameObj class. Most importantly, our movement methods for the Player.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/><\/div><\/td><td><div class=\"text codecolorer\">&nbsp;class Player extends Phaser.Physics.Arcade.Sprite &nbsp;{ &nbsp;<br \/>\n<br \/>\n&nbsp; &nbsp; constructor(scene,name,description,image, x, y) {<br \/>\n&nbsp; &nbsp; super(scene); &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; this.scene = scene;<br \/>\n&nbsp; &nbsp; this.name = name;<br \/>\n&nbsp; &nbsp; this.description = description;<br \/>\n&nbsp; &nbsp; \/\/\/ &nbsp;SET INTERACTIVE FOR COLLISIONS<br \/>\n&nbsp; &nbsp; this.avatar = this.scene.physics.add.image(x,y, image).setInteractive();<br \/>\n&nbsp; &nbsp; this.x = x;<br \/>\n&nbsp; &nbsp; this.y = y;<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; status() {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(this.name);<br \/>\n&nbsp; &nbsp; &nbsp; console.log(this.description);<br \/>\n&nbsp; &nbsp; } <br \/>\n<br \/>\n&nbsp; &nbsp; keyBoardMovement() {<br \/>\n&nbsp; &nbsp; &nbsp; let cursors = this.scene.input.keyboard.createCursorKeys();<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (cursors.left.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;LEFT&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x -= 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; else if (cursors.right.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;RIGHT&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x += 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (cursors.up.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;UP&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.y -= 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; else if (cursors.down.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;DOWN&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; this.avatar.y += 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; pointerMovement(x,y) {<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (this.scene.input.activePointer.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x = x<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.avatar.y = y<br \/>\n&nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; else<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n<br \/>\n<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n<br \/>\n}<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Movement:<\/h2>\n\n\n\n<p>In the above code example.  We invoke our Player class exactly as we did the GameObj class.  The only difference being the added methods of  keyBoardMovement() and pointerMovement().  <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Within our keyBoardMovement() method we are using if statements to check to see if this.scene.input.keyboard.createCursorKeys() is down.  If the arrow keys are pressed our Player.avatar sprite will move in that particular direction on the Phaser canvas. Incremented by 10 pixels. <\/li><li>The pointerMovement() method allows us to move our Player.avatar sprite around with  mouse input.   We are checking if this.scene.input.activePointer.isDown = true. If the previous is true then we move the Player to that x and y position on the canvas instantly.   If you will notice in the next step.  The Player does move instantly where we click and can &#8220;Teleport&#8221; around the game screen.  We will address that shortly.<\/li><\/ul>\n\n\n\n<p>In the below code.  Lets Instantiate our class objects after our class initialization.  <\/p>\n\n\n\n<p>For both our Player and GameObj instances we will define them with the below arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&#8220;this&#8221; is the game scene that the instance is spawned into references the Phaser game object.<\/li><li>A unique name in a string.<\/li><li>a unique description in a string.<\/li><li>The key to the image we want for our objects avatar<\/li><li>The object avatar&#8217;s X position on the canvas.<\/li><li>The object avatar&#8217;s Y position on the canvas.<\/li><\/ul>\n\n\n\n<p>Organize our code and run the scene.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/>89<br \/>90<br \/>91<br \/>92<br \/>93<br \/>94<br \/>95<br \/>96<br \/>97<br \/>98<br \/>99<br \/>100<br \/>101<br \/>102<br \/>103<br \/>104<br \/>105<br \/>106<br \/>107<br \/>108<br \/>109<br \/>110<br \/>111<br \/>112<br \/>113<br \/>114<br \/>115<br \/>116<br \/>117<br \/>118<br \/>119<br \/>120<br \/>121<br \/>122<br \/>123<br \/>124<br \/>125<br \/>126<br \/>127<br \/>128<br \/>129<br \/>130<br \/>131<br \/>132<br \/>133<br \/>134<br \/>135<br \/>136<br \/>137<br \/>138<br \/><\/div><\/td><td><div class=\"text codecolorer\">const BLACK = &quot;#1c2333&quot;<br \/>\nconst RED = &quot;&quot;<br \/>\nconst WHITE = &quot;&quot;<br \/>\n<br \/>\nconst config = {<br \/>\n&nbsp; &nbsp; type: Phaser.AUTO,<br \/>\n&nbsp; &nbsp; width: 400,<br \/>\n&nbsp; &nbsp; height: 600,<br \/>\n&nbsp; &nbsp; backgroundColor: '#1c2333',<br \/>\n&nbsp; &nbsp; physics: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; default: 'arcade',<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; arcade: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gravity: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y: 0<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debug: true<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; },<br \/>\n&nbsp; &nbsp; scene: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; preload: preload,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; create: create,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; update: update<br \/>\n&nbsp; &nbsp; }<br \/>\n};<br \/>\n<br \/>\nconst game = new Phaser.Game(config);<br \/>\n<br \/>\n<br \/>\nfunction preload() {<br \/>\n&nbsp; this.sprite1 = this.load.image('sprite1', 'assets\/repl.png');<br \/>\n&nbsp; &nbsp; <br \/>\n}<br \/>\n<br \/>\nfunction create() {<br \/>\n<br \/>\n<br \/>\n&nbsp;class Player extends Phaser.Physics.Arcade.Sprite &nbsp;{ &nbsp;<br \/>\n<br \/>\n&nbsp; &nbsp; constructor(scene,name,description,image, x, y) {<br \/>\n&nbsp; &nbsp; super(scene); &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; this.scene = scene;<br \/>\n&nbsp; &nbsp; this.name = name;<br \/>\n&nbsp; &nbsp; this.description = description;<br \/>\n&nbsp; &nbsp; \/\/\/ &nbsp;SET INTERACTIVE FOR COLLISIONS<br \/>\n&nbsp; &nbsp; this.avatar = this.scene.physics.add.image(x,y, image).setInteractive();<br \/>\n&nbsp; &nbsp; this.x = x;<br \/>\n&nbsp; &nbsp; this.y = y;<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; status() {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;STATUS&quot;);<br \/>\n&nbsp; &nbsp; } <br \/>\n<br \/>\n&nbsp; &nbsp; keyBoardMovement() {<br \/>\n&nbsp; &nbsp; &nbsp; let cursors = this.scene.input.keyboard.createCursorKeys();<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (cursors.left.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;LEFT&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x -= 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; else if (cursors.right.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;RIGHT&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x += 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (cursors.up.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;UP&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.y -= 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; else if (cursors.down.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;DOWN&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; this.avatar.y += 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; pointerMovement(x,y) {<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (this.scene.input.activePointer.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x = x<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.avatar.y = y<br \/>\n&nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; else<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; }<br \/>\n&nbsp; }<br \/>\n<br \/>\n<br \/>\nclass GameObj extends Phaser.Physics.Arcade.Sprite &nbsp;{ &nbsp;<br \/>\n<br \/>\n&nbsp; &nbsp; constructor(scene,name,description,image, x, y) {<br \/>\n&nbsp; &nbsp; super(scene); &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; this.scene = scene;<br \/>\n&nbsp; &nbsp; this.name = name;<br \/>\n&nbsp; &nbsp; this.description = description;<br \/>\n&nbsp; &nbsp; \/\/\/ &nbsp; SET INTERACTIVE() FOR COLLISIONS<br \/>\n&nbsp; &nbsp; this.avatar = this.scene.physics.add.image(x,y, image).setInteractive();<br \/>\n&nbsp; &nbsp; this.x = x;<br \/>\n&nbsp; &nbsp; this.y = y;<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; status() {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;STATUS&quot;);<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n}<br \/>\n<br \/>\nplayer = new Player(this,&quot;PLAYER&quot;,&quot;PLAYER DESC&quot;,&quot;sprite1&quot;,100,100);<br \/>\nobject = new GameObj(this,&quot;GAME OBJECT&quot;,&quot;GAME OBJECT DESC&quot;,&quot;sprite1&quot;,100,100);<br \/>\nobject2 = new GameObj(this,&quot;GAME OBJECT 2&quot;,&quot;GAME OBJECT 2 DESC&quot;,&quot;sprite1&quot;,400,400);<br \/>\n<br \/>\n}<br \/>\n<br \/>\nfunction update() {<br \/>\n<br \/>\n&nbsp; const mousePosition = {<br \/>\n&nbsp; x:this.input.activePointer.x,<br \/>\n&nbsp; y:this.input.activePointer.y,<br \/>\n<br \/>\n&nbsp; }<br \/>\n&nbsp; <br \/>\n&nbsp; console.log(mousePosition.x);<br \/>\n&nbsp; console.log(mousePosition.y);<br \/>\n&nbsp; <br \/>\n&nbsp; player.keyBoardMovement()<br \/>\n&nbsp; player.pointerMovement(mousePosition.x,mousePosition.y)<br \/>\n<br \/>\n<br \/>\n}<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>After running the above code you should see that the Player object that we instanced is controllable with the mouse upon left click being true.   The Player avatar sprite instantly goes to that particular x \/ y position though.  This may or may not be the movement behavior you want in your game.  In this case though we are going to use the Phaser built in  method of physics.moveToObject().  Handily, Phaser calculates everything we need.  Both for smooth mouse and touch screen movement.  We can use this method for both the Player&#8217;s movement towards the mouse as well as other Game Objects movement towards the player or other waypoints.  Lets add that method below in the update method of our Phaser project.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/><\/div><\/td><td><div class=\"text codecolorer\">function update() {<br \/>\n<br \/>\n&nbsp; const mousePosition = {<br \/>\n&nbsp; x:this.input.activePointer.x,<br \/>\n&nbsp; y:this.input.activePointer.y,<br \/>\n<br \/>\n&nbsp; }<br \/>\n&nbsp; <br \/>\n&nbsp; console.log(mousePosition.x);<br \/>\n&nbsp; console.log(mousePosition.y);<br \/>\n&nbsp; <br \/>\n&nbsp; player.keyBoardMovement()<br \/>\n&nbsp; \/\/player.pointerMovement(mousePosition.x,mousePosition.y)<br \/>\n<br \/>\n&nbsp; \/\/\/ &nbsp; PLAYER SMOOTH MOUSE MOVMENT<br \/>\n&nbsp; this.physics.moveToObject(player.avatar, mousePosition, 400);<br \/>\n<br \/>\n&nbsp; \/\/\/ &nbsp; &nbsp;FOLLOW BEHAVIOR<br \/>\n&nbsp; this.physics.moveToObject(object.avatar, player.avatar, 200);<br \/>\n<br \/>\n}<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>In the above example.  We are calling this.physics.moveToObject().  <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>player.avatar is the first argument.  This references our Players physics sprite object.  This object moves to the next object.<\/li><li>mousePosition references our constant above that calculates the active pointers x and y position on the Phaser canvas.<\/li><li>400 is the  speed at which the object moves towards the target object.<\/li><\/ul>\n\n\n\n<p>We can also get a very easy to use follow behavior with this method.  In our next declaration we pass the object.avatar as our source object and the player.avatar as the target object.   Run the code.  We should now see the game object following the player.<\/p>\n\n\n\n<p>Final code should look like below.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/>67<br \/>68<br \/>69<br \/>70<br \/>71<br \/>72<br \/>73<br \/>74<br \/>75<br \/>76<br \/>77<br \/>78<br \/>79<br \/>80<br \/>81<br \/>82<br \/>83<br \/>84<br \/>85<br \/>86<br \/>87<br \/>88<br \/>89<br \/>90<br \/>91<br \/>92<br \/>93<br \/>94<br \/>95<br \/>96<br \/>97<br \/>98<br \/>99<br \/>100<br \/>101<br \/>102<br \/>103<br \/>104<br \/>105<br \/>106<br \/>107<br \/>108<br \/>109<br \/>110<br \/>111<br \/>112<br \/>113<br \/>114<br \/>115<br \/>116<br \/>117<br \/>118<br \/>119<br \/>120<br \/>121<br \/>122<br \/>123<br \/>124<br \/>125<br \/>126<br \/>127<br \/>128<br \/>129<br \/>130<br \/>131<br \/>132<br \/>133<br \/>134<br \/>135<br \/>136<br \/>137<br \/>138<br \/>139<br \/>140<br \/>141<br \/>142<br \/>143<br \/>144<br \/><\/div><\/td><td><div class=\"text codecolorer\">const BLACK = &quot;#1c2333&quot;<br \/>\nconst RED = &quot;&quot;<br \/>\nconst WHITE = &quot;&quot;<br \/>\n<br \/>\nconst config = {<br \/>\n&nbsp; &nbsp; type: Phaser.AUTO,<br \/>\n&nbsp; &nbsp; width: 400,<br \/>\n&nbsp; &nbsp; height: 600,<br \/>\n&nbsp; &nbsp; backgroundColor: '#1c2333',<br \/>\n&nbsp; &nbsp; physics: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; default: 'arcade',<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; arcade: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gravity: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y: 0<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; debug: true<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; },<br \/>\n&nbsp; &nbsp; scene: {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; preload: preload,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; create: create,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; update: update<br \/>\n&nbsp; &nbsp; }<br \/>\n};<br \/>\n<br \/>\nconst game = new Phaser.Game(config);<br \/>\n<br \/>\n<br \/>\nfunction preload() {<br \/>\n&nbsp; this.sprite1 = this.load.image('sprite1', 'assets\/repl.png');<br \/>\n&nbsp; &nbsp; <br \/>\n}<br \/>\n<br \/>\nfunction create() {<br \/>\n<br \/>\n&nbsp;\/\/input = this.input;<br \/>\n<br \/>\n&nbsp;class Player extends Phaser.Physics.Arcade.Sprite &nbsp;{ &nbsp;<br \/>\n<br \/>\n&nbsp; &nbsp; constructor(scene,name,description,image, x, y) {<br \/>\n&nbsp; &nbsp; super(scene); &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; this.scene = scene;<br \/>\n&nbsp; &nbsp; this.name = name;<br \/>\n&nbsp; &nbsp; this.description = description;<br \/>\n&nbsp; &nbsp; \/\/\/ &nbsp;SET INTERACTIVE FOR COLLISIONS<br \/>\n&nbsp; &nbsp; this.avatar = this.scene.physics.add.image(x,y, image).setInteractive();<br \/>\n&nbsp; &nbsp; this.x = x;<br \/>\n&nbsp; &nbsp; this.y = y;<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; status() {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;STATUS&quot;);<br \/>\n&nbsp; &nbsp; } <br \/>\n<br \/>\n&nbsp; &nbsp; keyBoardMovement() {<br \/>\n&nbsp; &nbsp; &nbsp; let cursors = this.scene.input.keyboard.createCursorKeys();<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (cursors.left.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;LEFT&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x -= 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; else if (cursors.right.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;RIGHT&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x += 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (cursors.up.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;UP&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; this.avatar.y -= 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; &nbsp; else if (cursors.down.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;DOWN&quot;);<br \/>\n&nbsp; &nbsp; &nbsp; this.avatar.y += 10;<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; pointerMovement(x,y) {<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; if (this.scene.input.activePointer.isDown)<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.avatar.x = x<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.avatar.y = y<br \/>\n&nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; else<br \/>\n&nbsp; &nbsp; &nbsp; {<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; }<br \/>\n&nbsp; &nbsp; }<br \/>\n&nbsp; }<br \/>\n<br \/>\n<br \/>\nclass GameObj extends Phaser.Physics.Arcade.Sprite &nbsp;{ &nbsp;<br \/>\n<br \/>\n&nbsp; &nbsp; constructor(scene,name,description,image, x, y) {<br \/>\n&nbsp; &nbsp; super(scene); &nbsp; &nbsp;<br \/>\n&nbsp; &nbsp; this.scene = scene;<br \/>\n&nbsp; &nbsp; this.name = name;<br \/>\n&nbsp; &nbsp; this.description = description;<br \/>\n&nbsp; &nbsp; \/\/\/ &nbsp; SET INTERACTIVE() FOR COLLISIONS<br \/>\n&nbsp; &nbsp; this.avatar = this.scene.physics.add.image(x,y, image).setInteractive();<br \/>\n&nbsp; &nbsp; this.x = x;<br \/>\n&nbsp; &nbsp; this.y = y;<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n&nbsp; &nbsp; status() {<br \/>\n&nbsp; &nbsp; &nbsp; console.log(&quot;STATUS&quot;);<br \/>\n&nbsp; &nbsp; }<br \/>\n<br \/>\n}<br \/>\n<br \/>\nplayer = new Player(this,&quot;PLAYER&quot;,&quot;PLAYER DESC&quot;,&quot;sprite1&quot;,100,100);<br \/>\nobject = new GameObj(this,&quot;GAME OBJECT&quot;,&quot;GAME OBJECT DESC&quot;,&quot;sprite1&quot;,100,100);<br \/>\nobject2 = new GameObj(this,&quot;GAME OBJECT 2&quot;,&quot;GAME OBJECT 2 DESC&quot;,&quot;sprite1&quot;,400,400);<br \/>\n<br \/>\n}<br \/>\n<br \/>\nfunction update() {<br \/>\n<br \/>\n&nbsp; const mousePosition = {<br \/>\n&nbsp; x:this.input.activePointer.x,<br \/>\n&nbsp; y:this.input.activePointer.y,<br \/>\n<br \/>\n&nbsp; }<br \/>\n&nbsp; <br \/>\n&nbsp; console.log(mousePosition.x);<br \/>\n&nbsp; console.log(mousePosition.y);<br \/>\n&nbsp; <br \/>\n&nbsp; player.keyBoardMovement()<br \/>\n&nbsp; \/\/player.pointerMovement(mousePosition.x,mousePosition.y)<br \/>\n<br \/>\n&nbsp; \/\/\/ &nbsp; PLAYER SMOOTH MOUSE MOVMENT<br \/>\n&nbsp; this.physics.moveToObject(player.avatar, mousePosition, 400);<br \/>\n<br \/>\n&nbsp; \/\/\/ &nbsp; &nbsp;FOLLOW BEHAVIOR<br \/>\n&nbsp; this.physics.moveToObject(object.avatar, player.avatar, 200);<br \/>\n<br \/>\n}<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Repl for this section here:<\/p>\n\n\n\n<p><a href=\"https:\/\/replit.com\/@MANDREWS85\/phasertutorial2movementfinal#script.js\">https:\/\/replit.com\/@MANDREWS85\/phasertutorial2movementfinal#script.js<\/a><\/p>\n\n\n\n<p>Check out MI PYTHON Phaser JS Tutorial 3 here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview: This lesson focuses on sprite movement within Phaser. The Phaser game engine gives us different choices for movement styles. We have keyboard \/ joystick and mouse follow type movement today. We will focus on these two movement methods below. Before we do this we will also start organizing our game objects into a class based structure. This will help us in scaling up the game down the road. Extending Phaser Classes: In order to better keep track of our game objects. We are going to implement an Object Oriented&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":1,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":629,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/628\/revisions\/629"}],"wp:attachment":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}