A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Weird getChildByName and HitTestObject problem

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    75

    Weird getChildByName and HitTestObject problem

    Hi

    I am having a really weird problem I can't explain, here is my code:

    PHP Code:

    function onMoveTower(e:Event):void{
        if(
    towerPlacing){
            
            
    selectedTower.mouseX;
            
    selectedTower.mouseY;
            
            for(var 
    1pathItems 1i++){
                var 
    pathItem:MovieClip MovieClip(getChildByName("path" i));
                
    //pathItem.x += 5;
                
    if(selectedTower.hitTestObject(pathItem)){
                   
    selectedTower.alpha 0.2;
                   
    selectedTower.gotoAndStop(1);
                }else{
                    
    selectedTower.alpha 1;
                    
                }
            }
            
            
        }
        
        


    Basically it is a tower defense game and I am checking to see if my tower is hitting the path.

    The path is made up of 15 squares on stage with the names path1, path2 .... path15.

    That function I posted is inside a ENTER_FRAME event.

    Anyway for some reason it only detects a hitTestObject on the last path item.

    I thought it was a casting issue but if I uncomment the line pathItem.x += 5; then ALL the path items do move across that screen.

    I have no idea what the problem can be, can anyone help me?

    Cheers

  2. #2
    Member
    Join Date
    May 2012
    Posts
    51
    It only works for the last item because you continue the loop even after you're found the right path. Say hitTextObject() returns true on i = 3, the alpha is reduced to 0.2. But the function will return false on i = 4, so the alpha is set back to 1. What you need is a break statement at the end of the if clause.

  3. #3
    Member
    Join Date
    Jun 2009
    Posts
    75
    Quote Originally Posted by cleong View Post
    It only works for the last item because you continue the loop even after you're found the right path. Say hitTextObject() returns true on i = 3, the alpha is reduced to 0.2. But the function will return false on i = 4, so the alpha is set back to 1. What you need is a break statement at the end of the if clause.
    wow that didn't occur to me at all, thanks for your help

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