ForumsProgramming ForumWalls Help

14 4290
Maaron
offline
Maaron
47 posts
Nomad

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

  • 14 Replies
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

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
Maaron
offline
Maaron
47 posts
Nomad

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

Thanks, Maaron

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

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
Maaron
offline
Maaron
47 posts
Nomad

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

dank
offline
dank
986 posts
Peasant

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++;
}
}
Maaron
offline
Maaron
47 posts
Nomad

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.

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

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
}
}
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

And I wrote the first if wrong XP

if (press right And variable == true)
Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

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

Maaron
offline
Maaron
47 posts
Nomad

So I would write it like this:

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

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

You just got a ) at the wrong place

if(Key.isDown(Key.RIGHT) and ICanWalkRight == true)
{

}


Let me give an example without ( ), if you were checking two variables, it would be like this

if(Bat == true and Man == true)
{
trace("I'm Batman!&quot
}
Maaron
offline
Maaron
47 posts
Nomad

What the hell is "trace" for.

Captain_J_Sheridan
offline
Captain_J_Sheridan
313 posts
Nomad

It shows a message on the Output window, its really useful to check if your events are happening and create debugs

Paste this code on Flash and test it to see what I mean

var Bat = true;
var Man = true;

if(Bat == true and Man == true)
{
trace("I'm Batman!&quot
}
Maaron
offline
Maaron
47 posts
Nomad

Ok I understand btw are you ever not logged into armor games lol

Showing 1-14 of 14