Multiple conditions for an if statement? (code doesn't work) - actionscript-3

This is for a jigsaw-like puzzle game. I want it so it goes directly to the next scene after all the pieces are in place. So beside each "map piece" is the coordinate where it will be in its proper place.
BUT //surprise surprise// it doesn't work :c is it even possible to put so many conditions in the first place?
Thank you for reading c: (justABeginner)
if (map1.x== 259.45 &&
map1.y== 77.05 &&
map2.x== 368.3 &&
map2.y== 69.45 &&
map3.x== 445.30 &&
map3.y== 90.4 &&
map4.x== 288.5 &&
map4.y== 207.15 &&
map5.x== 325.75 &&
map5.y== 164.65 &&
map6.x== 436.20 &&
map6.y== 187.65)
{
gotoAndStop (1, "Scene 3");
}

The code looks fine to me. If the condition doesn't evaluate to true, then it might actually be the values which are different. How about a simple debugging step before the If statement like printf to check if the values are exactly the same.
Sometimes, the precision will play a large role in such If statements.
Happy coding :)

You'll probably find its not working because its particularly difficult to align an object with sub pixel accuracy. Because of this, you may want to consider "flooring" or "rounding" your numbers. I would suggest flooring to avoid it being rounded up to the next value.
if (Math.floor(map1.x) == 259 && Math.floor(map1.y) == 77 &&
Math.floor(map2.x) == 368 && Math.floor(map2.y) == 69 &&
Math.floor(map3.x) == 445 && Math.floor(map3.y) == 90 &&
Math.floor(map4.x) == 288 && Math.floor(map4.y) == 207 &&
Math.floor(map5.x) == 325 && Math.floor(map5.y) == 164 &&
Math.floor(map6.x) == 436 && Math.floor(map6.y) == 187)
{
gotoAndStop (1, "Scene 3");
}
Doing something like this will give you a more "fuzzy" comparison and should be easier to line up the puzzle pieces. Also, you may want to consider perhaps helping the user by adding a "snap to place" feature ...
function inrange(targetX, targetY, mapX, mapY, strength) {
targetX -= (strength / 2);
targetY -= (strength / 2);
return (mapX >= targetX && mapX <= (targetX+strength) &&
mapY >= targetY && mapY <= (targetY+strength));
}
//snap map piece into place if within 3 pixels
if (inrange(259, 77, map1.x, map1.y, 3)) {
map1.x = 259;
map1.y = 77;
}

As a general piece of advice, you should never use an equality check (==) with a floating point number. At some point you will find that you can't trust that 0.5 + 0.2 would equal 0.7. it may end up equalling 0.70000000001 or 0.6999999999999
As others have mentioned, try rounding your numbers, or better yet, try a small range, within which the piece is snapped into position, say plus or minus 10 pixels either way?

Related

When I deduct/some 0.05 it some/deduct 0.05000001 (the var was declared as Number)

Here is the problem:
when VX is some or deduct by 0.05 it deduct/some 0.05000001
(This take part in the Enter-frame, FPS)
**if(vx < 5 && KR){
vx += 0.05;
}else if(vx > 0 && !KR){
vx -= 0.05;
}else if(vx < 0 && !KL){
vx += 0.05;
}else if(vx < -5 && KL){
vx -= 0.05;
}
if(vy < 5 && KD){
vy += 0.05;
}else if(vy > 0 && !KD){
vy -= 0.05;
}else if(vx < -5 && KU){
vy -= 0.05;
}else if(vx < 0 && !KU){
vy += 0.05;
}
Floating point numbers are inherently imprecise - the set of rational numbers is infinite but floating point Number-s are stored on 64 bits, according to the IEEE-754 standard. Obviously, 64 bits is not an infinite number of bits so certain floating point values can be represented exactly and others cannot.
In general, you cannot rely on a floating point number to have the same exact value you're trying to assign to it.
var n:Number = 0.05;
will be translated by the compiler to a floating point representation of 0.05 and that may introduce a rounding error.
When working with floating point numbers, you usually have to use a range around the desired number, instead of checking for exact equality. So instead of doing
if ( n == 0.05 ) // this may or may not fail, based on rounding
you should do something like
if ( ( n > 0.04 ) && ( n < 0.06 ) ) // this will more likely work
The range is up to you - you must decide how much difference is acceptable when you do the check (here I used 0.01). The smaller the range, the more likely you run into rounding errors, though. The larger the range, you'll get more and more imprecise numbers that pass through the check.
Here's a pretty decent article that goes into more detail. The internet also has a lot of resources on how to properly work with floating point numbers.

How could I say "when" in ActionScript 3?

I am learning ActionScript 3 rigth now and I have this code:
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
function mouse_move(event:MouseEvent): void
{
ourMirilla.x = mouseX * 1.85;
ourMirilla.y = mouseY * 2 - 1244.25;
I would like to add something like this:
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouse_move);
function mouse_move(event:MouseEvent): void
{
ourMirilla.x = mouseX * 1.85 WHEN MOUSEx IS (>35,<55);
ourMirilla.y = mouseY * 2 - 1244.25 WHEN MOUSEy IS (>35,<55);
How could I express that in ActionScript 3?
if and when are synonymous in this situation, so:
function mouse_move(event:MouseEvent):void
{
if(mouseX > 35 && mouseX < 55)
{
ourMirilla.x = mouseX * 1.85;
}
if(mouseY > 35 && mouseY < 55)
{
ourMirilla.y = mouseY * 2 - 1244.25;
}
}
If statement
The if statement asks a question. The associated code is executed only if the answer to that question is true. To write an if statement, start with if, followed by a set of parentheses around the conditional test you want to evaluate. Then use curly brackets to denote the code block you want to run if the condition is true.
if (ourMirilla.x > 10) {ourMirilla.alpha = 0.5);}
The operators
Conditional expressions frequently use the equality and comparison operators: == , !=, < , <= , > , >= . You can also use the logical operators: && (logical AND) and || (logical OR).
See ActionScript 3 fundamentals: Operators for more information.
Your code
function mouse_move(e:MouseEvent):void
{
if (e.stageX > 35 && e.stageX < 55) {ourMirilla.x = e.stageX * 1.85;}
if (e.stageY > 35 && e.stageY < 55) {ourMirilla.y = e.stageY * 2 - 1244.25;}
}
Add an if statement inside your function like this
function ....
if (MOUSEx IS (>35,<55))
do something ...
I am not familiar with language syntax, but this is the way how to add your When statement :)
Here you can read about Conditional Statements

How to make a for loop call an If statement every Increment of 20 AS3

Hey Everyone so I was wondering How I can make my If statement for efficient rather then creating multiple if statements for the same event.
So basically I have a If statement Like so:
if (nScoreJellyBeans == 20)
{
//Add Chocoloate bunny
chocolateBunny = new mcChocolateBunny();
stage.addChild(chocolateBunny);
chocolateBunny.x = stage.stageWidth / 2;
chocolateBunny.y = (stage.stageHeight / 2) - 90;
eggBar.destroyResourceEgg();
tChocolateEggTimer.start();
tEggTimer.stop();
tJellyBeanTimer.stop();
tBlackEggTimer.stop();
//Clear all eggs off the stage
for each(egg in aEggArray)
{
egg.parent.removeChild(egg);
aEggArray = [];
//egg = null;
}
}
Now in the same Function there are multiple of these exact If statements but they change when the score is incremented by 20.
So the Next IF statement would be if (nScoreJellyBeans == 40) so on and so on.
So how could i make it instead of making all these IF statements I would just have one loop that handled all the information in the If statement every time the nScoreJellyBean is incremented by 20?
I had an Idea of creating a for loop but not too sure how I would do it.
Does anyone have any ideas on how I can accomplish this?
You Can use condition like that
if (nScoreJellyBeans % 20 == 0) //this will work for zero also
use
if (nScoreJellyBeans % 20 == 0 && nScoreJellyBeans>0) //for greater than 0 and multiple of 20

Flash AS3 square root?

I can't do this: How can I get a number of displacement (movieclip)?
Example: MovieClip.x == 0 and MovieClip.y == 0
Then I move it.
MovieClip.x == 50 and MovieClip.y == -90
Now if I make a tween, what is the number of displacement (moving)?
I'm going to take a guess and interpret the question to be something like, "How far as the MovieClip moved (or been displaced)?".
The answer would then be: 102.956 -> 103 units (pixels). You can use the Pythagorean theorem to figure this out:
xDist = 50 - 0
yDist = -90 - 0
distanceTraveled = sqrt((xDist * xDist) + (yDist * yDist))
Which would link as to why the poster asked about sqaure root.
Use Math.sqrt() to take the sqaure root of a number.

C++ if statement seems to ignore the argument

Here's the code.
bool b_div(int n_dividend)
{
for (int iii = 10 ; iii>0 ; iii--)
{
int n_remainder = n_dividend%iii;
if (n_remainder != 0)
return false;
if (iii = 1)
return true;
}
}
After testing this function I made for a program, the function seems to stop at the if (n_remainder != 0) part. Now then the function SHOULD test if the number that the function takes in can be divided by all numbers from 10 to 1.(it takes in numbers until it returns true) I know the first number that this works with it is 2520 but even on this number it stops at if(n_remainder != 0). So I was hoping for some advice! Im having trouble troubleshooting it! Any links or words I should look for would be awesome! Im still pretty new to programming so any help you can give for learning would rock! Thanks!
Change your last if statement to:
if (iii == 1)
return true;
Currently you have only a single equals sign, which sets the variable iii to 1, and is always true. By using a double equals it will compare iii and 1.
In addition to SC Ghost's answer, you can actually also clean up your function a bit more :)
bool b_div(int n_dividend) {
for (int i = 10 ; i > 1 ; i--) {
int n_remainder = n_dividend % i;
if (n_remainder != 0) {
return false;
}
}
return true;
}
A few notes,
modulus of 1 will always be zero, so you only need to iterate while i > 1
you can completely remove the if(i == 1) check and just always return true after the for loop if the for loop doesn't return false. It basically removes an unnecessary check.
I think it's more standard to name your iterator iii as i, And I prefer brackets the way I wrote them above (this is of course completely personal preference, do as you please)