The issue at hand is my struggle to extract a value from a nested method and utilize it outside of its parent method. I am aiming for the output of "console.log(someObjects[i].valueChecker);" to display either "true" or "false," but instead, it simply returns the function itself.
My attempts thus far have led me to scouring the internet and Stack Overflow in search of a resolution. However, I have been unsuccessful in finding a suitable solution or deciphering the ones I have come across. It seems that the concept of "closures" might play a role here, as most recommendations involve returning from the submethod and subsequently returning the submethod from the parent method. Unfortunately, each time I have tried implementing these suggestions, I encountered numerous errors - whether it be an undefined submethod or the continued output of a function. The presence of multiple methods might be complicating matters further.
In the realm of context, I am currently in the process of developing a platformer game that features various iterations of the same enemy type. My objective is to conduct collision checks between the player character and the enemy's weapon, necessitating the extraction of certain values from the enemy function (I refrain from using the term "class", although I am unsure if this is the correct terminology). Being more well-versed in Java, the inability to easily create a separate class with a method for retrieving values is proving to be a source of frustration.
//assume all the other html/main stuff is already set up
var temp = {
create: c4,
update: u4
}
MyObject = function(value) {
this.value = value; //passed in value
var magicNumber = 4; //local value initialized/declared
this.valueChecker = function() {
//return boolean
return this.value == this.magicNumber;
}
this.otherValueChecker = function() {
//return boolean
return (this.value + 1) == this.magicNumber;
}
}
//just make the space bar tied to a boolean
var someKeyPress;
function c4() {
someKeyPress = game.input.keyboard.addKey(Phaser.Keyboard.A);
}
var someObjects = [];
//... later on in the program, presuming key already coded
function u4() {
//add a new MyObject to array someObjects
if (someKeyPress.isDown) {
//check with various random numbers between 3 to 5
someObjects.push(new MyObject(game.rnd.integerInRange(3, 5)));
}
//run through MyObject(s) added to someObjects, and see if any match number
for (var i = 0; i < someObjects.length; i++) {
console.log(someObjects[i].valueChecker);
}
}
/* current output
ƒ () {
//return boolean
return this.value == this.magicNumber;
}
*/