Skip to content

hamdanbasri/SparkAR-ScoreCounter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Score Counter for Spark AR

LerpControlSample

Easily add scores in Spark AR!


Table of Contents
  1. About
  2. Version Notes
  3. Functions
  4. Setup
  5. Example
  6. Contact

About

SparkAR
Having trouble adding score to your Spark AR game?
This code is here just to do that.

Version Notes

  • This effect is created using Spark AR v147.

Functions

  • Add scores in Spark AR using script.

Setup

Visitors

  • Open Spark AR and add 3 Rectangles to the scene.
  • Name the rectangles to Wizard, Orc & Dragon.
  • Add a 2D Text to assign a name to each rectangles as their children.
  • Add a 2D Text and name it to scoreText.

  • Try to arrange the Rectangles and Text to the following image:

  • Right click in the Asset Window and select Add Asset > Script > Javascript.
  • Name the script as script.js

Code

script.js

const Scene = require('Scene');
const Patches = require('Patches');
const Diagnostics = require('Diagnostics');

// Set initial variables
const orcScore = -10;
const dragonScore = 10;
const wizardScore = 5;
let score = 0;

(async function () {  // Enables async/await in JS [part 1]

  const [scoreText] = await Promise.all([
    Scene.root.findFirst('scoreText')
  ]);  

  // monitor() and subscribe() methods allows you yo "listen" variable changes in patch editor
  Patches.outputs.getBoolean('isDragon').then(function (dragonSignal) {
    dragonSignal.monitor().subscribe(function (dragonVal) {
      if (dragonVal.newValue == true) {
        score = score + dragonScore;
        displayScore();
      }
    });
  });

  Patches.outputs.getBoolean('isWizard').then(function (alphaSignal) {
    alphaSignal.monitor().subscribe(function (alphaVal2) {
      if (alphaVal2.newValue == true) {
        score = score + wizardScore;
        displayScore();
      }
    });
  });

  Patches.outputs.getBoolean('isOrc').then(function (alphaSignal) {
    alphaSignal.monitor().subscribe(function (alphaVal3) {
      if (alphaVal3.newValue == true) {
        score = score + orcScore;
        displayScore();
      }
    });
  });

  function displayScore() {    
    scoreText.text = "" + score.toString();
    Diagnostics.log('Score: ' + score) 
  }

})(); // Enables async/await in JS [part 2]
  • Click on the script.js and add 3 boolean functions under To Script.
  • Name the boolean functions as isWizard, isOrc & isDragon.
  • Drag the Wizard, Orc & Dragon from the Scene Panel into the Patch Editor.
  • Connect the Wizard, Orc & Dragon to the Switcher Patch.
  • The Switcher Patch is a Patch Group from the following group of patches:
  • Click on the arrow button on the left of the booleans and it will automatically add to your patch.
  • Connect the patch as follows:
  • That's it, try to implement it inside your own game.

Contact

Twitter LinkedIn

(back to top)

About

Score counter for Spark AR using script.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published