Addchild at same place as an instance

13 posts

Flag Post

I have a Ship class that has a shipinstance and shipinstance2 nested inside of it.


		function shootbullet(a, b, m, n)
		{
			var bullets = new Bullet();
			bullets.x = a; //x position the bullet will spawn at
			bullets.y = b; //y position the bullet will spawn at 
			bullets.xSpeed = m;
			bullets.ySpeed = n;
			bullets.rotation = this.rotation;
			stage.addChild(bullets); 
		}

if (numberOfBulletsVar > 1)
							{
								shootbullet(x + shipinstance.x, y + shipinstance.y,20,20);
							}
							if (numberOfBulletsVar > 2)
							{
								shootbullet(x + shipinstance2.x, y + shipinstance2.y,20,20);
							}

I tried that, and they spawn right at the proper spot when my ship isnt rotated, but as soon as I rotate the ship, then it spawns out of posistion. Why, and how do I fix it?

 
Flag Post

Obviously, when you rotate the ship, you have to rotate the point assigned for bullets to spawn, because you’re adding them outside the ship. I only see a static calculation in here.

 
Flag Post

? Even if the ship is rotated, Im just setting it to the x position of the shipinstance. Thats why I dont understand. If I rotate, then the x position of shipinstance is different, so it should still spawn at it (well obviously its not, so im wrong, but I just dont understand)

 
Flag Post

Unfortunately the issue is that a child’s x and y coordinates stay the same regardless of the parent’s orientation – just a standard convention

Trigonometry should be used to calculate the global coordinates mathematically

 
Flag Post

wow that sucks… how do I do that?

 
Flag Post

Try this out for size. (You may need to add + or -90 after “rotation”)

xOffset = Math.sin(rotation*(Math.PI/180))*distance;
yOffset = Math.cos(rotation*(Math.PI/180))*distance;
shootbullet(x + shipinstance.x + xOffset , y + shipinstance.y - yOffset,20,20);

“distance” is however far away “shipinstance” is from the center of the parent object. So if your ship is at 20, and shipinstance is at 25, “distance” would be 5.

 
Flag Post

eh, it didnt work. Ill probably just go back to doing it the old fashioned way of having 10 bullet classes. The rotation is just making it too complicated for my first game in as3. Thanks for the attempt. When I make a top down shooter instead of astroidsesque Itll be a lot easier. Trying to compile them all into 1 clip was a great idea, but its been taking me almost a week and this rotation is still messing it up.

 
Flag Post

hey, you can use localToGlobal() to determine the coordinates of your desired spawnpoint that’s relative to ship into stage’s respective coordinates, could just do.

 
Flag Post

How would I do that?

 
Flag Post
Originally posted by BluePriest:

How would I do that?

point=localToGlobal(point); //var point = new Point(x, y);

 
Flag Post

So I currently have it as


var pt = new Point(shipinstance.x, shipinstance.y);
			pt=localToGlobal(pt);


shootbullet(shipinstance.pt, shipinstance.pt,20,20);

but my bullet is spawning at the bottom of the screen instead of at the location of the shipintsance.

I also tried doing something like


shootbullet(x + shipinstance.pt, y + shipinstance.pt,20,20);

but with the same result. Am I doing something wrong with setting the x and y point in the localtoglobal?

 
Flag Post

Yep, you have to set your pt:Point relative to the ship, therefore no reference to “shipinstance.x” and “shipinstance.y”. You should have your ship drawn, say it has the cannon up front, and you want your bullets to spawn 5 pixels forwards from the center of your ship, which is the ship’s register point, the point around which it rotates. So, you write:

 const FIRING_POINT:Point=new Point(5,0);
var pt:Point=FIRING_POINT;
pt=localToGlobal(pt);
shootbullet(pt, rotation); 

note that rotation has to be passed into shooting routine, otherwise you’ll have troubles when you want your ship to shoot downwards, and instead your bullets will fly say rightwards.

 
Flag Post

how would I modify my shootbullet functions x and y to just search for pt for its x and y coordinate? Im also unsure of where to add it in my Ship class. My initial thought was that it went in the Ship function, since it should only need to be loaded once, but when I do that, it says pt is an undefined property. So I stuck it in my move function which is where the enterframe event is called, and it doesnt give me the error, so I am guessing it goes there? I just want to make sure Im doing it right.