|
Prev: Parent Code
Next: Protected Movie
From: rritchey on 5 May 2008 09:58 I recently downloaded a collision detection class from gskinner.com for an interaction that I am making in AS2 where I needed some REAL hitTesting with 2 transparent PNG's. The class works fine testing locally by itself, but for final release, the SWF will be loaded into a Flash interface via a Loader component. Lockroot is set to false by default in the interface for other reasons, but I am able to change it on the fly via a function in the interface. Regardless of the lockroot settings, when the interaction is loaded into the interface, the hit detection does not work. I am thinking this may have something to do with the following 2 lines in the class: var bounds1:Object = p_clip1.getBounds(_root); var bounds2:Object = p_clip2.getBounds(_root); I am wondering if the (_root) pass into getBounds is causing problems with the different roots in the final interface. This is broken even with lockroot set to true. I have tried changing the _root to _parent, this, and removing it altogether. It only works outside of the interface when the _root is there, and it doesn't work inside the interface at all. Any thoughts?
From: dzedward on 5 May 2008 10:15 what happens if you trace _root. Do you get the expected results?
From: rritchey on 5 May 2008 10:25 Why do I never think to trace things like _root..... I traced it out, and with lockroot set to true, I do get the correct path. (In my instance that is "_level0.loaderClip.currentPage.my_loader") But the hit detection still breaks. Here is my code, and the collision detection class from gskinner my code: function checkCollision() { // check for collisions: var collisionRect:Rectangle = CollisionDetection.checkForCollision(gauge,smFront,120); // if there is a collision: if (collisionRect) { drawin.clear(); with (drawin) { beginFill(0x00FF00,20); lineStyle(0,0x00FF00,40); moveTo(collisionRect.x,collisionRect.y); lineTo(collisionRect.x+collisionRect.width,collisionRect.y); lineTo(collisionRect.x+collisionRect.width,collisionRect.y+collisionRect.heig ht); lineTo(collisionRect.x,collisionRect.y+collisionRect.height); lineTo(collisionRect.x,collisionRect.y); } if(smFront._x > 250 && smFront._x < 390 && collisionRect.height <= 33){ smFront._x = smBack._x = 318.95; zoomGauge._visible = bubble._visible = lgFront._visible = lgBack._visible = true; lgFront._y = lgBack._y = smBack._y+47; }else if(smFront._x > 250 && smFront._x < 390 && collisionRect.height > 33){ smFront._x = smBack._x = 318.95; smFront._y = smBack._y = 290; zoomGauge._visible = bubble._visible = lgFront._visible = lgBack._visible = true; lgFront._y = lgBack._y = smBack._y+47; } if(smFront._x < 310){ smFront._x -= collisionRect.width; smBack._x -= collisionRect.width; zoomGauge._visible = bubble._visible = lgFront._visible = lgBack._visible = false; lgFront._y = lgBack._y = -25; } if(smFront._x > 330){ smFront._x += collisionRect.width; smBack._x += collisionRect.width; zoomGauge._visible = bubble._visible = lgFront._visible = lgBack._visible = false; lgFront._y = lgBack._y = -25; } }else{ zoomGauge._visible = bubble._visible = lgFront._visible = lgBack._visible = false; lgFront._y = lgBack._y = -25; } //match position of the back piece of the O-Ring to the front piece smBack._x = smFront._x; smBack._y = smFront._y; } smFront.onPress = function() { this.startDrag(); interval = setInterval(checkCollision,1); this.onMouseUp = function() { this.stopDrag(); delete(this.onMouseUp) clearInterval(interval); i = 0; while(i < 10){ this._parent.checkCollision(); i++; } } } collision detection script: import flash.display.BitmapData; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geom.Rectangle; class com.gskinner.sprites.CollisionDetection { static public function checkForCollision(p_clip1:MovieClip,p_clip2:MovieClip,p_alphaTolerance:Number):R ectangle { // set up default params: if (p_alphaTolerance == undefined) { p_alphaTolerance = 255; } _level0.debug.text = _root; // get bounds: var bounds1:Object = p_clip1.getBounds(_root); var bounds2:Object = p_clip2.getBounds(_root); // rule out anything that we know can't collide: if (((bounds1.xMax < bounds2.xMin) || (bounds2.xMax < bounds1.xMin)) || ((bounds1.yMax < bounds2.yMin) || (bounds2.yMax < bounds1.yMin)) ) { return null; } // determine test area boundaries: var bounds:Object = {}; bounds.xMin = Math.max(bounds1.xMin,bounds2.xMin); bounds.xMax = Math.min(bounds1.xMax,bounds2.xMax); bounds.yMin = Math.max(bounds1.yMin,bounds2.yMin); bounds.yMax = Math.min(bounds1.yMax,bounds2.yMax); // set up the image to use: var img:BitmapData = new BitmapData(bounds.xMax-bounds.xMin,bounds.yMax-bounds.yMin,false); // draw in the first image: var mat:Matrix = p_clip1.transform.concatenatedMatrix; mat.tx -= bounds.xMin; mat.ty -= bounds.yMin; img.draw(p_clip1,mat, new ColorTransform(1,1,1,1,255,-255,-255,p_alphaTolerance)); // overlay the second image: mat = p_clip2.transform.concatenatedMatrix; mat.tx -= bounds.xMin; mat.ty -= bounds.yMin; img.draw(p_clip2,mat, new ColorTransform(1,1,1,1,255,255,255,p_alphaTolerance),"difference"); // find the intersection: var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF); // if there is no intersection, return null: if (intersection.width == 0) { return null; } // adjust the intersection to account for the bounds: intersection.x += bounds.xMin; intersection.y += bounds.yMin; return intersection; } }
From: dzedward on 5 May 2008 10:43 what happens if you trace out the bounds? for(var i in bounds1){ trace(i:+" "+bounds1[i]); }; You may be grabbing the bounds of the wrong object?
From: rritchey on 5 May 2008 11:35
My trace outside of the interface: yMax: 495.9 yMin: 45.9 xMax: 463 xMin: 157 My trace inside of the interface: yMax: 495.9 yMin: 45.9 xMax: 463 xMin: 157 So, the bounds appear to be getting set properly. |