Good Day Everyone, I am working on a script and it works really well, but there is just this one error that i just cannot figure out. What am i overlooking? I will post the error and code below.
Here is the error:
UnassignedReferenceException: The variable gameOverScore of Score has not been assigned.
You probably need to assign the gameOverScore variable of the Score script in the inspector.
Score.Start () (at Assets/2dspaceshooter/Scripts/Score.js:10)
Here is the script:
#pragma strict
var gameOverScore:GUIText;
var gameGUI:GameObject;
private var score:int = 0;
private var isGameOver = false;
function Start () {
gameOverScore.guiText.enabled = false;
guiText.text = "Score: " + score.ToString();
}
function addScore () {
if(!isGameOver){
score += 10;
guiText.text = "Score: " + score.ToString();
}
}
function doGameOver () {
isGameOver = true;
gameGUI.SetActive(false);
guiText.text = null;
gameOverScore.guiText.enabled = true;
gameOverScore.guiText.text = "Score: "+score;
}
Not that I really recognize your script as simple JavaScript, but it may be that you confuse an object Score with the lower case version score
Related
I want to remove the listener for individual objects that are animating. I want to remove the ticker for individual objects because they will stop at different times when they reach 200px in y. This code is one frame in Adobe Animate. So this code is not working:
this.stop();
that= this;
var aParticle;
var mySpeed = 12;
var myRotation = 4;
var totalParticles = 5;
var stopParticles = false;
var particleHolder = new createjs.Container();
var count = 0;
var collission_ar = [this.parent.mc_coll0, this.parent.mc_coll1, `this.parent.mc_coll2, this.parent.mc_coll3, this.parent.mc_coll4, this.parent.mc_coll5, this.parent.mc_coll6, this.parent.mc_coll7, this.parent.mc_coll8, this.parent.mc_coll9, this.parent.mc_coll10, this.parent.mc_coll11, this.parent.mc_coll12, this.parent.mc_coll13, this.parent.mc_coll14];`
var totalCollisions = collission_ar.length;
this.addChild(particleHolder);
//stage.update();
var xRange = width;
var yRange = height;
var scaleNum = 1;
//var collisionMethod = ndgmr.checkPixelCollision;
this.scaleX = 1;
this.scaleY = 1;
createParticles()
setTimeout(function(){
removeTimer();
}, 5000)
function createParticles(){
var particle_ar = [];
var randNum = Math.ceil(Math.random() * totalParticles);
aParticle = new lib['MC_leaf'+randNum]();
aParticle.name = 'MC_leaf'+count;
aParticle.x = Math.random() * xRange;
aParticle.y = -Math.random() * 15;
theNum = Math.random() * scaleNum;
aParticle.scaleX = theNum
aParticle.scaleY = theNum
aParticle.alpha = 1;
aParticle.collision = Math.floor(Math.random() * 2);
particleHolder.addChild(aParticle);
aParticle.addEventListener("tick", animateParticle.bind(that));
if(!stopParticles){
timer = setTimeout(function() { createParticles() }, 100);
}
count++;
}
function animateParticle (event){
var part = event.currentTarget;
event.currentTarget.y += mySpeed
event.currentTarget.x += Math.random()/10
event.currentTarget.rotation += myRotation;
if (part.y > 200) {
if(part.name == 'MC_leaf0') console.log('part0 y '+part.y);
part.removeEventListener("tick", animateParticle.bind(that));
}
}
function removeTimer() {
stopParticles = true;
timer = clearInterval();
}
var timer = setTimeout(function() { createParticles() }, 100, that);
So this code is just ignored:
part.removeEventListener("tick", animateParticle.bind(that));
You must pass a reference to the same method in removeEventListener that you used with addEventListener. When you use bind, it generates a wrapper function each time.
// This won't work.
part.removeEventListener("tick", animateParticle.bind(that));
A simple workaround is to store a reference to the bound function, and use that.
aParticle.tickHandler = animateParticle.bind(that);
aParticle.addEventListener("tick", aParticle.tickHandler);
Then use it when removing the listener
part.removeEventListener("tick", part.tickHandler);
There is a better way to handle this though. If you use the utility on() method instead of addEventListener, you can easily remove the method inside the handler.
aParticle.on("tick", animateParticle, that);
// Then when removing:
function animateParticle(event) {
if (conditions) {
event.remove();
}
}
The on() method also has a scope parameter, so you can skip the function binding. It is important to note though that the on() method does its own internal binding, so to remove a listener the usual way, you have to store a reference to it as well.
var listener = target.on("tick", handler, this);
listener.off("tick", listener);
Hope that helps!
So now that Ti.include is deprecated, I am forced to face the fact that I do not know the correct way to handle commonJS and require.
I have read and re-read many of the posts on SO and elsewhere but still cannot make sense of the syntax. I suspect it is because my original code is somewhat hackish to begin with.
Can anybody help by looking at the small code below and helping me translate it into commonJS?
In the document that contains the current Ti.Include I need to get to the variables dp and ff.
var dp = "";
if (Titanium.Platform.Android) {
dp = (Ti.Platform.displayCaps.dpi / 160);
} else {
dp = 1;
}
var version = Titanium.Platform.version.split(".");
version = version[0];
if (Titanium.Platform.displayCaps.platformWidth == '320') {
ff = 0;
} else if (Titanium.Platform.displayCaps.platformWidth == '768') {
ff = 3;
} else {
ff = 1;
}
Thank you all.
something like this will do.
Create a file named dpAndFfModule.js and place under the lib folder inside your project.
exports.getDp = function () {
var dp = "";
if (Titanium.Platform.Android) {
dp = (Ti.Platform.displayCaps.dpi / 160);
} else {
dp = 1;
}
return dp;
};
exports.getFf = function () {
var version = Titanium.Platform.version.split(".");
version = version[0];
var ff;
if (Titanium.Platform.displayCaps.platformWidth == '320') {
ff = 0;
} else if (Titanium.Platform.displayCaps.platformWidth == '768') {
ff = 3;
} else {
ff = 1;
}
return ff;
};
now inside .js files you need to require the module like this:
var dpAndFf = require('dpAndFfModule'); //you pass the filename (without extention) to require funciton.
dpAndFf.getDp(); // this executes the getDp function and returns your dp.
dpAndFf.getFf(); // same, executes getFf function and returns the result.
I have the below code which show be randomly true and randomly false. But in my case its always being false. Any help is appreciated. Thanks.
In the below code tail and heads are the tow buttons.
on(release)
{
var guess:Boolean = Boolean(Math.round(Math.random()));
var input:Boolean;
if (event.target.name == "tail"){
input = true;
}
else if (event.target.name == "heads"){
input = false;
}
if (guess == input){
var newresult = Number(income.text) + Number(amount.text);
income.text = Number(newresult);
}
else{
var newresult = Number(income.text) - Number(amount.text);
income.text = Number(newresult);
}
}
This will also work:
on(release)
{
var guess:Boolean = Boolean(Math.floor(Math.random()*2));
if (guess){
result.text = "Your Guess is corrent";
var newresult = Number(income.text) + Number(amount.text);
income.text = Number(newresult);
}
else{
result.text = "Your Guess is wrong";
var newresult = Number(income.text) - Number(amount.text);
income.text = Number(newresult);
}
}
You dont need the event.target.name because the function is within the event handler of each button. So you can just use a random boolean. Event.target.name also doesnt work in AS2.0
I am writing a method to grab the lowest unique id in an ItemFileWriteStore. I have a boolean to tell me when I reach my condition, but I can't get the scope correct.
The function call works as I expect, apart from when newIdOkay is set to true, it is not recognised in the while loop.
Please can you explain why and what I have to do to get this right?
Many thanks
Here is my code:
function checkNewId( size ) {
if( size == 0 ) {
console.log('found new ID!');
newIdOkay = true;
}
}
function addContentItem( store ) {
// New ID
var newIdOkay = false;
var newId = 0;
while( newIdOkay == false && newId < 8 ) {
newId++;
store.fetch({ query: {id:newId}, onBegin: dojo.hitch(this, "checkNewId"),
start:0, count:0, sync:true });
}
}
To understand the reason, you need to have basic understanding of identifier resolution and scope chain in JavaScript. I recommend you to read this article: http://jibbering.com/faq/notes/closures/
Regarding you question, in the function addContentItem, the check in the while loop is tested against the identifier newIdOkay which is in the of the activation object corresponding to this function execution context. In the function checkNewId, the identifier newIdOkay you set is in the global object. So these two are not the same one. An easy fix is to move the newIdOkay in addContentItem function to the global scope.
var newIdOkay = false;
function checkNewId( size ) {
if( size == 0 ) {
console.log('found new ID!');
newIdOkay = true;
}
}
function addContentItem( store ) {
// New ID
var newId = 0;
while( newIdOkay == false && newId < 8 ) {
newId++;
store.fetch({ query: {id:newId}, onBegin: dojo.hitch(this, "checkNewId"),
start:0, count:0, sync:true });
}
}
I'm wondering about JScript.NET private variables. Please take a look on the following code:
import System;
import System.Windows.Forms;
import System.Drawing;
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
}
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
}
var addHeader = function(){
out('header');
}
return {
endDocument: function(){
state = 1;
addHeader();
out('endDocument');
},
beginDocument: function(){
beginPage();
}
}
}
var j = new jsPDF();
j.beginDocument();
j.endDocument();
Output:
beginPage 2
header 2
endDocument 2
if I run the same script in any browser, the output is:
beginPage 2
header 1
endDocument 1
Why it is so??
Thanks,
Paul.
Just a guess, but it appears that JScript.NET doesn't support closures the same way as EMCAScript, so the state variable in endDocument() isn't referencing the private member of the outer function, but rather an local variable (undeclared). Odd.
You don't have to use new when calling jsPDF here since you're using a singleton pattern. jsPDF is returning an object literal so even without new you'll have access to the beginPage and endDocument methods. To be perfectly honest I don't know what the specifications call for when using new on a function that returns an object literal so I'm not sure if JScript.NET is getting it wrong or the browser. But for now try either getting rid of the new before jsPDF() or change your function to this:
var jsPDF = function(){
var state = 0;
var beginPage = function(){
state = 2;
out('beginPage');
};
var out = function(text){
if(state == 2){
var st = 3;
}
MessageBox.Show(text + ' ' + state);
};
var addHeader = function(){
out('header');
};
this.endDocument = function(){
state = 1;
addHeader();
out('endDocument');
};
this.beginDocument: function(){
beginPage();
};
}
That will allow you to use the new keyword and create more than one jsPDF object.
I've come across the same problem. In the following code, the closure bound to fun should contain only one variable called result. As the code stands, the variable result in the function with one parameter seems to be different to the result variable in the closure.
If in this function the line
result = [];
is removed, then the result in the line
return result;
refers to the result in the closure.
var fun = function() {
var result = [];
// recursive descent, collects property names of obj
// dummy parameter does nothing
var funAux = function(obj, pathToObj, dummy) {
if (typeof obj === "object") {
for (var propName in obj) {
if (obj.hasOwnProperty(propName)) {
funAux(obj[propName], pathToObj.concat(propName), dummy);
}
}
}
else {
// at leaf property, save path to leaf
result.push(pathToObj);
}
}
return function(obj) {
// remove line below and `result' 3 lines below is `result' in closure
result = []; // does not appear to be bound to `result' above
funAux(obj, [], "dummy");
return result; // if result 2 lines above is set, result is closure is a different variable
};
}();