PDA

View Full Version : Help With A ActionScript Code...


Elf2008
03-08-2008, 04:28 PM
Hi...

I am working on a Flash RPG. I know it will take me a while to complete it but hopefully one day i will have it finished.

I am having trouble getting a collision code to work. What i want is, when my man MovieClip collides with the wall MovieClip, i want him to stop. At the moment i have it so when you touch the wall it stopes, which is what i want, but then i cant move away. I have got the code so u can control the MovieClip with the arrow keys.

Here is my code:




onClipEvent (load) {
power = 10

}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)){
_x -= power;
}
if (Key.isDown(Key.RIGHT)) {
_x += power;
}
if (Key.isDown(Key.UP)) {
_y -= power;
}
if (Key.isDown(Key.DOWN)) {
_y += power;
}
}
onClipEvent (enterFrame) {
if (_root["wall"].hitTest(_root["man"])){
var power = 0;
}
}



When my Man collides with the wall he stops, but then i cant move away from the wall.

Please can someone tell me some code that will let me collide and stop at the wall, and let me move away after. And then when i collide for a second time he stops, and can move away again and so on.

Thank You

Elf

filipeabreu
03-08-2008, 04:51 PM
Put the variable value inside every if (Key.isDown) statement, in this order.

if (Key.isDown(Key.LEFT)){
power = 10
_x -= power;
}

Or even use the same add assignment operator and change the power variable in each statement:


onClipEvent (onKeyDown) {
if (Key.isDown(Key.LEFT)){
Xpower = -10
}
if (Key.isDown(Key.RIGHT)) {
Xpower = 10
}
if (Key.isDown(Key.UP)) {
Ypower = -10
}
if (Key.isDown(Key.DOWN)) {
Ypower = 10
}
}
onClipEvent (enterFrame) {
_x += Xpower
_y + = Ypower
if (_root["wall"].hitTest(_root["man"])){
var Xpower = 0;
var Ypower = 0;
}
}

This is pretty uncomplet yet. The fact is that you need a 4 side hit test, to see wich side has hit the wall, and then you can know wich side you can't keep going to (i.e. adding the valeu of Xpower to X or Ypower to Y). The code I posted will still give you the same problem, but it gives you a hint on how you can resolve this. It still got much to be done. Hope you can figure that out.

Elf2008
03-08-2008, 05:16 PM
I think i have finally worked it out... i just need someone to tell me if this is ok code to use... I have tested it and it wokes all round the wall... Just tell me if it is a suitable code for an RPG...

Here it is...



onClipEvent(enterFrame){
if (Key.isDown(Key.LEFT)){
power = 10
_x -= power;
}
if (Key.isDown(Key.RIGHT)){
power = 10
_x += power;
}
if (Key.isDown(Key.UP)){
power = 10
_y -= power;
}
if (Key.isDown(Key.DOWN)){
power = 10
_y += power;
}
}
onClipEvent (enterFrame) {
if (_root["wall"].hitTest(_root["man"])){
if (Key.isDown(Key.RIGHT)){
power = 10
_x += -power;
}
if (Key.isDown(Key.LEFT)){
power = 10
_x -= -power;
}
if (Key.isDown(Key.UP)){
power = 10
_y -= -power;
}
if (Key.isDown(Key.DOWN)){
power = 10
_y += -power;
}
}
}



Thank You

Elf

Durnus
03-11-2008, 09:09 PM
Well... no offense here, but I don't think you should be programming an RPG quite yet. Not to discourage you, it's a good idea, but you should get more experiance before taking on such a huge project. (I've learned that too many times.)

What I question is that code's portability and usability. I try to design my classes with that in mind. For example, a lot of times I start with a myObject class, which I define to do collisions and physics stuffs. Then I extend that class to make players and pickups and other things like that. It really makes it much faster in the end, and before making an RPG which would take a long time to make and requires a lot of similar code the way you're programming it, I would suggest pong or breakout or something that can be finished in a few hours where the code doesn't need to be expandable. You'll pick up a lot of ideas that'll help in the long run.

Good luck!