Skip to main content
Participant
April 28, 2014
Question

Error with BitmapData.hitTest

  • April 28, 2014
  • 1 reply
  • 275 views

I'm trying to better understand BitmapData.hitTest function so I went to :

http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000794.html

and pasted the example code into flash pro cc frame 1.

*****************************************

import flash.display.BitmapData;

import flash.geom.Point;

var myBitmapData:BitmapData = new BitmapData(100, 80, false, 0x00CCCCCC);

var mc_1:MovieClip = this.createEmptyMovieClip("mc", this.getNextHighestDepth());

mc_1.attachBitmap(myBitmapData, this.getNextHighestDepth());

var mc_2:MovieClip = createRectangle(20, 20, 0xFF0000);

var destPoint:Point = new Point(myBitmapData.rectangle.x, myBitmapData.rectangle.y);

var currPoint:Point = new Point();

mc_1.onEnterFrame = function() {

    currPoint.x = mc_2._x;

    currPoint.y = mc_2._y;

    if(myBitmapData.hitTest(destPoint, 255, currPoint)) {

        trace(">> Collision at x:" + currPoint.x + " and y:" + currPoint.y);

    }

}

mc_2.startDrag(true);

function createRectangle(width:Number, height:Number, color:Number):MovieClip {

    var depth:Number = this.getNextHighestDepth();

    var mc:MovieClip = this.createEmptyMovieClip("mc_" + depth, depth);

    mc.beginFill(color);

    mc.lineTo(0, height);

    mc.lineTo(width, height);

    mc.lineTo(width, 0);

    mc.lineTo(0, 0);

    return mc;

}

*************************************

I get the following error:

Scene 1, Layer 'Layer 1', Frame 1, Line 11, Column 461119: Access of possibly undefined property rectangle through a reference with static type flash.display:BitmapData.

Do I need to setup a property of some sort for this to run?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 28, 2014

You appear to be using Actionscript 2 and possibly a mix of Actionscript 2 and 3.  If you are trying to compile this as an Actionscript 3 file it won't work.

KyleRake1Author
Participant
April 28, 2014

Wow, your right, this was an AS2 help page. Thanks Ned.

Ned Murphy
Legend
April 28, 2014

You're welcome Kyle