PDA

View Full Version : Stop a mc moving objects from going through walls! [Flash][Actionscript]



cashleycool
06-27-2007, 02:48 AM
This is my tutioral to make mc objects stop from moving and go through walls...first if you don't have the movement scripts... copy and paste on a notepad or something, heres the code:

onClipEvent(enterFrame) {
if(Key.isDown(Key.RIGHT)) {
this._x += 6;
}
}

onClipEvent(enterFrame) {
if(Key.isDown(Key.LEFT)) {
this._x -= 6;
}
}

onClipEvent(enterFrame) {
if(Key.isDown(Key.UP)) {
this._y -= 6;
}
}

onClipEvent(enterFrame) {
if(Key.isDown(Key.DOWN)) {
this._y += 6;
}
}

Now,you can have a scripts from stopping a mc objects from ging trough a drawn wall....
on the next reply...

cashleycool
06-27-2007, 02:50 AM
I shall tell you how to do..now!

First of all, you need to make your wall. Make sure it is 50 pixels wide. Mine is just two rectangles with a gap of 220 pixels between them vertically. Each rectangle is 250 in height. If you use those dimensions this tutorial will be easier, since you wont have to change the code at all. Make them a MC (Movieclip) and delete the MC from the stage. (Both the rectangles should be in the same MC).

Now, open the library (Ctrl+L or Windows>Library) and you should see the MC you just made. Right Click on it and then go to ‘Linkage…’.

Now we can put our MC on the stage with actionscript by using the attachMovie() function. Enter this code onto the frame.

Code (actionscript)
var wallY:Number = 475;
createEmptyMovieClip("holder", getNextHighestDepth());
for (i=0; i<200; i++) {
holder.attachMovie("walls", "wall"+i, i);
_root.holder["wall"+i]._x = i*50;
_root.holder["wall"+i]._y = wallY;
if (wallY>550) {
down = true;
}
if (wallY<400) {
down = false;
}
if (down) {
wallY -= 50;
} else {
wallY += 50;
}
}
If you tested your movie now (Ctrl+Enter) you would find that you would have the wall on your stage, and that it covers the whole stage length and changes heights. It doesn’t move with the arrow keys yet, I will show you how to do that later. But for know I will explain the code you just got.

Line 1: This is just declaring a variable called wallY. This is going to tell the wall what its _y coordinates should be.

Line 2: This is creating an empty movie clip called holder. All the wall MC’s that are placed on the stage will be held in here, so when it comes to moving them we can just move holder instead of having to use a for statement. This stops flash from lagging a bit.

Line 3: This is where all the magic happens. It is a for statement, that will keep running until ‘i’ isn’t less than 200. Therefore following code will 200 times.

Lines 4-6: Here we are attaching the ‘wall’ MC to the stage. You should see that the code is holder.attachMovie(”wall… That means we are attaching it inside the holder MC. We are also declaring the walls _x and _y coordinates. The _x coordinates is i*50, so basically it increases by 50 pixels each time. The _y is equal to the wallY variable.

Lines 7-17: All of these lines are determining the walls height. 7-12 are seeing if the walls should move up or down. 12-17 are then making the wallY variable increase or decrease depending on what 7-12 came up with.

Now, if you enter this code beneath the code I just gave you your walls should move with the arrow keys.

Code (actionscript)
onEnterFrame = function () {
if (Key.isDown(Key.RIGHT)) {
holder._x -= 5;
} else if (Key.isDown(Key.LEFT)) {
holder._x += 5;
}
};That is pretty simple. The first line means ‘Everytime this frame is entered’. So the code after that will happen every frame. Unlike the previous you just got, which will only happen once.
The rest of the frames are what makes the walls move. if(Key.isDown(Key.RIGHT)){ is an if statement that is checking is the right arrow key is pressed. If it is than the holder MC’s _x coordinates is told to decrease by 5 pixels. That will make it move left, but appear as if you are traveling right. The next part says ‘if the right arrow key isnt pressed, and the left one is make the holder MC move right’.

Helpfull?????:heh:

sticknewyork
06-27-2007, 05:54 PM
too hard i give up well its not too hard im just lazy

Rather Cheesy
06-29-2007, 01:46 AM
I'll have to try it out when I get my flash comp back in working condition but sounds hopeful [I just skimmed]

Mooper
07-01-2007, 12:27 AM
brain hurting ah too hard for me beginner ow my head is steaming i tried it but i couldn't understand it. But great tut for more advanced people.

Scarecrow
07-01-2007, 12:31 AM
You totally just copy-pasted that, I have never seen you type with such grammar, and I know for a fact that you aren't smart enough to figure this out yourself.

*reported*

cashleycool
07-05-2007, 09:05 AM
You totally just copy-pasted that, I have never seen you type with such grammar, and I know for a fact that you aren't smart enough to figure this out yourself.

*reported*

ouch...i'm 19 :heh: :heh: :heh:


brain hurting ah too hard for me beginner ow my head is steaming i tried it but i couldn't understand it. But great tut for more advanced people.

Thank you, Jawz is a Radical. So try the script!

Scarecrow
07-06-2007, 03:13 AM
What the **** does your age have to do with anything?


You're a ****ing retard.

cashleycool
07-06-2007, 04:20 AM
What the **** does your age have to do with anything?


You're a ****ing retard.

LOL no fevegartgarstgvwrstbgvwdfvbrthbg

Scarecrow
07-06-2007, 08:13 PM
You didn't even answer my ****ing question you tool.

cashleycool
07-07-2007, 05:12 AM
You didn't even answer my ****ing question you tool.

You know matt2k7 doesn't likes you?:Spiderman:

And answer will be answer later

Scarecrow
07-07-2007, 05:32 AM
I don't ****ing give a shit if some noob called matt2k7 likes me.

God you're a ****ing noob.

cashleycool
07-07-2007, 05:40 AM
I don't ****ing give a shit if some noob called matt2k7 likes me.

God you're a ****ing noob.

True, i got his msn

Scarecrow
07-07-2007, 05:42 AM
I don't care if you have his msn either.

****ing hell.

cashleycool
07-07-2007, 05:50 AM
I don't care if you have his msn either.

****ing hell.

He made spp now u know......anyway why making those short madness?

Scarecrow
07-07-2007, 05:55 AM
He didn't make spp you ****ing tool.

I make those short madness's for fun.

cashleycool
07-07-2007, 06:16 AM
He didn't make spp you ****ing tool.

I make those short madness's for fun.

Then who make spp?

Scarecrow
07-07-2007, 06:20 AM
CRAZY JAY and Zulu.

Why the **** would you think matt2k7 made SPP?

Noob.

cashleycool
07-07-2007, 06:29 AM
CRAZY JAY and Zulu.

Why the **** would you think matt2k7 made SPP?

Noob.

er er er X99999 (sex sound, even better than the other one)

Gray
07-07-2007, 06:52 AM
Is this guy for real?

Scarecrow
07-07-2007, 06:56 AM
I ****ing hope not.

Deathwish
07-07-2007, 06:57 AM
Wtf am i reading?

Wtf
07-07-2007, 07:06 AM
The same has I.
And I'm lovin' it. (McDonald's)

cashleycool
07-07-2007, 07:31 AM
The same has I.
And I'm lovin' it. (McDonald's)

Wat r u lovin' it in?

gk99
10-26-2007, 06:34 PM
n0000000000000000bz That Is Nooooooooooooobs In Noob



But The Jaws Is'nt

turkoidz
10-27-2007, 05:23 AM
I agree... Is it not obvious matt is lying to you?