A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Hero stops on hitTest

  1. #1
    Tractor Veteran
    Join Date
    Jul 2003
    Posts
    184

    Hero stops on hitTest

    http://www.saskgmperformance.org/Brad/HMPort.fla
    http://www.saskgmperformance.org/Brad/HMPort.swf

    I'm trying to make a port of an old game, but I haven't used flash in a long time.

    So if you run the .swf and walk onto a weed or rock you'll see the problem I'm having. I have the code set up so that if there is no hitTest the character will respond to the keyboard.

    But, when there is a hitTest he gets stuck. I've tried making him go in the opposite direction until the hitTest returns false, but this turns out sloppy and buggy.

    I have to test for multiple, duplicated movie clips, so I'm having trouble finding a method that will work for me.

    Yes I searched
    Thanks in advance.
    Last edited by brad_sk88; 12-11-2006 at 03:41 PM.

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    perform the hitTest after you have updated your position with velocity, then render new position when all calculations have finished

    if(key.isDown(Key.RIGHT)) x += speed
    if(Key.isDown(Key.LEFT)) x -= speed

    if(_root.bounds.hitTest(x, y, true)) //do hit

    _x = x
    _y = y
    lather yourself up with soap - soap arcade

  3. #3
    Tractor Veteran
    Join Date
    Jul 2003
    Posts
    184
    Okay, so now I have
    Code:
    for(i = 0; i <= 200; i++){
    	if(!this.hitBox.hitTest(_root["obstacle"+i].hitBox)){
    		this._x = x;
    		this._y = y;
    	}
    }
    
    other stuff
    
    if(Key.isDown(Key.UP)){
    		if(Key.isDown(Key.SHIFT)){ //Player runs when shift is held with controls
    			this.gotoAndStop("RunN");
    			y -= 6;
    		}else{
    			this.gotoAndStop("WalkN");
    			y -= 2;
    		}
    }
    
    ^^repeated for each keystroke
    But now the hero doesn't move at all.
    Last edited by brad_sk88; 12-05-2006 at 04:39 AM.

  4. #4
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    You probably need a switch, otherwise it continually does the action uncer hittest, so it keeps sending and resending your hero to a given frame. Should have and hit!= 1 when testing for hit, and then switch hit = 1 after he's hit and sent to a frame you need to send it to. Then just reset this to 0 or whatever as needed ( hit animation completed ).

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    Quote Originally Posted by brad_sk88
    Okay, so now I have
    Code:
    for(i = 0; i <= 200; i++){
    	if(!this.hitBox.hitTest(_root["obstacle"+i].hitBox)){
    		this._x = x;
    		this._y = y;
    	}
    }
    
    other stuff
    
    if(Key.isDown(Key.UP)){
    		if(Key.isDown(Key.SHIFT)){ //Player runs when shift is held with controls
    			this.gotoAndStop("RunN");
    			y -= 6;
    		}else{
    			this.gotoAndStop("WalkN");
    			y -= 2;
    		}
    }
    
    ^^repeated for each keystroke
    But now the hero doesn't move at all.
    x and y cannot be temporary variables, in you loop trace out what x and y are. To use this method (bounding box hitTest) you'll need to update your coordinates before the hit, because hitTest uses your _x and _y movieclip coordinates for a bounding box hitTest. In my original post i used shapeflag which tests a point against a shape. So in order to use a bounding box method you will have to project your movieclip out of the collision. A common way is to just use the inverted velocity. But this will make your movieclip act sticky to the object when trying to move diagonally along it
    Last edited by mr_malee; 12-05-2006 at 07:30 PM.
    lather yourself up with soap - soap arcade

  6. #6
    Tractor Veteran
    Join Date
    Jul 2003
    Posts
    184
    The diagonal thing isn't a problem, this is a direct port and the original game had no diagonal movement, just 4 way. I may change it later but for now I'm keeping it that way.

    Unfortunately, I don't know how to use the 'inverted velocity,' can you elaborate?

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    i'm not sure what the correct term is, velocity, speed, acceleration or force but basically its the:

    var speed = 6 bit

    _x += speed

    if you collide, simply say, _x -= speed
    lather yourself up with soap - soap arcade

  8. #8
    Tractor Veteran
    Join Date
    Jul 2003
    Posts
    184
    I've done that, but when you turn around while the hero is 'bouncing' away you slide backwards over the object....

  9. #9
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    i fixed your fla
    lather yourself up with soap - soap arcade

  10. #10
    Tractor Veteran
    Join Date
    Jul 2003
    Posts
    184
    Wow, that's so simple, yet I would have never thought of it on my own.. Thanks.

  11. #11
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    i dunno if you have the framerate set to low or not. but it is very very choppy.

    i think this could be because of your for loop of 200 hittests. From looking at your game, the rocks/bushes look the same size as the character. I would suggest using a tile engine. It will speed things up a lot and make everything easier.

  12. #12
    Tractor Veteran
    Join Date
    Jul 2003
    Posts
    184
    It's running fine on my computer, anyone else having issues with speed?

    (link is in first post)

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