This is my first AS tut so... here it goes...
Sure, this tutorial is basic, but can also help a lot of starters. This tutorial will go in depth into what _x and _y are and how they are used.
So, what are _x and _y ?
Well, _x and _y are used in Actionscript all the time. Not only are they used in Actionscript, but they are used in almost all codeing languages!
_x is a horizontal coordinate somewhere on the screen.
_y is a vertical coordinate somewhere on the screen.
But how are these used in Actionscript??
Simple. The _x and _y integers are used to find positions on the screen. You can use the trace() method to find these positions easily. For example:
Make a MovieClip and put it anywhere on the screen. Give it the instance name of "my_mc" (no quotes) and put this action on the main timeline:
trace(_root.my_mc._x);
trace(_root.my_mc._y);
This will bring up the Output box with the coordinates of _x and _y for my_mc. The top number would be the _x and the bottom number would be the _y coordinate (because that is how the trace(); was written).
You are probably still wondering how this is going to help you for AS. Well, the _x and _y can be used set the _x and _y of an MC. They can be used to move MC's. They can be used for game engines and codes.
The _x and _y integers are used in almost every game (in my opinion).
Lets say you wanted to make something move with the arrow keys. You would add a simple movement code, such as:
onClipEvent(enterFrame){
speed=5
if(Key.isDown(Key.LEFT)){
this._x-=speed
}
if(Key.isDown(Key.RIGHT)){
this._x+=speed
}
if(Key.isDown(Key.UP)){
this._y-=speed
}
if(Key.isDown(Key.DOWN)){
this._y+=speed
}
}
If you paste that onto an MC you will see that the MC can move with the arrow keys. The code used two _x integers and two _y integers.
First, the speed was defined as 5. This way, whenever the word speed comes up, speed will equal 5.
Next, some keyCodes were established (if(Key.isDown)).
Then, the _x or _y integer was set, to determine the position and direction of which way the MovieClip would move.
So that is an important example of how _x and _y integers are used.
Examples? I'd like to see these "integers" in action
But of course! Here is a simple engine I made for finding _x and _y positions of my MC.
Clicky
Use the arrow keys to control the square, use the button "remember" to remember the _x and _y values of the square,
and "go" to go to the position that it last remembered!
That's cool, how does it work?
Well, I started by giving the square MC the same movement script I posted before. After I had that in, I made two Dynamic textboxes. The top one given the var name "xpos" and the bottom one given the var name "ypos".
With my square MC given the instance name "sq", I put this code onto the main timeline.
onEnterFrame=function(){
_root.xpos=_root.sq._x
_root.ypos=_root.sq._y
}
That simply tells the textboxes to equal the values of my square MC's _x and _y positions.
Now the buttons. The remember button was simple to make, as well as the go button.
On the remember button I put these actions onto it:
on (release) {
_root.newX = _root.sq._x;
_root.newY = _root.sq._y;
}
This makes it so that when the button is released, it defines 2 new variables (newX and newY) that are equal to the position that _root.sq is when the button is clicked, for the _x and the _y.
As for the go button, I used these actions:
on (release) {
_root.sq._x= _root.newX
_root.sq._y= _root.newY
}
This makes it so that when the go button is released, the square MC (_root.sq) will equal what _root.newX and _root.newY were last defined as.
And that is it to my whole engine! Simple, yet affective.
So as you can see, the _x and _y integers can be used in many different things! If used correctly, they can make your game worthwhile and good. Sure, this is not much, but if you are looking for basics on game making, there you go.
And one more time for... AS: Main
Hopefully you learned a lot from this, sorry if it's not great, this is my first AS tutorial, but I hope you got the point across, and how to use _x and _y. =D
No animals were harmed in the making of this tutorial