Be a Supporter!

addChild unwanted parent properties

  • 307 Views
  • 9 Replies
New Topic Respond to this Topic
Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
addChild unwanted parent properties 2012-05-07 16:39:50 Reply

I'm not sure if you'd need to see the code for this particular problem, but I am currently working on understanding using .as files to create a game.

Okay, so I have a "playerMC" that is loaded with the addChild function from the document class and I have a "bulletMC" that is loaded with addChild from the Player class with the playerMC as the parent. The bullets are fired when the mouse is clicked and are fired at whichever direction the mouse is (a radius).

The playerMC moves when the left and right keys are pressed and the playerMC.scaleX changes depending on whichever left or right key is pressed(so that it will face a certain direction).

The issue I'm having is that when the player is facing right, the bullets fire correctly (with them firing toward the mouse). Now, when the player is facing left, the bullets fire opposite of the mouse on the X axis.

I have almost fixed the issue with an if/else statement using the players scale values, but the issue leaks out a bit and a bullet fires in the wrong direction for a split second before adjusting the trajectory, so this is not the way to go. I want it to be perfect.

So the bullet child is adopting the scale property of the parent player. Any ideas as to how to prevent this? (if you need to see code, I will post it)

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to addChild unwanted parent properties 2012-05-07 17:01:54 Reply

At 5/7/12 04:39 PM, Barzona wrote: Okay, so I have a "playerMC" that is loaded with the addChild function from the document class and I have a "bulletMC" that is loaded with addChild from the Player class with the playerMC as the parent. The bullets are fired when the mouse is clicked and are fired at whichever direction the mouse is (a radius).

Why are you adding the bullet to the player MC? They are separate entities. addChilding a gun to the player, I understand, but the bullet should be added to the doc class. Then set its properties from there once it's been added.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to addChild unwanted parent properties 2012-05-07 20:10:15 Reply

At 5/7/12 05:01 PM, MSGhero wrote: Why are you adding the bullet to the player MC? They are separate entities. addChilding a gun to the player, I understand, but the bullet should be added to the doc class. Then set its properties from there once it's been added.

What about a grappling gun?

They're cool.

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to addChild unwanted parent properties 2012-05-07 22:24:21 Reply

"Why are you adding the bullet to the player MC? They are separate entities. addChilding a gun to the player, I understand, but the bullet should be added to the doc class. Then set its properties from there once it's been added."

You mean to instantiate the bullet (addchild) from the document class and then add it's properties to the bullet class?

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to addChild unwanted parent properties 2012-05-08 00:19:33 Reply

At 5/7/12 10:24 PM, Barzona wrote: "Why are you adding the bullet to the player MC? They are separate entities. addChilding a gun to the player, I understand, but the bullet should be added to the doc class. Then set its properties from there once it's been added."

You mean to instantiate the bullet (addchild) from the document class and then add it's properties to the bullet class?

The player and the bullet should be on the same level, if that's the doc class then so be it, although I would make an inner Game class separate from the Menu class et cetera, but that's just me.

If the bullet is actually in the player then you're gonna have serious problems later.


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

BBS Signature
MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to addChild unwanted parent properties 2012-05-08 00:34:11 Reply

At 5/7/12 08:10 PM, Sam wrote: What about a grappling gun?

They're cool.

I'm entertaining this idea a bit lol...

One of the reasons for not adding it to the player is adding multiple bullets and keeping track of the relative scaleX's of each. It'd be a pain

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to addChild unwanted parent properties 2012-05-08 00:56:55 Reply

At 5/7/12 08:10 PM, Sam wrote:
At 5/7/12 05:01 PM, MSGhero wrote: Why are you adding the bullet to the player MC? They are separate entities. addChilding a gun to the player, I understand, but the bullet should be added to the doc class. Then set its properties from there once it's been added.
What about a grappling gun?

They're cool.

I really want to do that at some point.

Barzona
Barzona
  • Member since: Jan. 4, 2009
  • Offline.
Forum Stats
Member
Level 13
Game Developer
Response to addChild unwanted parent properties 2012-05-08 01:10:30 Reply

At 5/8/12 12:19 AM, MintPaw wrote:
At 5/7/12 10:24 PM, Barzona wrote: "Why are you adding the bullet to the player MC? They are separate entities. addChilding a gun to the player, I understand, but the bullet should be added to the doc class. Then set its properties from there once it's been added."

You mean to instantiate the bullet (addchild) from the document class and then add it's properties to the bullet class?
The player and the bullet should be on the same level, if that's the doc class then so be it, although I would make an inner Game class separate from the Menu class et cetera, but that's just me.

If the bullet is actually in the player then you're gonna have serious problems later.

Okay, here is the code I'm using to add the bullet to the stage:

function fire (m:MouseEvent){
			//spawn a bullet
			var bullet = new Bullet();
			bullet.rotation = this.empty.rotation - 90;
			bullet.x = this.x;
			bullet.y = this.y-50;
			stage.addChild(bullet);
		}

this code is inside the player class .as file right now with "this" referencing the playerMC. Are we understanding that this is the reason the bullet children's scale is changing when the player does?

Is this code actually putting the bullets into the playerMC? I figured it was only positioning it to the same place.

Here's the sample of the game so far. It moves with A and D to move left and right and clicks to fire a bullet.

Sandremss128
Sandremss128
  • Member since: Aug. 22, 2009
  • Offline.
Forum Stats
Supporter
Level 11
Programmer
Response to addChild unwanted parent properties 2012-05-08 04:43:25 Reply

At 5/7/12 08:10 PM, Sam wrote:
What about a grappling gun?

They're cool.

From an OOP perspective a grappling gun is a different unit than the player. They are however related in the sense that the grappling gun is attached to the position of the player but it can have its own relations with the game world.

Indeed the answer is to have the bullets not be in the player. Player is only the initiator of a Bullet and is by no means responsible for the interactions of those bullets (as a class).

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to addChild unwanted parent properties 2012-05-08 19:06:45 Reply

At 5/8/12 04:43 AM, Sandremss128 wrote:
At 5/7/12 08:10 PM, Sam wrote:
What about a grappling gun?

They're cool.
From an OOP perspective a grappling gun is a different unit than the player. They are however related in the sense that the grappling gun is attached to the position of the player but it can have its own relations with the game world.

Indeed the answer is to have the bullets not be in the player. Player is only the initiator of a Bullet and is by no means responsible for the interactions of those bullets (as a class).

I was more going along the lines of: "bullet" being added to the gun.

I would probably go for that when going about a grappling gun but yeah, any gun that has "disposable" ammo, the bullets should be added to a doc class or container of some sort.