Professional HTML5 Mobile Game Development by Pascal Rettig
Author:Pascal Rettig
Language: eng
Format: epub
Publisher: Wiley Publishing, Inc.
Published: 2012-08-17T16:00:00+00:00
Adding in the Player
With all these components in place, up next is adding in a player class. This player class will represent the player as they move around the game and encapsulate all their functionality. All you need to do is subclass the Q.Sprite class, set some basic properties in the constructor, and add the components that were built in the last section.
Add the Q.Player class, as shown in Listing 13-9, below the components defined previously.
Listing 13-9: The Player class
Q.Player = Q.Sprite.extend({ init: function(props) { this._super(_({ sheet: 'characters', frame: 65, wait: 0, z: 10, attack: 5, health: 40, maxHealth: 40, gold: 0, xp: 0 }).extend(props)); this.add('player_input, tiled, camera, transition'); } });
The player defines a number of initial properties that won’t be used immediately, such as health, maxHealth, gold, and xp, but these will be used later in the chapter. As you can see, however, using components makes it easy to add reusable chunks of functionality to sprites without creating a deep class hierarchy.
To get a player on the screen, add the player to the stage in the level1 scene at the bottom of the file, and while you’re there, add the transition component to the stage as well, so the stage can track the player smoothly.
Q.scene('level1',new Q.Scene(function(stage) { if(Q.width > 600 || Q.height > 600) { stage.rescale(2); } stage.level = stage.insert( new Q.Level("level1.txt",stage) ); stage.add('transition'); var player = stage.insert(new Q.Player({ x: 1 * tileSize, y: 1 * tileSize })); player.camera.track(); player.bind('removed',stage,function() { Q.stageScene('level1'); }); })); Q.stageScene('level1');
With these pieces in place, you should load the rpg.html file and have the player move around the stage in response to the keyboard arrow keys or the joypad. (The joypad won’t actually be visible, but the player character will respond if you drag your finger around.)
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(14616)
The Mikado Method by Ola Ellnestam Daniel Brolund(11877)
Hello! Python by Anthony Briggs(11791)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(11240)
Dependency Injection in .NET by Mark Seemann(11001)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10519)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(9832)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(9420)
Grails in Action by Glen Smith Peter Ledbrook(9165)
Hit Refresh by Satya Nadella(9038)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(8808)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(8594)
The Kubernetes Operator Framework Book by Michael Dame(8470)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(8309)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8291)
Robo-Advisor with Python by Aki Ranin(8245)
Practical Computer Architecture with Python and ARM by Alan Clements(8218)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8189)
Building Low Latency Applications with C++ by Sourav Ghosh(8091)