A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Repeatable Objects

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    19

    Repeatable Objects

    Hey guys, I'm a complete noob - I just started learning flash. I've run into a bit of trouble and I'm hoping someone here can help me out.

    I have code written such that whenever you left click, a movieclip that I drew is placed on the stage. Whenever you hit the spacebar, a moving ball travels to the right. When it comes in contact with the movieclip, it should do something - but this where I'm having trouble.

    if (tiletype == 1)
    {
    var leftDeflectTile:LeftDeflectTile = new LeftDeflectTile();
    addChild(leftDeflectTile);
    leftDeflectTile.x = newX;
    leftDeflectTile.y = newY;
    }

    When the leftDeflectTile gets created in the function that handles left mouse clicks, you can visually create a bunch on the screen, but it seems like after that it considers them null. I can't do a hittest of any sort with leftDeflectTile in another function. If I make the new LeftDeflectTile(); outside the functions in a class, you lose the ability to create more than one but the hittest succeeds.

    What can I do to maintain the ability to create more than one of the object (one for every click) and still do hittests later?

    Thanks!
    Supra

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your problem is one of scope. When you declare leftDeflectTile inside a function, its scope is limited to that function. You've already discovered that by trying to move it outside. What you need to do is keep a collection of LeftTileDeflects. I have already answered very similar questions twice today, and I'm feeling lazy. Search this forum for Array, or just read a few threads on the first page.

  3. #3
    Junior Member
    Join Date
    Jun 2008
    Posts
    19
    thanks for pointing me in a direction - I'm making significant progress now that I'm working with arrays.

    One quick question I have is how can I test what kind of object an array has?

    I want something like this when the array contains a LeftDeflectTile object at i
    trace(tileArray[i] == LeftDeflectTile);

    but.. this doesn't work, it returns false even when it is, in fact, a LeftDeflectTile. What can I change to get this to work?

    Thanks!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What you've got there is testing whether an instance of LeftDeflectTile is the same thing as the class LeftDeflectTile. It is not. You are looking for the "is" operator, or the instanceof operator. They are very similar, and either will work for you.

    Code:
    trace(tileArray[i] is LeftDeflectTile); //true
    However, having to check the type of thing in your array is quite unusual. Most of the time you KNOW what's in there, because you put it there. I'm guessing you're handling different types of tiles differently. Have you considered having a Tile base class with common methods which could be overridden by different types of Tile subclasses? No worries if this sounds too complex, but it's something you may want to return to when you get more comfortable.

  5. #5
    Junior Member
    Join Date
    Jun 2008
    Posts
    19

    Temporarily Suppressing Listener?

    thanks guys this really helped!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center