AS: Main-Your Number One Resource For AS!
I dont know if this has been done before but if not, I thought it would be pretty helpful.
What?
In this tutorial you will be learning how to dynamically resize objects. You will learn the commands of _height and _width. You will also learn about _xscale and _yscale too.
Why?
Well, Im sure most of you wont really use this but this is a good way to simulate a z axis in flash. You can also use it for animaions as a shortcut to make an object get smaller or larger. So lets get started!
-----------
First I will explain the _height and _width commands. They are the easiest to work with. These are good to make a perfect circle or square. Ok, so say you want a perfect circle? Well, what you can do is draw your circle anyway you want and then convert it to a MC. After you have done that, you can give it the following code:
onClipEvent(load) {
_height=20;
_width=20;
}
The way that works is it tells flash that the circle's width should be 20 and its height should also be 20. That way, it makes the circle perfect! If you want, you can change the numbers to whatever you want. But if you want it perfect, you must keep the two numbers the same. Did I explain that well? I hope I did. I will now teach you some more about these commands.
-----------
Ok, so what if you want to change the width or height with the keyboard? This can be good for simulating a z axis. Well, first you could create a MC and give it this code:
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)) {
_width+=5;
}
}
Of course, you can change Key.RIGHT to whatever key you want. If you want to change the height and not the width, replace _width with _height. That will work good. Oh and one more thing, you can replace +=5 with -=5 if you want. The only difference is it will make the object's width get smaller and not bigger. Now I will explain how this works.
-----------
The way it works is it tells flash that if the right arrow key is pressed, it will increase the width by five pixels. Thats pretty easy, huh? Well, simulating a z axis isnt that hard either so I will now tell you how to do it.
-----------
Ok, simulating a z axis isnt to different then the previous script. But remember, this is only a simple way of doing it and its not reccomeneded for big 3D games. Okay, first create a MC that you want as the character. After you do that, you could give it this code:
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)) {
_height-=5;
_width-=5;
}
if(Key.isDown(Key.DOWN)){
_height+=5;
_width+=5;
}
}
See, that's not that hard! You can see how it's like the other code. Basically, I added that when you press a key, the height and width get changed. Thats how you make something get smaller or bigger. Remember, its very basic and its only considered to be a "fake" z axis. I will now explain it.
-----------
The way it works is it tells flash that when you press the up arrow key, the height and width get smaller. It makes it look like the charcter is walking away. When you press the down arrow key, the width and height get bigger. It makes it look as if the character is walking forward. If you want to see a little sample, clickhere.
Now, since that is the basics, you will learn about something a little bit harder. It is _xscale and _yscale. I personally think they look better when simulating a z axis but thats me. I will only be explaining them vaguely becuase they are almost the same as height and width.
-----------
Ok, _xscale and _yscale. Pretty much the same idea but different. Okay, the thing you cant really do with these is make like a perfect circle. You can however use these to flip an object. But to do that, you have to adjust the little "+" symbol in the MC so that its in the middle of the MC. Once you do that, you can give the MC this code:
onClipEvent(enterFrame){
_xscale=-100;
}
What that does is it makes the object face the other way. It saves a lot of time becuase you dont have to use the free transform tool to make it perfect. It will be perfect if you use this code. Now, if you want to make it flip upside down, you can replace _xscale wth _yscale.
-----------
Now, what if you want to make that action happen only if you use the keyboard? Well, make a MC and give it this code:
onClipEvent(enterFrame){
if(Key.isDown(Key.LEFT)){
_xscale=-100;
}
}
The way the code works is it tells flash to make the object flip only if you press the left key. That code wont make the object move becuase this tutorial doesnt teach that.
-----------
Now, Im starting to get lazy so this last part will be quick. You can use _xscale and _yscale the same way you do with _width and _height. But the only difference is, I think using _xscale looks better. What you can do is to make an objects width get bigger is give it this code:
onClipEvent(enterFrame){
_xscale+=5;
}
Remember, you can use if(Key.isDown( commands to make it happen only when you press a certain key. You can also replace _xscale with _yscale or combine them.
-----------
Hopefully I included everything relative. I also think I did a good job explaining. If the codes dont work, its becuase of the auto-line breaker. For more help, check out
AS: Basic Movement