00:00
00:00
Newgrounds Background Image Theme

wilwz just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

x/y position of a movie clip.

3,235 Views | 43 Replies
New Topic Respond to this Topic

x/y position of a movie clip. 2012-11-16 22:50:10


Ok, so I have a movie clip inside of a movie clip inside of a movie clip. In other words, I have movie clip-ception. The problem that I have having is when I reference the x pos of the movie clip. It gives me the position relative to its parent movie clip. How to I make it so that it gives me the position relative to the stage? Thanks!


none

Response to x/y position of a movie clip. 2012-11-16 23:05:17


At 11/16/12 10:50 PM, MarijuanaPenguin wrote: Ok, so I have a movie clip inside of a movie clip inside of a movie clip.

you're using AS2, I assume?

The problem that I have having is when I reference the x pos of the movie clip. It gives me the position relative to its parent movie clip. How to I make it so that it gives me the position relative to the stage? Thanks!

subtract the parent MovieClip's x and y from that.

stageX = parentMC.childMC.x - parentMC.x;
stageY = parentMC.childMC.y - parentMC.y;

Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-16 23:07:38


Sorry, I forgot to mention, but no, AS3.


none

Response to x/y position of a movie clip. 2012-11-16 23:08:51


At 11/16/12 11:07 PM, MarijuanaPenguin wrote: Sorry, I forgot to mention, but no, AS3.

Also I have 2 parents, not just one.


none

Response to x/y position of a movie clip. 2012-11-16 23:18:36


Also this is in a class linked to the 2nd movie clip


none

Response to x/y position of a movie clip. 2012-11-16 23:25:55


At 11/16/12 11:18 PM, MarijuanaPenguin wrote: Also this is in a class linked to the 2nd movie clip

look up localToGlobal and globalToLocal.

Response to x/y position of a movie clip. 2012-11-17 00:03:25


At 11/16/12 11:25 PM, Sandremss128 wrote:
At 11/16/12 11:18 PM, MarijuanaPenguin wrote: Also this is in a class linked to the 2nd movie clip
look up localToGlobal and globalToLocal.

Thanks, but now I keep getting

D:\Dennis\Documents\Flash\tank_turret.as, Line 10	1180: Call to a possibly undefined method Point.
D:\Dennis\Documents\Flash\tank_turret.as, Line 10	1046: Type was not found or was not a compile-time constant: Point.

Help!


none

Response to x/y position of a movie clip. 2012-11-17 00:04:01


At 11/16/12 11:07 PM, MarijuanaPenguin wrote: Sorry, I forgot to mention, but no, AS3.

Why in the world do you need MovieClips when you have a perfectly acceptable class Like Sprite? Or even Bitmap?
Or a framework like Flixel? Or Starling?

(since Sandre seems to have answered your actual question)


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 00:04:38


I think I just need to import point into the class, but where is it?


none

Response to x/y position of a movie clip. 2012-11-17 00:06:51


At 11/17/12 12:04 AM, egg82 wrote:
At 11/16/12 11:07 PM, MarijuanaPenguin wrote: Sorry, I forgot to mention, but no, AS3.
Why in the world do you need MovieClips when you have a perfectly acceptable class Like Sprite? Or even Bitmap?
Or a framework like Flixel? Or Starling?

(since Sandre seems to have answered your actual question)

Because (correct me if I am wrong), but sprites are single framed movie clips, correct? I am going to have multiple frames that I will need to have played within the movie clip at certain times. i.e. A firing animation (like a muzzle flash)


none

Response to x/y position of a movie clip. 2012-11-17 00:07:04


At 11/17/12 12:03 AM, MarijuanaPenguin wrote: Help!

you need to import the Point class.
AS3 will not allow you to just copy/paste code without knowing what it does first, so don't even bother trying. You need to actually look at what's going on.

If you have FlashDevelop, just Ctrl+Shift+1 while the text cursor is on the Point declaration. If not, you'll have to look it up on Google.
(score 1 for FD)


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 00:08:04


At 11/17/12 12:06 AM, MarijuanaPenguin wrote: I am going to have multiple frames that I will need to have played within the movie clip at certain times.

addChild and removeChild.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 00:14:33


At 11/17/12 12:07 AM, egg82 wrote:
At 11/17/12 12:03 AM, MarijuanaPenguin wrote: Help!
you need to import the Point class.
AS3 will not allow you to just copy/paste code without knowing what it does first, so don't even bother trying. You need to actually look at what's going on.

If you have FlashDevelop, just Ctrl+Shift+1 while the text cursor is on the Point declaration. If not, you'll have to look it up on Google.
(score 1 for FD)

Yea I rarely copy and paste code. It is just when I looked this up none of the sites really explained it to me they just gave the code. Also yea i completely forgot about importing the class. Thanks!


none

Response to x/y position of a movie clip. 2012-11-17 00:44:31


Well, just as I had thought it was over, it Billy Maysed me. I get this error and I figured out what line of code is causing it.

Attempting to launch and connect to Player using URL D:\Dennis\Documents\Flash\Tanks.swf
[SWF] D:\Dennis\Documents\Flash\Tanks.swf - 5267 bytes after decompression
TypeError: Error #1009: Cannot access a property or method of a null object reference.
	at tank_turret()[D:\Dennis\Documents\Flash\tank_turret.as:11]
	at flash.display::Sprite/constructChildren()
	at flash.display::Sprite()
	at flash.display::MovieClip()
	at tank()[D:\Dennis\Documents\Flash\tank.as:16]
	at flash.display::Sprite/constructChildren()
	at flash.display::Sprite()
	at flash.display::MovieClip()

The code that is causing it is:

var slope_frontXY:Point = new Point(slope_front.x, slope_front.y);

slope_front is the object that is nested withing two other movie clips and I want to get the x/y position of it.


none

Response to x/y position of a movie clip. 2012-11-17 03:01:22


At 11/17/12 12:44 AM, MarijuanaPenguin wrote: slope_front is the object that is nested withing two other movie clips and I want to get the x/y position of it.

And it is not available at the time you try to access it.

Provide more information about what you want to achieve.
Where is this line that causes the error?
Where is slope_front?

More generally:
What is slope_front?
Why do you need its position as a Pint object?

Response to x/y position of a movie clip. 2012-11-17 13:07:59


Provide more information about what you want to achieve.

Ok, so I am making a game where you control a tank and can shoot it. I already made the moving code, and the aiming code. I am trying to make the code for it to fire.

The code is linked to the tank_turret movie clip. Now ignore the code for the bullet to spawn and fly, I am not done with that. Right now I just want to get the x/y pos. Once again the problem is that it is giving me the position relative to the mc which it is in, and is always going to be 0,-16 or something like that. I tried doing the localToGlobal, but now I get the error in the output as I stated above. Scroll down more for an explanation of what everything is. The code is this

NOTE: in the mouseClicked function, firing is set to false on purpose because I was not testing that atm.

package  {
	
	import flash.display.MovieClip;
	
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.geom.*;
	import round_mc;
	
	public class tank_turret extends MovieClip {
		var slope_frontXY:Point = new Point(slope_front.x, slope_front.y);
		var round:round_mc = new round_mc();
		var fired:Boolean = false;
		
		public function tank_turret() {
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseClicked);
			stage.addEventListener(Event.ENTER_FRAME, update);

            slope_frontXY = slope_front.parent.parent.localToGlobal(slope_frontXY);
            
			
			// constructor code
		}
		
		protected function mouseClicked(e:MouseEvent) {
        
	    stage.addChild(round);
		round.x = slope_front.x;
		round.y = slope_front.y;
		round.rotation = rotation;
		fired = false;	
		}
		protected function update(e:Event) {
			
			if (fired) {
			var slopeY = slope_front.y - slope_back.y;
			var slopeX = slope_front.x - slope_back.x;
			round.x += (slopeX)/5;
			round.y += (slopeY)/5;
			}
			
		}
		
	}
	
}

tank_turret consists of four movie clips. The turret_base and turret_barrel(irrelevant to this. and then the slope_front and slope_back. They are the two red boxes you see in the front of the barrel and in the back (barrel is under the base so you can't see it all the way). I am using them to calculate slope so I can have the bullet move the correct x/y value relative to its rotation. I also use the front one as a spawn location. My linkage in the library is tank(the whole thing) is linked to tank. round_mc(the bullet aka that thing to the right) is linked to round_mc. And finally tank_turret(the turret_base, turret_barrel, slope_front, and slope_back) is linked to tank_turret. All their linked class files have the same name as the mc they are linked to. Here is a picture for your viewing pleasure.

x/y position of a movie clip.


none

Response to x/y position of a movie clip. 2012-11-17 14:51:27


I hope you realize how ridiculously inefficient everything you're doing is. It would be funny if it wasn't so damn bad.

basically you're trying to use AS3 like you used AS2.
This is a no-no. It won't work, AS3 is too different.

public class tank_turret extends MovieClip

should be:

public class TankTurret extends Sprite
var round:round_mc = new round_mc();

why???

var fired:Boolean = false;

I can almost guarantee a more efficient way of doing this.

stage.addEventListener(Event.ENTER_FRAME, update);

don't abuse stage. That's like using "_root." everywhere in AS2.

slope_frontXY = slope_front.parent.parent.localToGlobal(slope_frontXY);

i'm going to cry. Children don't access parents. If you must do something, dispatch an event (even better, a Signal)

protected function mouseClicked(e:MouseEvent) {

does this need to be protected? Why?

stage.addChild(round);

you add children to classes extending DisplayObject, only one thing should ever bi in the stage, and that's Main.

protected function update(e:Event) {

again, unless you're planning on extending this class (I hope not), it doesn't need to be protected.

also, I dunno if it's a bad copy/paste, but your formatting is way off.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 15:24:45


Again, I am a beginner and this is my first time using classes... so here are more questions for your pleasure :D

var round:round_mc = new round_mc();

why???

(Correct me if I am wrong) But when I watched a tutorial on addChild(); the guy said you need to do that first. If I don't what do I do instead?

stage.addEventListener(Event.ENTER_FRAME, update);

don't abuse stage. That's like using "_root." everywhere in AS2.

What do you mean?

slope_frontXY = slope_front.parent.parent.localToGlobal(slope_frontXY);

i'm going to cry. Children don't access parents. If you must do something, dispatch an event (even better, a Signal)

Example please, I still have no idea of what to do here.

protected function mouseClicked(e:MouseEvent) {

does this need to be protected? Why?

Again, this is my first time using classes, and the guy in the tutorial said you should protect your functions, no reason why, he just said it. Can you please explain to me the different things and when I should use them. i.e. public, private, etc.

stage.addChild(round);
you add children to classes extending DisplayObject, only one thing should ever bi in the stage, and that's Main.

Explain please. Remember I am a newbie.

protected function update(e:Event) {
also, I dunno if it's a bad copy/paste, but your formatting is way off.

No this is not copied/pasted except for the two localToGlobal lines. And the only reason I did that is because none of the sites explained it at all and just gave the code. If you can explain it please :3 Also what part of the formatting is off?


none

Response to x/y position of a movie clip. 2012-11-17 15:50:43


At 11/17/12 03:24 PM, MarijuanaPenguin wrote: Again, I am a beginner and this is my first time using classes... so here are more questions for your pleasure :D

var round:round_mc = new round_mc();

why???
(Correct me if I am wrong) But when I watched a tutorial on addChild(); the guy said you need to do that first. If I don't what do I do instead?

Yes you must initiate a class first, but what is that class? Its name is meaningless and I would guess that it does nothing.

stage.addEventListener(Event.ENTER_FRAME, update);

don't abuse stage. That's like using "_root." everywhere in AS2.
What do you mean?

Remove the "stage." part of that line. It serves no purpose, it's like using this.health++ rather that just health++ it's just there to make you feel better or something.

slope_frontXY = slope_front.parent.parent.localToGlobal(slope_frontXY);

i'm going to cry. Children don't access parents. If you must do something, dispatch an event (even better, a Signal)
Example please, I still have no idea of what to do here.

Objects are not allowed to access there parents this means using "parent.", "root.", or even having a direct reference to parents. If you need to do this then you've design the class badly and likely a parent class should preform this action.

protected function mouseClicked(e:MouseEvent) {

does this need to be protected? Why?
Again, this is my first time using classes, and the guy in the tutorial said you should protect your functions, no reason why, he just said it. Can you please explain to me the different things and when I should use them. i.e. public, private, etc.

Use private unless you need parents to access the variables, then use public. The other identifiers very specific and you won't need to worry about them right now.

stage.addChild(round);
you add children to classes extending DisplayObject, only one thing should ever bi in the stage, and that's Main.
Explain please. Remember I am a newbie.

Don't add things to the stage, the stage is not as flexible as you'd think, add one Object to the stage and add all other Objects inside of it.

protected function update(e:Event) {
also, I dunno if it's a bad copy/paste, but your formatting is way off.
No this is not copied/pasted except for the two localToGlobal lines. And the only reason I did that is because none of the sites explained it at all and just gave the code. If you can explain it please :3 Also what part of the formatting is off?

Every brace has an additional indentation. Examine the lines below.

private function myFunction():void
{
     if (5 > 2)
     {
          trace("5 is indeed greater than two");
          while (true)
          {
               trace("This loop will run forever");
          }
     }
     if (true)
     {
          trace("This code is meaningless, it is only an example of good formatting");
     }
}

If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 15:59:40


At 11/17/12 03:24 PM, MarijuanaPenguin wrote: Again, I am a beginner and this is my first time using classes... so here are more questions for your pleasure :D

alright, sounds fun.

(Correct me if I am wrong) But when I watched a tutorial on addChild(); the guy said you need to do that first. If I don't what do I do instead?

graphics

example:

graphics.lineStyle(lineThickness, lineColor, lineAlpha);
graphics.beginFill(color, colorAlpha);
graphics.drawCircle(radius, radius, radius);
graphics.endFill();

which will make a circle with registration top-left.

What do you mean?

I mean "_root.", "stage.", and "this." all have their purposes, but should not be used unless absolutely necessary.

Example please, I still have no idea of what to do here.

meaning don't use "parent." - I have not once needed to use it. I'm sure it has its use, but you should really avoid it.

Again, this is my first time using classes, and the guy in the tutorial said you should protect your functions, no reason why, he just said it. Can you please explain to me the different things and when I should use them. i.e. public, private, etc.

you use protected if you're building frameworks that you're sharing with the whole world (even then, not much, unless you want your framework to be really inflexible) - that's about it. Bottom line is don't worry about protecting your stuff.

Explain please. Remember I am a newbie.

means don't abuse "stage." - it has a specific purpose, and everything else should be done within the class itself. Main extends Sprite for a reason.

No this is not copied/pasted except for the two localToGlobal lines. And the only reason I did that is because none of the sites explained it at all and just gave the code. If you can explain it please :3 Also what part of the formatting is off?

I think Mint already showed you good formatting. He doesn't like to cuddle braces, but cuddling is a matter of choice. Personally, I cuddle my braces.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 16:05:30


Yes you must initiate a class first, but what is that class? Its name is meaningless and I would guess that it does nothing.

Well, it does do nothing, so do I need to do that or not?

Objects are not allowed to access there parents this means using "parent.", "root.", or even having a direct reference to parents. If you need to do this then you've design the class badly and likely a parent class should preform this action.

So then how would I go around to finding what the red blocks' x and y positions are relative to the stage?

Don't add things to the stage, the stage is not as flexible as you'd think, add one Object to the stage and add all other Objects inside of it.

So I should add a container to the stage and then add all the bullets to it with say appendChild(); ?

Every brace has an additional indentation. Examine the lines below.

Lol, that is how I always used to write my code but then a watched this "pro's" tutorials and he said that I should use the most common method and that is how I am doing it. Should I stop watching tutorials? xD

Thanks :D


none

Response to x/y position of a movie clip. 2012-11-17 16:11:24


Ok, I got everything you said except for this. Are you saying I should use this instead of the round movie clip? Do I still do addChild(); or what?

graphics

example:

graphics.lineStyle(lineThickness, lineColor, lineAlpha);
graphics.beginFill(color, colorAlpha);
graphics.drawCircle(radius, radius, radius);
graphics.endFill();

which will make a circle with registration top-left.

none

Response to x/y position of a movie clip. 2012-11-17 16:21:17


At 11/17/12 04:11 PM, MarijuanaPenguin wrote: Ok, I got everything you said except for this. Are you saying I should use this instead of the round movie clip? Do I still do addChild(); or what?

i'm saying you shouldn't be using a MovieClip at all. There is only 1 thing a MovieClip can do that a Sprite can't, and that's for animators to worry about, not programmers.
(besides, exporting to a sprite sheet and blitting would be better, anyway)

no, you don't addChild. you go into your DisplayObject (whatever's extending Sprite) and just draw the graphics there. You should only need to do so once, but just in case you want to add a new "frame" to the sprite, you may want to look up how to clear the graphics.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 20:43:02


I still didn't get what I came here for though, lol. How do I get the x and y relative to the stage and not the mc the two red boxes are in?


none

Response to x/y position of a movie clip. 2012-11-17 20:58:18


At 11/17/12 08:43 PM, MarijuanaPenguin wrote: I still didn't get what I came here for though, lol. How do I get the x and y relative to the stage and not the mc the two red boxes are in?

the red boxes shouldn't be in a DisplayObject, they should be in a vector in a class.
Do that, and problem solved.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 21:23:57


:vector in a class.

Uhm... what's that? :P


none

Response to x/y position of a movie clip. 2012-11-17 21:39:13


At 11/17/12 09:23 PM, MarijuanaPenguin wrote: Uhm... what's that? :P

in the simplest terms: a typed array.

var bullets:Vector.<Sprite> = new Vector.<Sprite>

it works almost exactly like an array, except it has a few extra features, and it can only hold objects of one type.

Documentation exists for a reason.

keep pushing a new bullet class into the vector, and make sure you remove old bullets when they're no longer needed.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to x/y position of a movie clip. 2012-11-17 22:31:35


At 11/17/12 09:39 PM, egg82 wrote:
At 11/17/12 09:23 PM, MarijuanaPenguin wrote: Uhm... what's that? :P
in the simplest terms: a typed array.

var bullets:Vector.<Sprite> = new Vector.<Sprite>

it works almost exactly like an array, except it has a few extra features, and it can only hold objects of one type.

Documentation exists for a reason.

keep pushing a new bullet class into the vector, and make sure you remove old bullets when they're no longer needed.

Wait, I am confused, how does that get me the x/y coords? I feel like I am missing something really obvious.


none

Response to x/y position of a movie clip. 2012-11-17 22:36:53


Actually, better idea. Does anyone know if it is possible to convert rotation to slope? I got everything to work but I need to make the bullets fly in the direction they are rotated. I know this can be done with trigonometry, but since I am in 9th grade, I did not learn that yet. Thanks.


none

Response to x/y position of a movie clip. 2012-11-17 23:40:21


At 11/17/12 10:36 PM, MarijuanaPenguin wrote: Actually, better idea. Does anyone know if it is possible to convert rotation to slope? I got everything to work but I need to make the bullets fly in the direction they are rotated. I know this can be done with trigonometry, but since I am in 9th grade, I did not learn that yet. Thanks.

No, but you can convert degrees to radiant and radiant to a vector(not the same kind of vector egg was talking about) by sin and cosing it.
But that won't fix your terribly designed and ultimately broken code.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature