Exercise on Classes
We'll apply our knowledge of classes to a video game model in which we there are two types of characters.
We'll cover the following...
Exercise 1:
Create a PlayerCharacter
and a NonPlayerCharacter
with a common ancestor Character
. The characters are located in a 10x10 game field. All characters appear at a random location. Create the three classes, and make sure you can query where each character is.
Press + to interact
class Character {constructor( id, name, x, y ) {//Write your code here}get position() {//Write your code here}}//Define Player Character and NonPlayerCharacter classes herefunction createPlayer( id, name ) {//Write your code here}function createNonPlayer( id, name ) {//Write your code here}
Explanation:
This exercise has many solutions. We just used ...