Community

Walls Help

Posted Sep 6, '08 at 5:55am

Maaron

Maaron

45 posts

Iron - Serf

Hey All,

I just want to know how to make a solid wall in flash, so a say movieclip1 can't go through movieclip2.  I've looked everywhere but can't find anything that explains the code properly.

Thanks, Maaron

Posted Sep 6, '08 at 6:01am

Captain_J_Sheridan

Captain_J_Sheridan

143 posts

Gold - Serf

You need to give an instance name to your Wall symbol so you can refer to it on the code

There are two options: one, actually block the Character, which is not very clever, and two, stop him from walking

So, let's say we have a variable "ICanWalkRight" to track if I can walk to the right or if there's a wall right in front of me, you don't actually have to be so specific tough

if (YourCharacter.hitTest(Wall))
{
ICanWalkRight = false;
}

This is just the concept tough, you can code something much nicer

 

Posted Sep 6, '08 at 6:27am

Maaron

Maaron

45 posts

Iron - Serf

Ok I understand everything but the "ICanWalkRight" variable.  How do I set that variable up to track if I walk right.

Thanks, Maaron

 

Posted Sep 6, '08 at 7:02am

Captain_J_Sheridan

Captain_J_Sheridan

143 posts

Gold - Serf

Ok, from the start, let's declare the variable just for clarification

var ICanWalkRight = true;

Well, you just started the game, you should be able to walk Right

And let's say this is your code for walking right now, not actual code as you can see

When I press Right
{
Add 2 to MyCharacter._x
}

And when you collide with a wall, you use the

if (MyCharacter.hitTest(Wall))
{
ICanWalkRight = false;
}

So, if you collide with a wall, it will turn the variable to false, now you just need to apply this variable state to the code for walking

When I press Right AND ICanWalkRight == true
{
Add 2 to MyCharacter._x
}

To be honest, that wouldn't work, because you only have one movieclip to test both direction, so you'd also get a ICanWalkLeft = false if you had one, but then you could create invisible symbols inside your character, properly placed into the right or left side to detect proper collisions, like RightDetect

if (MyCharacter.RightDetect.hitTest(Wall))
{
ICanWalkRight = false;
}

And you would still have trouble because you would never be able to walk Right again after colliding, because ICanWalkRight would be false forever, except you did a negating code for it too

if (MyCharacter.hitTest(Wall))
{
ICanWalkRight = false;
}
else
{
ICanWalkRight = true;
}

I made it a little confusing, sorry about that

 

Posted Sep 6, '08 at 9:15am

Maaron

Maaron

45 posts

Iron - Serf

LOL thanks for the explanation.  I think I might have to read it a few times, LOL.

 

Posted Sep 6, '08 at 1:30pm

dank

dank

777 posts

Iron - Lord

Moderator

You can also run a while on the hitest and move them back depending on what side their coming from.

while(this.hitTest(_root.wall)){
    if(lastX < _x){
        this._x--;
    }else{
         _x++;
    }
}

 

Posted Sep 7, '08 at 12:17am

Maaron

Maaron

45 posts

Iron - Serf

Captain_J_Sheridan I understood everything you said except the part where u wrote.

"When I press Right AND ICanWalkRight == true
{
Add 2 to MyCharacter._x
}"

How do I add the ICanWalkRight == true to the other command.

 

Posted Sep 7, '08 at 12:27am

Captain_J_Sheridan

Captain_J_Sheridan

143 posts

Gold - Serf

You'd need an if statement running inside a loop

(if press right And variable == true)
{

}

You're familiar with "and", right?

Here's the code for walking Right on my game, altough there aren't any walls in it, my character can't walk while he's shoting

this.onEnterFrame = function()
{
    if (Key.isDown(39) and shoting == false)
    {
    Character._x = Character._x + 5
    }
}

 

Posted Sep 7, '08 at 12:28am

Captain_J_Sheridan

Captain_J_Sheridan

143 posts

Gold - Serf

And I wrote the first if wrong XP

if (press right And variable == true)

 

Posted Sep 7, '08 at 12:29am

Captain_J_Sheridan

Captain_J_Sheridan

143 posts

Gold - Serf

And I kept writing it wrong, the "and" can't be with a capital A

 

Posted Sep 7, '08 at 4:03am

Maaron

Maaron

45 posts

Iron - Serf

So I would write it like this:

onClipEvent (enterFrame) {
    if (Key.isDown (Key.Right and ICanWalkRight == true)) {

 
Reply to Walls Help

You must be logged in to post a reply!