dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: object.addEventListener - hitTest problem

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    object.addEventListener - hitTest problem

    I have a grid of 1024 square. Each square is called cellMC and each has an addEventListener KeyboardEvent.KEY_UP.

    I have one square that travels over the other squares called playerMC; playerMC uses a stage.addEventListener KeyboardEvent.KEY_DOWN (arrow keys) to travel.

    Problem: I cannot get the cell object to detect if the player has touched it.

    for each cell:
    cellMC.addEventListener(KeyboardEvent.KEY_UP, testHit);

    function testHit(event:KeyboardEvent):void{
    if (event.target.hitTestObject(playerMC)) {
    trace(event.target.name);
    }
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What do you mean each square is called cellMC? Do you mean that you have a variable called cellMC that you use to instantiate each of the 1024 squares? Or do you mean you gave each of them the name "cellMC"? Or something else?

    Keyboard events are usually not added to individual display objects because those objects will only dispatch keyboard events if they have the keyboard focus. They probably do not in your case, so your listener is never called. You'll need to add that listener to the stage instead, and instead of using event.target, you'll need to iterate over each of your cell squares.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thanks. I am new to AS3 and rewriting my game from AS2.

    "Do you mean that you have a variable called cellMC that you use to instantiate each of the 1024 squares?" - Yes. I have also named them cellMC1 - cellMC1024.

    "They probably do not in your case, so your listener is never called." - Yes... never called.

    I can use addEventListener with a Mouse Event for each object and get the result I want when the mouse passes over the object. Can this be done with a keyboard event? The player uses the arrow key to travel up-down-right-left, and as fast as the player can hit the arrow key is as fast as the player can move.

    So for each hit of the arrow key I will need to loop through 1024 objects to test for a hit? I hope I have mistaken your reply.

    Thanks, again.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    The game has a grid of 32 x 32 squares that the player can pass over using the arrow keys to navigate. Each square is 20 x 20 pix and each hit of the arrow key moves the player 20 pix.

    In my original version the cellMC (movieclip) has an onClipEvent(keyDown) to test if the playerMC has made contact. Then I duplicate cellMC 1024 times in a square 32 x 32. Each cellMC has unique points, awards, etc for game play.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    I did what you recommended. I looped through the cellMC's and it works. It took sometime to understand how to target the cellMC, though, but I enjoy the learning process. AS3 is more organized and easier to read than navigating all the timeline code in AS2 Flash IDE, especially nested movieclips.

    Thanks.

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