## AS: _x and _y

• 5,477 Views
• 20 Replies
TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
AS: _x and _y 2006-01-23 18:07:07

AS: Main

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
Rantzien
Rantzien
• Member since: Jan. 27, 2005
• Offline.
Forum Stats
Member
Level 15
Blank Slate
Response to AS: _x and _y 2006-01-23 18:10:33

It might just be two properties, but I've lost count (like I kept count) of the numbers of times I have had to explain _x and _y to people. Very useful, nice job =)

Siggy0501
Siggy0501
• Member since: Jan. 8, 2005
• Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: _x and _y 2006-01-23 18:16:48

I guess that this was implied by the following script.

At 1/23/06 06:07 PM, True_Darkness wrote: if(Key.isDown(Key.UP)){
this._y-=speed
}

But the further up on the stage you go, the smaller the _y property's value becomes.

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-01-23 18:20:36

At 1/23/06 06:10 PM, Rantzien wrote: It might just be two properties, but I've lost count (like I kept count) of the numbers of times I have had to explain _x and _y to people. Very useful, nice job =)

Yea, I know what you mean, thanks =)

-Siggy- yeah you are right, same thing with the _x position, if it goes too far off the stage to the left, it will reach the negatives =)

Naois
Naois
• Member since: Oct. 31, 2005
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: _x and _y 2006-01-23 18:21:25

At 1/23/06 06:16 PM, -Siggy- wrote: But the further up on the stage you go, the smaller the _y property's value becomes.

That is worth saying. For those who are used to a "normal" graph axis, Flash's is different. The x and the y both "zero" at the upper left hand corner, and get higher to the right and down. This means that instead of up being a += y, it is a -=y. This can be confusing to people who use graphs.

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-01-23 18:25:06

At 1/23/06 06:21 PM, Naois wrote:
At 1/23/06 06:16 PM, -Siggy- wrote: But the further up on the stage you go, the smaller the _y property's value becomes.
That is worth saying. For those who are used to a "normal" graph axis, Flash's is different. The x and the y both "zero" at the upper left hand corner, and get higher to the right and down. This means that instead of up being a += y, it is a -=y. This can be confusing to people who use graphs.

Yea, I should of explained that =(. I remember having trouble with that so much when I first started AS. I got confused all the time :P

Johnny
Johnny
• Member since: Apr. 17, 2004
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: _x and _y 2006-01-23 18:31:21

Everytime I think there's nothing left to do a tute about... one comes out. Nice one Dark!!

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-01-23 18:40:16

At 1/23/06 06:31 PM, Johnny_Krysys wrote: Everytime I think there's nothing left to do a tute about... one comes out. Nice one Dark!!

So very true, and thanks! haha, what will they think of next?

AS: turtle sex
backup
backup
• Member since: Aug. 20, 2005
• Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to AS: _x and _y 2006-01-23 19:37:34

At 1/23/06 06:07 PM, True_Darkness wrote: Not only are they used in Actionscript, but they are used in almost all codeing languages!

Erm, no. _x and _y are ONLY used in actionscript.

Johnny
Johnny
• Member since: Apr. 17, 2004
• Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: _x and _y 2006-01-23 19:50:59

At 1/23/06 07:37 PM, backup wrote:
At 1/23/06 06:07 PM, True_Darkness wrote: Not only are they used in Actionscript, but they are used in almost all codeing languages!
Erm, no. _x and _y are ONLY used in actionscript.

Well, technically... most coding languages use the same system for axis, save for a Z axis... so even though _x and _y aren't "directly" used in other languages, knowing x and y axis is.

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-01-23 20:09:27

At 1/23/06 07:37 PM, backup wrote:
At 1/23/06 06:07 PM, True_Darkness wrote: Not only are they used in Actionscript, but they are used in almost all codeing languages!
Erm, no. _x and _y are ONLY used in actionscript.

Yes, Johnny is right. While it may not be specificly "_x" and "_y" it might be something like "x", "y", "X", "Y". But yes, the horizontal and vertical positions (x,y) are used in other languages. Like Java...

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: _x and _y 2006-05-30 15:08:03

Lol, it's... Complicated...

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-05-30 15:34:59

At 5/30/06 03:08 PM, -Toast- wrote: Lol, it's... Complicated...

Yeah this thing should be in Super Advanced!

O RLY?
Paranoia
Paranoia
• Member since: Apr. 22, 2005
• Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: _x and _y 2006-05-30 16:36:58

I didn't think that _x and _y had to be integers :s

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-05-30 16:53:11

At 5/30/06 04:36 PM, _Paranoia_ wrote: I didn't think that _x and _y had to be integers :s

_x and _y aren't integers. But their valures are. _y=6 well 6 would be the vaule of _y making it the integer. I was just trying to be fancy =D

XIIIthSoundsSoldier
XIIIthSoundsSoldier
• Member since: Feb. 21, 2005
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: _x and _y 2006-07-01 15:22:17

Mine isnt working when i put the movement script in
this is what i have
trace(_root.my_mc._x)
trace(_root.my_mc._y)
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
}
}

whats wrong with it?

caseyo
caseyo
• Member since: May. 14, 2005
• Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to AS: _x and _y 2006-07-01 15:26:17

Those traces have to be on a handler, like just move them into the onClipEvent(enterFrame).

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-07-01 15:36:53

At 7/1/06 03:22 PM, XIIIthSoundsSoldier wrote: Mine isnt working when i put the movement script in
this is what i have
trace(_root.my_mc._x)
trace(_root.my_mc._y)
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
}
}

whats wrong with it?

Well first you have to define the speed.
So you would create a load event to load the variable speed, which you would set to a number such as 5. For example:

speed=5
}

Next I noticed you used the trace command. A trace is just an output, meaning it is just a box that tells you something, it doesn't do anything to the game. So unless you want to know the coordinates of the _x and _y or the MC, then you dont need those.

Also, keep in mind that using "this." is optional, you don't need it.
Next, all you do is combine the load event and the enterFrame event. Like so:

speed=5
}
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=speed
}
if(Key.isDown(Key.LEFT)){
this._x+=speed
}
if(Key.isDown(Key.UP)){
this._y-=speed
}
if(Key.isDown(KeyDOWN)){
this._y+=speed
}
}

And you would put all that on your MC. =D

XIIIthSoundsSoldier
XIIIthSoundsSoldier
• Member since: Feb. 21, 2005
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to AS: _x and _y 2006-07-01 15:58:22

Thanks that kinda worked.

The problem now is that it only moves rightwards and upwards.

When i press left, it goes right.
When i press down, nothing happens.
When i press right and up, it actually goes in those directions.

Is that supposed to happen? or did i do something wrong? if so what?

TrueDarkness
TrueDarkness
• Member since: Aug. 31, 2004
• Offline.
Forum Stats
Member
Level 27
Blank Slate
Response to AS: _x and _y 2006-07-01 16:07:07

At 7/1/06 03:58 PM, XIIIthSoundsSoldier wrote: Thanks that kinda worked.

The problem now is that it only moves rightwards and upwards.

When i press left, it goes right.
When i press down, nothing happens.
When i press right and up, it actually goes in those directions.

Is that supposed to happen? or did i do something wrong? if so what?

My fault actually, but you shouldn't of copyed and pasted =p\
Since your still learning I'll let you get away, but you should learn to be able to fix codes if they are messed up, like mine. The code should be:

speed=5
}
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x+=speed
}
if(Key.isDown(Key.LEFT)){
this._x-=speed
}
if(Key.isDown(Key.UP)){
this._y-=speed
}
if(Key.isDown(Key.DOWN)){
this._y+=speed
}
}