Hi guys. I’m making a game for my final year degree project that has two characters on screen but one is a reflection of the other in the bottom half. They both respond to the same keyboard controls (left,right,up,down) and can swap positions with each other when the spacebar is pressed.
I have posted on the forum before when I was having trouble getting the characters to stop “flipping” positions on every frame while the spacebar was held down. Shaedo and JonnyR were very helpful and I managed to sort out the problem I was having. However, I have since run into some more problems that I just havn’t been able to work out in code so I’m posting again in the hope that someone will help me!
One of the main mechanics in the game involves objects in the top half of the screen flipping positions at the same time one of the characters does, when that character is touching the object. My aim is to get the object to appear in the same position in the bottom half of the screen but upside down, as though it is has become a reflection of itself (only one object exists, it just swaps positions).
I have been able to get the objects to change positions but the problem with the spacebar press has come up again - if you hold the spacebar down the objects will flip on every frame like the characters were before Shaedo supplied the code to stop them. If I apply the same code to delay the flip on the objects they don’t work at all - the code to stop the flip is - if ((char.flip) && (flipCounter > flipCounterLimit)) { - “char.flip” is the spacebar press boolean, “flipCounter” is initiated in the game loop and simply counts the number of frames, and “flipCounterLimit” is set at 60 for a 1 second delay (game runs at 60fps).
Can anyone help me out?
All the “flip” code is inside the moveCharacter function. Here is the section of code responsible for flipping the characters and objects as it exists at the moment:
//char is the top screen character, schar is the bottom screen character
//shifters is the array that contains the objects to be flipped
public function moveCharacter(char:Object,schar:Object,timeDiff:Number) {
if (timeDiff < 1) return;
var plachaFlipPointY:Number = char.mc.y;
var shadowPlachaFlipPointY:Number = schar.mc.y;
if ((char.flip) && (flipCounter > flipCounterLimit)) {
flipCounter = 0;
if (char.mc.y <= midPoint-3) {
char.mc.y = shadowPlachaFlipPointY;
schar.mc.y = plachaFlipPointY;
char.mc.scaleY = -1;
schar.mc.scaleY = -1;
}
else if (char.mc.y >= midPoint+3) {
char.mc.y = shadowPlachaFlipPointY;
schar.mc.y = plachaFlipPointY;
char.mc.scaleY = 1;
schar.mc.scaleY = 1;
}
}
for(var j=0;j<shifters.length;j++) {
if (shifters[j].mc.y <= midPoint-3) {
if ((char.flip) && (char.mc.hitTestObject(shifters[j].mc))) {
shifters[j].mc.y = schar.mc.y;
shifters[j].mc.scaleY = -1;
//return;
}
}
else if (shifters[j].mc.y >= midPoint+3) {
if ((char.flip) && (schar.mc.hitTestObject(shifters[j].mc))) {
shifters[j].mc.y = char.mc.y;
shifters[j].mc.scaleY = 1;
}
}
}
(I realise that the object’s new position after the “flip” isn’t accurate as it only moves to the registration point of the character. I will hopefully solve this with an invisible hit box inside each character)