Game Tutorial OOP 5

Objective

  • Update Enemy class and Player class to handle the collusion.

Code

**View source code: game-tutorial-oop5.js

I will only explain the code that is new or updated from the previous tutorial.

Player.prototype._calculate = function(){
  var enemy = this.data.refMainObjects.enemy;
  var frame = this.data.refMainObjects.frame;
  this.move();
  this.processCollusionToEnemy(enemy,frame);
};

processCollusionToEnemy function call is added after player move.

Player.prototype.processCollusionToEnemy = function(enemy,frame){
  if(this.data.y==enemy.data.y && this.data.x<=enemy.data.x && this.data.x+this.data.text.length>=enemy.data.x){
    frame.addScore();
    enemy.init();
  }
};

This is processCollusionToEnemy function.

Enemy.prototype._calculate = function(){
  var frame = this.data.refMainObjects.frame;
  var player = this.data.refMainObjects.player;
  this.move();
  if(this.checkHitFrame(frame)){
    this.init();
  }
  player.processCollusionToEnemy(this,frame);
};

player's processCollusionToEnemy function call is added after enemy move.

Run Example

Click Screen first, then press arrow keys in your keyboard to move player.

Type these commands into the browser console on this page to test.

main.inactive() //inactive main and main.objects(frame, enemy)
main.init() //re-activate main and main.object(frame, enemy)
main.objects.player.inactivate() //inactivate player.
main.objects.player.init() //re-activate player.
main.objects.enemy.inactivate() //inactivates enemy.
main.objects.enemy.init() //re-activates enemy.
main.objects.frame.inactivate() //inactivates frame.
main.objects.frame.init() //re-activates frame.

Summary

We coded a simple program in 2 different ways - 1. coding with only Text Game Maker JS managers(TM.ScreenManager, TM.InputManager and TM.DebugManager) and 2. coding in OOP with managers + TM.IObject, TM.ILoopObject and TM.IProgram.

The 2nd way looks more complicated since this program is so simple, however, it would help you a lot if you make complex programs.

Anyways I hope you enjoy this library and share with me when you made something! Thanks.

Comments

Add Comment

Login with SNS account to write comments. see details

UP