I'm having some trouble with the error "1050: Cannot assign to a non-reference value." I'm still fairly new to coding, and so being unable to fix this error is frustrating, any help will be greatly appreciated.
var PracticeDummyHealth:int=50
var PlayerAttack:int=20;
public function PlayerAttackFunction(){
if(PracticeDummyHealth>0){
PracticeDummyHealth-PlayerAttack=PracticeDummyHealth;
}
}
An grammar construct which is not a Property/Variable name is on the left of the = assignment operator:
// expression = expression
PracticeDummyHealth-PlayerAttack=PracticeDummyHealth;
// which makes as much sense to ActionScript as .. it's not an equation solver :)
// 100 - 50 = 100
Compare with this valid code:
// variable = new_value
PracticeDummyHealth = PracticeDummyHealth - PlayerAttack;
// or
PracticeDummyHealth -= PlayerAttack;
Note that a "reference" (read: Property/Variable name) appears on the left of the = (or compound -=) in both of these cases. This terminology comes from the specification which deals with l-values and it is slightly unfortunate it doesn't yield a nicer error message here.
Related
Sorry ahead of time for french in my code or badly translated errors (idk what they are in english so Google translate). I'm working on a program at school to add all numbers, all even numbers or all odd numbers (different buttons) from an array (seperate file, called U2A2_Elements.as) and I'm getting multiple errors, I'm getting :
1061: Call for indexOf method might not be defined via the static int type reference at entier = (entier.indexOf(entierSaisi));
1119: Access to the length property can not be defined via the reference type static int" at for (var i=entier; i entier.length; i++).
1061: Call for pop method might not be defined via the reference type static int" at entier.pop();.
Any help would be greatly appreciated as I have no idea what to do with the code nor does the teacher or anyone else.
EDIT: Forgot to put the link to the code http://pastebin.com/5nyf3z7g
In your supprimerFunction() function, you forgot that your array is mesEntiers ( and not entier which is an int object ), so I think that you should write :
function supprimerFunction(event:MouseEvent):void {
var entierSaisi:String;
var entier:int;
entierSaisi = (txtEntier.text);
entier = int(entierSaisi);
entier = mesEntiers.indexOf(entier);
if (entier != -1) {
for (var i = entier; i < mesEntiers.length; i++) {
entier[i] = entier[i + 1];
}
mesEntiers.pop();
}
}
Of course, I try just to remove errors mentioned in your question, and not improve your function.
Hope that can help.
Keep in mind, this is part of a homework assignment - so please, no direct answer. I just need some help in finding out the answer, so a link to a tutorial to help me understand the material would be great.
SML code:
datatype 'ingredient pizza =
Bottom
| Topping of ('ingredient * ('ingredient pizza));
datatype fish =
Anchovy
| Shark
| Tuna;
(* Testing Pizza Objects *)
val my_pizza1 = Topping(Tuna, Topping(Shark, Topping(Anchovy, Bottom)));
val my_pizza2 = Topping(Shark, Topping(Tuna, Topping(Anchovy, Bottom)));
val my_pizza3 = Topping(Anchovy, Topping(Shark, Topping(Tuna, Bottom)));
(* My Function Start *)
fun rem_ingredient Bottom = Bottom
| rem_ingredient(t) = fn(Topping(p)) => Topping(t, rem_ingredient(p))
| rem_ingredient(Topping(t,p)) = Topping(t, rem_ingredient(p));
(* My Function End *)
If I call the function rem_ingredient with 1 parameter
val rem_tuna = rem_ingredient Tuna;"
I should get a function that can then call
rem_tuna my_pizza3;
to remove Tuna from pizza3
If I call the same function with 2 parameters
rem_ingredient Tuna my_pizza2;
I should directly remove Tuna from the pizza2 object with the 2 parameters.
The Problem:
I keep getting the error: syntax error: replacing EQUALOP with DARROW on the 3rd constructor of rem_ingredient, I know I'm missing something that is probably obvious. We just start learning SML last week in Programming Languages and I'm still trying to understand it. Anyone pointing me in direction would be appreciated.
Again, no direct answer please, I want to learn the material, but I'm not sure what I'm trying to fix.
To get rid of the syntax error you need to put parentheses around the fn expression (since the following | pattern is otherwise taken to be part of the fn).
However, that is not your real problem here. The function as written does not have a consistent type, because the second case returns a function while the others do not.
As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points
I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.
if i don't specifically type a variable in my code will it compile as a default datatype? for example, the "for each ... in" function works best without typing the variable:
for each (var element in myArray)
{
//process each element
}
does my element variable have a datatype? if it is typed as Object is it better to actually write element:Object or does it matter?
EDIT
actually, that was a bad example, since the element variable is going to be typed to whatever element is in myArray.
but is that how it works if a variable is untyped? it will simply become what is passed to it?
here's a better example for my question:
var a = "i'm a string"; //does this var becomes a String?
var b = 50.98; //does this var becomes a Number?
var c = 2; //does this var becomes an int?
for each (var element in myArray)
The element variable doesn't have any data type - it is untyped and thus can hold anything.
And yeah, it is equivalent to writing element:Object or element:*, but it is always advisable to type your variables - this will help you catch some errors before you run the code. The mxmlc compiler will emit a warning if you don't do this, which can be fixed by typing it as e:Object or e:*.
var a = 45.3; //untyped variable `a`
a = "asd"; //can hold anything
/*
This is fine: currently variable `a` contains
a String object, which does have a `charAt` function.
*/
trace(a.charAt(1));
a = 23;
/*
Run time error: currently variable `a` contains
a Number, which doesn't have a `charAt` function.
If you had specified the type of variable `a` when
you declared it, this would have been
detected at the time of compilation itself.
*/
trace(a.charAt(1)); //run time error
var b:Number = 45.3;
b = "asd"; //compiler error
trace(a.charAt(1)); //compiler error