Why is a global variable (inside function) not identified? - javascript

So, I was trying to initialize variables and put a few global variables in my Unity script, but when I run the code, it says that x, y, and z are unknown identifiers. I've been trying to find the answer to this:
function Awake () {
x = 0; //Unity doesn't work with commas
y = 0;
z = 0;
}
function Update () {
if (Input.GetTouch) {
x = x-1;
}
Transform.position = Vector3(x,y,z);
}
The thing with putting the variables outside the function is that they will repeat and act as update.
I'm also new to Unity, and JavaScript.

It looks to me (granted with my limited exposure to unity) like you need to declare your variables at a global level. Try doing something like this:
// declare these variables in the global scope.
var x;
var y;
var z;
function Awake () {
x = 0;
y = 0;
z = 0;
}
function Update () {
if (Input.GetTouch) {
x = x-1;
}
Transform.position = Vector3(x,y,z);
}

Related

passing variables across functions on javascript

I'm trying to pass variables across functions,
something like
function one() {
var x = 1
}
function two() {
var y = x + 1;
}
alert(y);
Is there a way to do it?
edit:Thanks everyone for being so helpful, but maybe I should have been more specific with my question.
You current way has x and y in the scope of the function, which means the other function doesnt know it exists. Also, its good practice to name functions according to what they do. 3 straightforward ways to do this.
Global
Params
Inline
Set two variables outside of the functions scope that any function can reach.
var x, y;
function assignOne() {
x = 1;
}
function addOne() {
y = x + 1;
}
assignOne();
addOne();
console.log(y);
Pass in a parameter to to the function and return values.
function one() {
return 1;
}
function addOneTo(x) {
return x + 1;
}
const y = addOneTo(one());
console.log(y);
Perform functions inline
var x = null;
function one() {
x = 1;
}
function two() {
return x + 1;
}
one();
const y = two();
console.log(y);
You will need to hoist the scope, by extracting the variable declaration to outside of the functions. That is to say, define x and y outside of the functions. Note that you can still update their values from within the functions. However, don't forget that you'll actually need to invoke both functions as well!
This can be seen in the following:
var x, y;
function one() {
x = 1;
}
function two() {
y = x + 1;
}
one();
two();
console.log(y);
If you really want to get variable declared in one method, return it
function one(){
var x = 1;
return x;
}
function two() {
var x = one();
var y = x + 1;
return y;
}
alert(two());
Seems like you want to have shared state between both functions instead of passing arguments. So, an object oriented pattern seems to be appropriate.
class Thing {
constructor() {
this.x = 1;
}
one() {
return this.x;
}
two() {
return this.x + 1;
}
}
const t = new Thing();
console.log(t.one());
console.log(t.two());
If you want to share variables between functions but don't want to declare them in global scope, you can use a closure like this:
(function() {
var x, y;
function one() {
var x = 1
}
function two() {
var y = x + 1;
}
one();
two();
alert(y);
})();

GreenSock, tween function arguments

I got two functions called on mouse events:
function menuBtnOver(e){
var b = e.data;
b.setPosition(b.x, b.y+5);
}
function menuBtnOut(e){
var b = e.data;
b.setPosition(b.x, b.y-5);
}
and:
setPosition:function(x, y) {
if(!x) x = 0;
if(!y) y = 0;
this.element.css("left", x);
this.element.css("top", y);
}
element property is a jQuery object.
It is working ok but i want to animate this. How can i do this with TweenLite?
I've tried following code:
function menuBtnOver(e){
TweenLite.to(e.data, 1, {top:500});
}
As well as this:
function menuBtnOver(e){
TweenLite.to(e.data.getElement(), 1, {top:500});
}
and many other combinations but none of them worked.
Only on method which partially work is this:
function menuBtnOver(e){
TweenLite.to(e.data, 1, {y:400, onUpdate:e.data.setPosition, onUpdateParams:[e.data.x, e.data.y]});
}
But it work only on fist button when I roll over and (after any time) roll out, it moves directly to given position (without tween) and then the tween goes forever giving me error each time(at least - I couldn't get any errors or anything with other attempts).
Uncaught TypeError: Cannot read property 'css' of undefined
at: this.element.css("left", x);
Update
I figured out what is going on.
I've changed code as so:
function menuBtnOver(e){
TweenLite.to(e.data, 1, {y:400, onUpdate:e.data.setPosition, onUpdateParams:[e.data.x, e.data.y], onUpdateScope:e.data});
}
But the problem with this is that arguments to update function which I set to e.data.y/x aren't dynamic references and always stay as those exact values from menuBtnOver state. So the tween works if i change setPosition function to:
setPosition:function(x, y) {
if(!x) x = 0;
if(!y) y = 0;
this.element.css("left", this.x);
this.element.css("top", this.y);
}
But obviously this is not what I want to do.
So I have option to make something like this:
MenuButton.prototype = {
setPosition:function(x, y) {
if(!x) x = 0;
if(!y) y = 0;
this.x = x; this.y = y;
this.element.css("left", x);
this.element.css("top", y);
},
updatePosition:function(){
this.element.css("left", this.x);
this.element.css("top", this.y);
}
}
function menuBtnOver(e){
TweenLite.to(e.data, 1, {y:400, onUpdate:e.data.updatePosition, onUpdateScope:e.data});
}
Or define external update function in similar manner. The question still stays the same so is there a simple way to do this simpler. Does GS tween has any mechanic which automate this process?
Thanks to everyone for attention :)
this in setPosition is referring to that function and not the this of the onClick event.
you need to do pass this to setPosition. As in the example below, where I passed it as self in the function setPosition.
function menuBtnOver(e){
var b = e.data;
b.setPosition(this, b.x, b.y+5);
}
function menuBtnOut(e){
var b = e.data;
b.setPosition(this, b.x, b.y-5);
}
and:
setPosition:function(self, x, y) {
if(!x) x = 0;
if(!y) y = 0;
self.element.css("left", x);
self.element.css("top", y);
}
this always refernces the function in which is was called. as you can read about her. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
So you can pass this in a function as a variable.

Global variable and addEventlistener

Here is the problem: I would like to have global variable X which takes value of alpha. With code below, value for X in console is always zero.
var X = 0;
window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
var alpha = Math.round(event.alpha);
X = alpha;
}
console.log(X);
EDIT:
I wanted to do something like
var X = 0;
window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
var alpha = Math.round(event.alpha);
X = alpha;
}
function f(x){ return x*x }
f(X);
Now I understand that I have to put f(X) within function handleOrientation. I guess there is not other way around?
You're using the value of X before it changes. Here's the order in which your code runs:
Create a function called handleOrientation. (Why first? Because creating the functions described by function declarations is one of the very first things done upon entry to a scope.)
Declare a variable called X. (First not only because it's at the top, but because creating variables declared with var is one of the very first things done upon entry to a scope, soon after processing function declarations.)
Assign 0 to X. (This is the first step-by-step code that runs.)
Call addEventListener on window.
Output X to the console.
(If and when the deviceorientation event is triggered on window) Update the value of X.
Instead, use the value after it changes:
var X = 0;
window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
var alpha = Math.round(event.alpha);
X = alpha;
console.log(X);
}
Or:
var X = 0;
window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
var alpha = Math.round(event.alpha);
X = alpha;
doSomethingWithX();
}
function doSomethingWithX() {
console.log(X);
}

How can I pass a variable from function x to function y and a varible from function y to functio x without run the whole function

How can I pass a variable from function x to function y and a varible from function y to function x without run the whole function. For Example below I want to sent the z variable from function two to function one and the y variable from function one to function two but without run the function two, just send the variable value.
function one(x) {
var y;
y = x+1;
}
function two(z) {
z = 9;
var k = y +9;
one(z)
}
Is a way to do that? Or it is not achievable with this way?
This is what you have:
function one(x) {
var y;
y = x+1;
}
function two(z) {
z = 9;
var k = y +9;
one(z)
}
From what I can understand you need to do this:
var z = 9;
function one(x) {
var y;
y = x+1;
}
function two() {
z = 9;
var k = y +9;
one()
}
However, it is extremely difficult to understand what you want to achieve. Can you give a scenario as to where you will use this kind of code?

Scope of variables in functions using javascript

Why the y is leaked outside the function as scope in JavaScript are bound to functions.
A detailed explanation would be fruitful.
var x = 0;
function f(){
var x = y = 1; // x is declared locally. y is not!
};
f();
console.log(x, y); // 0, 1
It's just syntax error.
The line var x = y = 1; Means:
Declare local variable x,
y = 1,
Declare global variable y (since it's not declared)
x = y;
Replace
var x = y = 1;
with
var x = 1, y = 1;
or
var x = 1;
var y = 1;
and you will get local variable y
http://www.w3schools.com/js/js_variables.asp
The reason the y variable is global is that in Javascript if you omit the var keyword from an assignment statement, the variable the value is assigned to is declared on the global object.
That means that if you wrote the f() function this way, declaring both x and y with the var keyword:
function f(){
var x, y;
x = y = 1;
};
Then both x and y would be local variables (local x shadowing the global one).
It's bad practice to assign a variable without declaring it (with the var keyword).
New versions of JS will throw an error on this. You can use EC5's strict mode to take advantage of this behavior:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode
so you'd write your function this way:
function f(){
'use strict'
var x, y;
x = y = 1;
};
now if you forget to declare y you get an error yelling at you to do so.

Categories

Resources