PDA

View Full Version : [AS2] 2 Player game problems.


cdrake
08-29-2004, 07:27 PM
The code works when it is just for 1 pereson. But when I try to have it for 2 people only one of them will work.

This is what I am using to try and get the 2 guys to move.

life2 = 1;
life = 1;
gravity = 0.9;
fall = 0.4;
xpos = 0;
_root.onEnterFrame = function() {
if (Key.isDown(65)) {
balloon2.play();

xpos = xpos-1;
balloon2._xscale = 220;
} else if (Key.isDown(68)) {
balloon2.play();

xpos = xpos+1;
balloon2._xscale = -220;

}
if (Key.isDown(87)) {
balloon2.play();

up = true;
fall = fall-0.9;
gravity = 0.3;
} else {
up = false;
}
balloon2._y = balloon2._y+fall;

balloon2._x = balloon2._x+xpos;

gravity = gravity*1.01;
fall = fall+gravity;
};

_root.onEnterFrame = function() {
if (Key.isDown(Key.LEFT)) {
balloon.play();
xpos = xpos-1;
balloon._xscale = 220;
} else if (Key.isDown(Key.RIGHT)) {
balloon.play();
xpos = xpos+1;
balloon._xscale = -220;
}
if (Key.isDown(Key.UP)) {
balloon.play();
up = true;
fall = fall-0.9;
gravity = 0.3;
} else {
up = false;
}
balloon._y = balloon._y+fall;
balloon._x = balloon._x+xpos;
gravity = gravity*1.01;
fall = fall+gravity;
};
stop();

deQue
08-29-2004, 08:12 PM
The problem is that your second _root.onEnterFrame segment overwrites the existing _root.onEnterFrame code, so only the second segment works.
In order to get rid of this you need to put both segments in ONE onEnterFrame function.

That said, you should also AVOID COMPLETELY from using the onEnterFrame function to listen on keys. You should use Intervals instead, which are more precise, and will provide you with better movement, and will also be less strainful on the processor.

cdrake
08-30-2004, 01:20 AM
I got rid of _root.onEnterFrame = function(), but only 1 still works.

JEBoothjr
08-30-2004, 05:48 PM
I wouldn't use onEnterFrame OR intervals to listen for keys. Have you tried using Listeners? Intervals and EnterFrames are both going to be taxing on the cpu since they are constantly running.


myListener = new Object();
myListener.onKeyDown=function(){
trace("ASDF");
}
Key.addListener(myListener);

deQue
08-31-2004, 02:20 AM
That is indeed very true, but Key Listeners don't work as well as intervals when multiple key strokes are used.