1

Please tell me if there is more information required to solve this problem

I have an array that lists objects followed by their locations and I'm iterating through it to find what frame the program is to go to.

So the loop that iterates through the list is bound the the character object so the character changes frames when it hits an object in the list.

I cut some stuff out so this isn't a code dump

function moveDude(e:Event){
 var obj:Object = e.target;
 for (i = 0; i<cols.length; i+=2)
        {
            if (cols[i] != null)
            {
                if (obj.hitTestObject(cols[i]))
                {
                    gotoAndStop(cols[i+1]);


                    break;
                }
              }
            }

Array of objects defined outside function

var cols = new Array(world.t1,6,world.lv1,"donkeyKong",background1.hit,3,world.door,1);

http://prntscr.com/5mqdpm the function in defined in the frame that extends over the other frames and then is added or removed from the character object if the character is using the listener or not.

example on first frame:

if (character!=null)
    {   character.addEventListener(Event.ENTER_FRAME, moveDude);


    }

this all works fine, however when the character travels from one frame to another, the hitTest with the object that returns it to frame 1 does not work? see the last object/frame in the array, it's not being detected in the for loop mentioned below

7
  • This is pretty confusing. So in your first block, what is obj? Your cols array and iterating every other item is a strange way to set this up. Commented Dec 30, 2014 at 20:34
  • Yea I know I didn't take a course so all my code is unusual. Obj is a reference to character through var obj:Object = e.target; I have defined higher up in the function. I'll paste the whole fla if you want to have a better look. Commented Dec 30, 2014 at 20:37
  • 1
    Most likey when you change frames and go back, a new character is created (instantiated), but it's hard to tell what's going with out seeing more of the program Commented Dec 30, 2014 at 20:39
  • @LDMS Hey you're that guy that helped me with that other issue! I remember now thanks by the way. anyway, I use the same character instance throughout the program i just change it's coordinates and whatnot. Here is full FLA mediafire.com/download/pm3jf5dgyr4eg5r/Final.zip Commented Dec 30, 2014 at 20:48
  • 1
    I opened it up, it's a little crazy. It hangs flash when I run it once I hit enter. (and it goes to frame 1 or 2). I simply don't have the time to weed through it and tell you what's wrong. What you should do, is forgo all timeline code. Use a document class, give your visual assets linkage id's, then instantiate them through code, and dispose them through code too. Trying to debug and change things in a tangle of timeline code is tedious at best. Make class files that extend Sprite for each scene or game state, and organize your code that way. Commented Dec 31, 2014 at 16:52

2 Answers 2

1

When you move from one frame to the next, everything on the previous frame is deleted, so unless your cols array is regenerated when you come back to the frame, you're trying to test for collisions with objects that don't exist anymore.

Sign up to request clarification or add additional context in comments.

Comments

0

This may not be your issue, however;

for (i = 0; i<cols.length; i+=2)

You're not declaring i, here, so what happens when the loop goes out of scope? Does i get destroyed? I don't know. I don't fully understand garbage collection myself. Where are you declaring i? Because if it's outside the loop, then you may not be resetting its value. If you aren't declaring it at all, perhaps it's not getting garbage collected in time and holding it's value

for (var i:uint = 0; i<cols.length; i+=2)

Is how a variable should be declared if you only want it to have scope within the for loop. When the loop restarts, it will be reinitialised to 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.