-
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.x = mouseX;
selectedTower.y = mouseY;
for(var i = 1; i < pathItems + 1; i++){
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
-
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.
-
Originally Posted by cleong
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|