-2

I'm working on a game in Actionscript, and the following code is contained within a function that checks whether or not a key has been pressed on the keyboard. You can ignore all of the other variables, as the only thing that's important is 'OrbRing', a movieclip being loaded from the library.

When the TAB key is pressed, the length of a bar onscreen is decreased by a set amount, and a ring appears onscreen. The first time this is attempted, everything functions normally: the ring appears, and the bar length is decreased.

However, the second time this is attempted, the bar length is decreased, but the OrbRing does not appear.

Strangely enough, the third time this is attempted, everything functions normally again, just like the first time.

But then the fourth time, the issues are the same as the second attempt.

So every even attempt, the addChild code seems to be ignored, but the function is still running since the bar length decreases every time. And every odd attempt, the function performs normally.

I have no idea why this is happening, and I get no error messages. Any help or ideas that might fix this would be greatly appreciated.

if(levelseven==true) {
    if (mykey.keyCode==Keyboard.TAB) {
        if(gear1==true && CPUBAR1_mc.CPUBARYLW_mc.width>=215) {

            CPUBAR1_mc.CPUBARYLW_mc.width-=215;
            addChild(OrbRing);
            OrbRing.x=Orb_mc.x;
            OrbRing.y=Orb_mc.y;
            OrbRing.gotoAndPlay(1);     
        }
    }
}
xsami
  • 1,312
  • 16
  • 31
Brojack
  • 3
  • 2
  • Have you checked if you even hit the conditional with a trace? My first assumption is that either gear1 is false or the width of that Movieclip is beneath 215. –  Sep 08 '17 at 12:48
  • Need some more context here. I'd need to see the functions involved to help further. – sfxworks Sep 08 '17 at 12:58
  • What is the code used to make the ring disappear? – BadFeelingAboutThis Sep 08 '17 at 18:03
  • 1. I can confirm that the conditionals have been hit. I've expanded on why in my response to the answer down below. 2. For more context on the function, sure thing. The only other line of code involved in this function is this one, aside from adding the event listener earlier on. function checkkeysdown(mykey:KeyboardEvent) { 3. On last frame of movieclip - this.parent.removeChild(this); – Brojack Sep 08 '17 at 18:15
  • Try using a different key. TAB has a special meaning in Flash. – Vesper Sep 09 '17 at 05:28
  • Thanks for the suggestion, but I've already found a solution. Although you're right, using the TAB key can do things you don't always want to happen in flash, so I'll likely be replacing it with a different key anyways. – Brojack Sep 09 '17 at 07:36
  • By what is readable code is ok. Check the conditionals, since the second, fourth, etc. time you might not even get to the addChild. Put a trace after each if statement and get a glimpse of what gets executed. – FilippoG Sep 08 '17 at 13:50
  • I can confirm that the 'if' statement is being run and all the conditionals are being met, as when the 'if' statement is run, it's supposed to decrease the width of a movieclip onstage, as shown by this line of code "CPUBAR1_mc.CPUBARYLW_mc.width-=215;". On the second and fourth attempts (i.e. the 'problem' attempts), this decrease in the movieclip width still occurs, but the addChild command does not. This confirms that the conditionals for the 'if' statement have been met, it's just ignoring the addChild command. – Brojack Sep 08 '17 at 16:11
  • That's very strange, it would trigger an error if something were wrong in the addChild statement. Other possibilities might be content or container clip have alpha at 0, or visible at false, or width/height at 0. – FilippoG Sep 08 '17 at 16:13
  • It is quite a head-scratcher. There are definitely no error messages. As far as alpha being involved, yes there are frames where the alpha is at 0 in the movieclip, but this is done manually (i.e. not through code), and these frames run normally on every odd attempt. I'll try setting the alpha to 100 for all these frames and see if it changes anything. – Brojack Sep 08 '17 at 18:21

1 Answers1

-1

I've found the solution. For whatever reason (I still don't know why), the movieclip was getting stuck on the first frame on every even attempt. Simply adding a gotoAndPlay(2); instruction made sure the movieclip played every time. Thanks very much for all your help.

Brojack
  • 3
  • 2