A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: bitmapdata.hitTest

  1. #1
    Member
    Join Date
    Feb 2007
    Posts
    63

    bitmapdata.hitTest

    I'm cutting my teeth in actionscript 3 on a game that has a character running through a world.

    So, I have set up my Hit Testing by using the bitmap data hit test method, since I figured my world is going to be destructible - it'd be nice to update the level and then redraw it and have the character interact with the new change. (That works beautifully)

    I am however; a bit confused as to how I have my hit Testing set up. I've been messing around with it, and it works for now - but I'm not sure why..

    currently, I have a character set up by using a class I built and using a series of animations I created. So, this character has a walking and falling animation, etc. This is a movie clip.

    Then there's a bitmap 'emptyBitmap' with bitmap data created - however; I never really added this as a child to the character. This is sized to the dimensions of my character.

    My level is created as a movielip, then it's drawn to a bitmap - when the level movieclip is changed, the bitmap redraws, and that's how this updates.

    currently, my hit testing is a huge mess of if statements - that I'd like to simply (hopefully).

    Code:
      if(emptyBitmap.bitmapData.hitTest(new Point(character.x, character.y), 255, new Point(level.x, level.y))) {
         trace("I'm on the ground");
    
            if(emptyBitmap.bitmapData.hitTest(new Point(character.x + 3, character.y - (character.height /2)), 255, new Point(level.x, level.y))) {
             trace("this hit's my right side at the midpoint, or the gut area - so, I hit the wall");
        }}

    This is completely messy, however; my idea is that if I check for each area of the character I can get him to react differently. So, if the point where the feet would be, then he's on the ground.. but when the point to the right where the gut would be, he's hit a wall and needs to reverse.

    If a point 3/4ths down hits on the right, then he's hit a slop and needs to go up on the slope. I'm not sure how I'd write this code any cleaner.

    Any pointers?

    Also, I'm a little concerned that I'm comparing one bitmap data that never moves using the points of the character to compare with the level.

  2. #2
    Member
    Join Date
    Feb 2007
    Posts
    63
    Ok, I've cleaned up a few things.

    I figured out that I can add the bitmap to the character, and then just turn off the visibility of the bit map and it'll still hitTest properly. Nice to know!

    I'd still like to clean this code up better - but for the life of me, can't figure that out.

    Here's my official hitTest statement.

    Code:
    if (lemmingGuy.lemmingChar.bitmapData.hitTest(new Point(lemmingGuy.x,lemmingGuy.y ),255,b.bitmapData,new Point(b.x,b.y))) {
    		if (endFall == -1) {
    			endFall = lemmingGuy.y;
    			fallDistance = endFall - startFall;
    			fallDeath(fallDistance);
    		}
    		lemmingActivity("walk");
    		if(lemmingGuy.lemmingAction != "stopper"){
    		lemmingGuy.lemmingAction = "walking";
    		} else if(lemmingGuy.lemmingAction == "stopper"){
    			lemmingGuy.lemmingAction = "stopper";
    		}
    		// will I walk up this slope?
    		if (lemmingGuy.lemmingChar.bitmapData.hitTest(new Point(lemmingGuy.x + 2,lemmingShins),255,b.bitmapData,new Point(b.x,b.y))) {
    			lemmingGuy.lemmingAction = "slope";
    			// did I bumpt into a wall?
    			if ((lemmingGuy.lemmingChar.bitmapData.hitTest(new Point(lemmingGuy.x + 2,lemmingGut),255,b.bitmapData,new Point(b.x,b.y))) ||(lemmingGuy.bitmapData.hitTest(new Point(lemmingGuy.x -  50,lemmingGut),255,b.bitmapData,new Point(b.x,b.y)))) {
    				if((lemmingGuy.lemmingAction == "slope") || (lemmingGuy.lemmingAction == "walk")){
    				lemmingGuy.scaleX *= -1;
    				lemmingGuy.lemmingAction = "reverse";
    				}
    					// Did I bump my head?
    					if (lemmingGuy.lemmingChar.bitmapData.hitTest(new Point(lemmingFeet,lemmingGuy.y - 32),255,b.bitmapData,new Point(b.x,b.y))) {
    						lemmingGuy.lemmingAction = "reverse";
    				}
    			}
    		}
    	} else {
    		// we must be falling...
    		if (startFall == -1) {
    			endFall = -1;
    			startFall = lemmingGuy.y;
    			lemmingActivity("fall");
    		}
    .. yeah

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