How could I say "when" in ActionScript 3? - 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

Related

Interpolate function in Google Scripts

I am using google sheets to log my water usage and want to find out how much I have used in a given period by using linear interpolation. I would really like to use a function similar to forecast, but instead of using the entire range to interpolate, just use the nearest points above and below.
I am keen to try and code it myself (have done lots of VBA) but don't know really where to start with google scripts. Does anyone have a starting point for me?
The process I would take is:
Interpolate(x, data_y, data_x)
// Check value is within range of known values (could expand function to use closet two values and extrapolate...)
(is X within XMin and XMax)
// Find closet X value below (X1), corresponding Y1
// Find closet X value above (X2), corresponding Y2
Return Y = Y1+(X-X1)*((Y2-Y1)/(X2-X1))
function interpolation(x_range, y_range, x_value) {
var xValue, yValue, xDiff, yDiff, xInt, index, check = 0;
if(x_value > Math.max.apply(Math, x_range) || x_value < Math.min.apply(Math, x_range)) {
throw "value can't be interpolated !!";
return;
}
for(var i = 0, iLen = x_range.length; i < iLen-1; i++) {
if((x_range[i][0] <= x_value && x_range[i+1][0]> x_value) || (x_range[i][0] >= x_value && x_range[i+1][0] < x_value)){
yValue = y_range[i][0];
xDiff = x_range[i+1][0] - x_range[i][0];
yDiff = y_range[i+1][0] - yValue;
xInt = x_value - x_range[i][0];
return (xInt * (yDiff / xDiff)) + yValue;
}
}
return y_range[x_range.length-1][0];
}

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

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?

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 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

Preventing object from getting stuck on walls and rapidly bouncing between two points

I currently have objects populating the screen randomly and bouncing around the stage. The problem is, some of the objects are getting stuck and bouncing back and forth rapidly between two points. How can I prevent this?
Here's how I have the EnterFrame setup:
var ballT = e.target
ballT.x += ballT.vx;
ballT.y += ballT.vy;
if (ballT.x + ballT.width / 2 >= sWidth || ballT.x - ballT.width / 2 <= 0) {
ballT.vx = - ballT.vx;
} else if (ballT.y + ballT.height / 2 >= sHeight || ballT.y - ballT.height / 2 <= 0) {
ballT.vy = - ballT.vy;
}
Any ideas or any good reads worth checking out?
Most likely, your objects are going a bit "too much out of bounds", so that when you turn them around, they don't make it inside the bounds before you turn them around again. You may want to change the code to be a bit more clear on when to turn things around;
var ballT = e.target
ballT.x += ballT.vx;
ballT.y += ballT.vy;
// Outside to the right and heading right - turn around
if ( ballT.x + ballT.width / 2 >= sWidth && ballT.vx > 0 )
ballT.vx = -ballT.vx;
// Outside to the left and heading left - turn around
if ( ballT.x - ballT.width / 2 <= 0 && ballT.vx < 0 )
ballT.vx = -ballT.vx;
// Outside at the bottom and heading down - turn around
if ( ballT.y + ballT.height / 2 >= sHeight && ballT.vy > 0 )
ballT.vy = -ballT.vy;
// Outside at the top and heading up - turn around
if ( ballT.y - ballT.height / 2 <= 0 && ballT.vy < 0 )
ballT.vy = -ballT.vy;