Skip to main content
Manic-Mars
Known Participant
March 4, 2017
Answered

Collision Detection within child MovieClips

  • March 4, 2017
  • 1 reply
  • 1190 views

I know Ive been posting a lot of questions, but its only because this forum has been so helpful with solving my problems. So I have the right code for movement and collision, but my items on stage are arranged in a way that make it difficult for the code to access. The moving object is on the base level, but the walls are part of the background so they have to be in the background MovieClip. (Otherwise they wont scroll when the background does) So how can I call the child movie clips, which all have the same instance name, without confusing the program? Ive tried MovieClip(getChildByName("wall")) but although the program runs with no errors, it ignores all code after that. Heres the full line for reference: Collision.block(character, MovieClip(getChildByName("wall"))); And if you need it the Collision class I'm using is found here: [ActionScript 3] Collision Class - Pastebin.com

This topic has been closed for replies.
Correct answer Colin Holgate

Well I have a lot of walls since Im doing a big maze type level, I actually want to be able to flip the gravity and view of background at some point. At this point, here is the current playable product: LV one Demo - mars714's Sta.sh  (I'll work on gravity more once the collision works) At any rate, your code returned one warning, void used where a Boolean value was expected. The expression will be type corrected as Boolean. I have the walls numbered, but I'm not sure what to put as the instance name for the original walls.


I see that Collision works differently to how I thought it worked. You call it like:

Collision.block(pseudocharacter,wallmc);

and then look at Collision.collisionSide to find out which direction was hit.

When I've told you to set the fake wall x value, you would also need to be setting its y value if the character can move up and down.

You would name the real walls with the same name, and then your fake wall moving code could look like this:

var i;

var realwallmc: MovieClip;

var fakewallmc: MovieClip;

for (i = 0; i < 10; i++) {

  realwallmc = backgroundmc["wall" + (i + 1)];

  fakewallmc = this["wall" + (i+1)];

  fakewallmc.x = realwallmc.x - backgroundmc.x;

  fakewallmc.y = realwallmc.y - backgroundmc.y;

}

1 reply

Colin Holgate
Inspiring
March 4, 2017

You could put alpha zero versions of the walls at the same level as the character. Those copies x value ought to be the same as the real wall x value minus the background movieclip's x value.

The Collision class is reading the width of the symbol, and so may have the same issue you've had before. A way around that would be to have a rectangle that is a representative width for the character, perhaps the average of all the widths the character goes through. Again, that rectangle could be alpha zero, so the user doesn't see it, and its x value would always be set to match the character's x value.

With those three invisible things at the same level it would be easier to use the block function.

Manic-Mars
Known Participant
March 4, 2017

i think I understand the concept, but I'm having trouble figuring out how create alpha zero versions of my wall movie clip. (Its actually calling the wall movie clip multiple times and I transformed it in width to fit each border.) I pulled a copy of the frame with the movie clips up to the main level, but if I edit the alpha in the movie clip, it will effect the ones in background. So I Clicked color effect and set it to alpha, and then 0%. The code works, but still ignores the walls.

Colin Holgate
Colin HolgateCorrect answer
Inspiring
March 4, 2017

Well I have a lot of walls since Im doing a big maze type level, I actually want to be able to flip the gravity and view of background at some point. At this point, here is the current playable product: LV one Demo - mars714's Sta.sh  (I'll work on gravity more once the collision works) At any rate, your code returned one warning, void used where a Boolean value was expected. The expression will be type corrected as Boolean. I have the walls numbered, but I'm not sure what to put as the instance name for the original walls.


I see that Collision works differently to how I thought it worked. You call it like:

Collision.block(pseudocharacter,wallmc);

and then look at Collision.collisionSide to find out which direction was hit.

When I've told you to set the fake wall x value, you would also need to be setting its y value if the character can move up and down.

You would name the real walls with the same name, and then your fake wall moving code could look like this:

var i;

var realwallmc: MovieClip;

var fakewallmc: MovieClip;

for (i = 0; i < 10; i++) {

  realwallmc = backgroundmc["wall" + (i + 1)];

  fakewallmc = this["wall" + (i+1)];

  fakewallmc.x = realwallmc.x - backgroundmc.x;

  fakewallmc.y = realwallmc.y - backgroundmc.y;

}