1

Honestly my question may have been answered but I don't have a college degree and only understand basic math and some algebra. I do have a lot of experience coding in as3 and animate and have read alot of answers and questions so far but I'm still lost.

I know how to do a hitTest on the object and picture I made for the green in my golf game. It appears that bitmap data is what I need to use but I'm new to it and never used it at all.

What Im thinking is when the golf ball enters the hitTestObject bounding box and is true to get the bitmap data and only look for color and dont worry about the transparent pixels. Then when the ball stops it should be on the green???

I have no idea so far on how to do this or is this the best thing to do?

I attached a picture of the green and some of the course so you can look and try to understand whats going on. Sand traps will be an issue but for now I want to focus on the green.

golf green with bounding box

jasonl
  • 23
  • 4

1 Answers1

0

What you're looking for is BitmapData.hitTest.

First, you need to have a BitmapData of the green grass. You only need to do this once so don't include this part inside your loop when you check for collision.

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;

private var bm:Bitmap;
private var bitmapData:BitmapData;

//after everything has instantiated:
bitmapData = new BitmapData(backhole1.green1.width, backhole1.green1.height);
bitmapData.draw(backhole1.greenMap);
bm = new Bitmap(bitmapData);

When ready, check for collision:

if (bm.bitmapData.hitTest(new Point(backhole1.greenMap.x, backhole1.greenMap.y), 255, new Point(backhole1.golfball.x, backhole1.golfball.y), 255)) {
  //collision detected, do stuffs
}

The value 255 is to check for opacity of the bitmap. Anything that is not 100% opague will be treated as transparent in this case.

jasonl
  • 23
  • 4
user1234567
  • 333
  • 2
  • 10
  • I'm sorry I woke up today and tried this answer. There is a problem with a missing close ) . Its in the collision detection code. You opened with three ( and closed one but opened again and closed 2. So there needs to be one more close. I put one on the end after the ,255 so there are 2 close but still have an error with geom.point – jasonl Mar 05 '19 at 14:54
  • The actual error is 1067:implicit coercion of a value of it to an unrelated type flash.geom:Point. Could be cause I might have that missing close ) in the wrong place. – jasonl Mar 05 '19 at 14:58
  • I edited the answer so you can see what I changed to put it into my program and I got 1067 error. I added the missing ) as well. – jasonl Mar 05 '19 at 15:15
  • What is // after everything has been instantiated. Do I need more code here or something? – jasonl Mar 05 '19 at 18:13
  • Right, I missed a bracket. Instantiated means when the object is 'created'. Since we are referencing blackhole1.green1, we have to make sure the MC is created on the stage before we can reference it. So that bit of code has to come after blackhole1.green1 is created. – user1234567 Mar 05 '19 at 19:41
  • ok great. if it wasnt for you I would have been in the dark. I did find this code and after playing with it it works great and was along the line of what you answered. http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/ – jasonl Mar 05 '19 at 20:12