Actionscript 3, What is the difference is between scale and dimension - actionscript-3

I'd like to know if I change the scale value, what happened to the object? I'm using flash air system.
I draw a movie clip box with 1000 x 1000px dimension.
I might change the size with 2 ways :
1st : Control of dimension with mc.with or mc.height
2nd : Control of dimension with mc.scaleX or mc.scaleY
Once I try to change the object with 500 x 500px,
Which one do you prefer : mc.width = mc.height = 500 vs mc.scaleX = mc.scaleY = 0.5
What is the benefit of using scale method?

Some good reading in the documentation here.
Basically they do exactly the same thing. It just depends on the case which one is easier for the developer to define in that case. If you know you need the result to be as wide as 212 pixels or the same width as object1 it makes sense to say
object2.width = 212;
or
object2.width = object1.width;
Let's assume you prefer to keep object2's dimensions proportional. You could then say
object2.scaleY = object2.scaleX;
without even knowing how many pixels that is or having another object of that same height to set it to.
The final note is this: if you change scale, dimension changes, and when you change dimension, scale also does change. In other words, setting scaleX to 1 will also set it back to its original width. Use them interchangeably. Use the one that is simpler for you in that instance.

Related

How do I scale a Libgdx Animation?

I an currently using Libgdx Animation class( com.badlogic.gdx.graphics.g2d.Animation) in a game. However I need the animation to grow in size on certain events. How do I accomplish this?
Animation is used to select the correct frame (TextureRegion) based on the time elapsed. It has nothing to do with how you render that frame. Therefor it is not different as how you render anything else.
You didn't provide enough information for that. But usually you'd render it something like this:
TextureRegion textureRegion = animation.getKeyFrame(time += delta);
spritebatch.draw(textureRegion, x, y, width, height);
So to change the size would be to change the width and height variables. You didn't provide enough information on how you'd like to do that. But you can do that for example using this:
width = 0.8f;
height = 1.9f;

How to scale objects to a certain size?

I have a question about scaling objects. I have a function that calculates the target scale of a group of objects. The scale is dependant on the amount of objects. At the same time I want the objects to have a maximum scale. I want the objects to be no bigger than 55 pixels wide. So I divide the 55 by the object width to get the maximum scale. So I came up with the following function:
private function setScale():Number
{
//The 6 is just a random number I chose.
scale = 6/amountObjects;
if (scale >= 55/averageObjectWidth)
{
scale = 55/averageObjectWidth;
}
return scale;
}
The problem I have is that when I perform this function multiple times I eventually get the wrong scale. I'll illustrate this in the following example:
I have ten objects, so the scale would be 0.6.
The average object width is 138, so the maximum scale is about 0.4.
So I would then scale the objects with this scale factor. The new width would be then 55.
Then when the amount of objects changes I run this function again. Now there are 9 objects, the new scale based on the amount of objects becomes about 0.7. Here is the pickle now, since the new object width is 55 when I do scale = 55/averageObjectWidth... I get 1! Meaning that it will skip the if statement and return me a scale of 0.7.
All I want to do is have this class return me a scale based on the amount of objects, that is still under the width of 55.
I hope I was clear enough and thank you for your time.
Would I be right in assuming that these objects are Sprites?
In which case when you set the scale/width/height of the sprite you are setting its scale. So to get the 'true'/unscaled width you could use sprite.width / sprite.scaleX (or set the scale back to 1, but you'd have to set it back again, that may cause weird graphical glitches)

How to make a simple volume dragable button which properly functions ? i have researched but cant find an effective way

I am always trying to make a simple sound player which also has volume control but i cant figure out how to make it connected with the sound volume ,
i did make a Button which can be dragged but i wanted to set its maximum x and y ,
so i did this ,
vol_player_btn.addEventListener(MouseEvent.MOUSE_MOVE,buttonInside);
protected function buttonInside(e:MouseEvent):void {
if (e.buttonDown) {
vol_player_btn.x = 480;
vol_player_btn.y = mouseY;
}
}
but then
two problems arise which tells me i am making the volume button the wrong way
and maybe i need help
The two problems are
How do i link it with sound that volume is 100,90,80 etc. (i know about sound transforms but still not on how to link it with this button)
And it can go up and and down as much as the mouse moves , Yes i know i can set it
something like this in the function and inside the if(e.buttonDown)
if (mouseY is less than the number i will guess randomly)
{then do the things}
But what i know is that this is not an efficient way and so i will be eager to hear your ways ideas about the volume functionality
Lets start with solving the second problem. You want to implement an upper and lower 'cap' so that the volume button can't go above or below a certain height. Pretty simple to do:
if (e.buttonDown) {
vol_player_btn.x = 480;
vol_player_btn.y = mouseY;
// Logic to keep button y value between a min and max value
if(vol_player_btn.y > MAX_HEIGHT)
vol_player_btn.y = MAX_HEIGHT;
if(vol_player_btn.y < MIN_HEIGHT)
vol_player_btn.y = MIN_HEIGHT;
}
To solve the first issue you want to convert the height range of the button (eg. 120 to 320) into a typical volume value (0 to 100). You can do this simply by finding the percentage:
percentage = (vol_player_btn.y - MIN_HEIGHT) / (MAX_HEIGHT - MIN_HEIGHT)
Using the above equation, if the button height is at its lowest (eg y = 120px) the percentage will equal 0. If it's at its highest (eg y = 340) the percentage will equal 100. At it's mid point (eg y = 230) the percentage will equal 50, and so on.

Flash AS3 Reduce stage height from top

I'm trying to reduce stage height from the top to get an item to spawn under a specific area. Where the HUD is.
I used to have my HUD on the bottom which caused no problem. Because my function has:
headY = Math.ceil((((stage.stageHeight - hudHeight)))/(headWidth))*Math.random())*headWidth;
This reduces stage.stageHeight with 80 so the head can spawn properly. However, this isn't the case when the HUD is at the top. What this line currently does is that it is making the head not able to spawn under stageheight - 80 pixels instead of the top.
My question is; how can I make the code reduce the stageheight from the top.
Thanks in advance,
Jordi
Extra info about my question:
All I'm trying to do actually, is 'selecting' the bottom of the stage, leaving the stage out of the picture. I was just using stageheight as an indicator to control the 800 pixels. So when I manipulate the stageheight, I wouldn't have to change the code. But since this might not be possible. There should be a different way. I hope you understand my question now. If not, what I want to do is select everything from 80 pixels down and under. The stage's height is 800 pixels so I need to select 720 pixels. However, I can't find a way to select the bottom 720 pixels.. only the top 720 pixels.
Although your question is hard to understand, the answer is most likely that you cant. The stageHeight is read-only. You can set it in HTML, and even set it to a percentage. But you can't alter it in Actionscript.
The way your question is worded, makes it extremely hard to understand what you want.
I am guessing that what you want is to spawn a given object and ensure that it is not spawned in the same space as your HUD.
So if your screen is 800 x 800 , and your HUD takes up the full width of the screen and the top 80 pixels of the screen you might try this to get a valid y spawn location :
var headY:Number = Math.random() * (800 - 80) + 80;
That would give you a resulting value from 80 - 800;
Given your variables I would suggest this :
var headY:Number = Math.random() * (stage.stageHeight - hudHeight) + hudHeight;
Now based on your code, it seems like you are also trying to ensure that whatever you are spawning is not partially off the top or bottom of the screen, so you need to factor in the height of the head. So consider this code :
var headY:Number = Math.random() * ((stage.stageHeight - headHeight) - (hudHeight + headHeight)) + (hudHeight + headHeight);
Not completely sure if this is what you are looking for, but it's the best I can do based on your question and trying to suppose what it is that you want.

Comparing two bitmaps against each other for match as3

I'm trying to position an image on top of another image based upon the make-up of the smaller image. The smaller image is a cut-out of a larger image and I need it to be positioned exactly on the larger image to make it look like a single image, but allow for separate filters and alphas to be applied. As the images are not simple rectangles or circles, but complex satellite images, I cannot simply redraw them in code. I have quite a few images and therefore do not feel like manually finding the position of each image every and hard setting them manually in actionscript. Is there any way for me to sample a small 5-10 sq. pixel area against the larger image and set the x and y values of the smaller image if a perfect match is found? All the images are in an array and iterating through them has already been set, I just need a way to sample and match pixels. My first guess was to loop the images pixel by pixel right and down, covering the whole bitmap and moving to the next child in the array once a match was found, leaving the matched child where it was when the perfect match was found.
I hope I understood your question correctly.
There may be an option that uses copypixels to achieve what you want. You can use the bitmapdata.rect value to determine the size of the sample you want, and loop through the bigger bitmap using thet rectangle and a moving point. Let's see if I can code this out...
function findBitmapInBitmap(tinyimg:BitmapData, largeimg:BitmapData):Point {
var rect:Rectangle = tinyimg.rect;
var xbound:uint = largeimg.rect.width;
var ybound:uint = largeimg.rect.height;
var imgtest:BitmapData = new BitmapData(tinyimg.rect.width, tinyimg.rect.height);
for (var ypos:uint = 0, y <= ybound, y++) {
for (var xpos:uint = 0, x <= xbound, x++) {
imgtest.copyPixels(largeimg, rect, new Point(xpos, ypos);
if (imgtest.compare(tinyimg) == 0) return new Point(xpos, ypos);
}
}
return new Point(-1,-1); // Dummy value, indicating no match.
}
Something along those lines should work - I'm sure there's room for code elegance and possible optimization. However, it seems like something like this method would be very slow, since you'd have to check each pixel for a match.
There is a better way. Split your big image into layers, and use the blitting technique to composite them at runtime. In your case, you could create a ground texture without satellites, and then create the satellites separately, and use the copyPixels method to place them whereever you want. Google "blitting in as3" to find some good tutorials. I'm currently working on a game project that uses this technique and it's a very good method.
Good luck!
Edit: Forgot to code in a default return statement. Using this method, you'd have to return an invalid point (like (-1,-1)) and check for it outside the function. Alternatively, you could just copy your small bitmap to the big one within the function, which would be much more logical, but I don't know your requirements.
You need to find pixel sequence in the big image. BitmapData.getPixel gives you pixel value. So get first pixel from small image, find it in big image, then continue comparing until you find full match. If you have trouble to code that, feel free to ask.
For the actual comparison, there's BitmapData.compare which returns the number 0 if the BitmapData objects are equivalent.