hittest y position

  • ryrocks
  • Newbie
  • Newbie
  • ryrocks
  • Posts: 6

Post 3+ Months Ago

Hey,
I have 2 movieclip class's.
-A ball which you control
-a series of rectangles that the ball "lands" on.

using a hittest, when the ball "hits" the rectangle movieclip it stops directly on top of that rectangle.

I have 3 rectangles all linked to the same class containing the hittest.

My problem is that when I move the ball across each rectangle, it does not land in the same place each time. I believe this is because since I'm using an ENTER_FRAME event, the hitCheck doesn't coincide with the ball's true first contact, and sometimes makes the ball move a little lower than desired.

I've uploaded the .swf: http://www.activ8-3d.co.uk/ryan/test/floor.swf
(use arrow keys to control)

To overcome this I added to the hittest:
[as]
if (_ball_mc.hitTestObject(this))
{
_ball_mc.onGround = true;
}
....
if (_ball_mc.onGround)
{
_ball_mc.y = this.y - this.height - 13;
}
[/as]

_ball_mc.y = this.y - this.height - 13; is the correct position I want the ball to land each time, I thought this would have cracked it, but alas! Its such a minor problem that I didn't want to ask for help on it, but I just can't take it no more!

Here's the full code:

Ball.as document class controls the ball:

Code: [ Select ]
package
{
    import flash.ui.Keyboard;
    import flash.events.*;
    import flash.display.*;

    public class Ball extends MovieClip
    {     
        public var leftDown:Boolean = false;
        public var rightDown:Boolean = false;
        public var jump:Boolean = false;
        public var velocity:Number = 20;
        public var falling:Boolean = true;
        public var speed:int = 5;
        public var onGround:Boolean = false;
    
        public function Ball()
        {         
            stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
            stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
            stage.addEventListener(Event.ENTER_FRAME, moveChar);
        }
        
        public function downDetect(event:KeyboardEvent):void
        {
            if(event.keyCode == Keyboard.LEFT)
            {
                leftDown = true;
            }
            if(event.keyCode == Keyboard.RIGHT)
            {
                rightDown = true;
            }
            if(event.keyCode == Keyboard.UP && !falling)
            {
                jump = true;
            }
        }
        
        
        public function upDetect(event:KeyboardEvent):void
        {
            if(event.keyCode == Keyboard.LEFT)
            {
                leftDown = false;
            }
            if(event.keyCode == Keyboard.RIGHT)
            {
                rightDown = false;
            }
        }
        
        public function moveChar(event:Event):void
        {
            if (leftDown)
            {
                this.x -= speed;
            }
            if (rightDown)
            {
                this.x += speed;
            }
            if (jump)
            {
                this.y -= velocity;
                velocity -= 1.5;
            }
        }
    }
}
  1. package
  2. {
  3.     import flash.ui.Keyboard;
  4.     import flash.events.*;
  5.     import flash.display.*;
  6.     public class Ball extends MovieClip
  7.     {     
  8.         public var leftDown:Boolean = false;
  9.         public var rightDown:Boolean = false;
  10.         public var jump:Boolean = false;
  11.         public var velocity:Number = 20;
  12.         public var falling:Boolean = true;
  13.         public var speed:int = 5;
  14.         public var onGround:Boolean = false;
  15.     
  16.         public function Ball()
  17.         {         
  18.             stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
  19.             stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
  20.             stage.addEventListener(Event.ENTER_FRAME, moveChar);
  21.         }
  22.         
  23.         public function downDetect(event:KeyboardEvent):void
  24.         {
  25.             if(event.keyCode == Keyboard.LEFT)
  26.             {
  27.                 leftDown = true;
  28.             }
  29.             if(event.keyCode == Keyboard.RIGHT)
  30.             {
  31.                 rightDown = true;
  32.             }
  33.             if(event.keyCode == Keyboard.UP && !falling)
  34.             {
  35.                 jump = true;
  36.             }
  37.         }
  38.         
  39.         
  40.         public function upDetect(event:KeyboardEvent):void
  41.         {
  42.             if(event.keyCode == Keyboard.LEFT)
  43.             {
  44.                 leftDown = false;
  45.             }
  46.             if(event.keyCode == Keyboard.RIGHT)
  47.             {
  48.                 rightDown = false;
  49.             }
  50.         }
  51.         
  52.         public function moveChar(event:Event):void
  53.         {
  54.             if (leftDown)
  55.             {
  56.                 this.x -= speed;
  57.             }
  58.             if (rightDown)
  59.             {
  60.                 this.x += speed;
  61.             }
  62.             if (jump)
  63.             {
  64.                 this.y -= velocity;
  65.                 velocity -= 1.5;
  66.             }
  67.         }
  68.     }
  69. }


Ground.as class containing the hittest:
Code: [ Select ]
package
{
import flash.display.MovieClip;
import flash.ui.Keyboard;
import flash.events.*;

public class Ground extends MovieClip
{
    private var _ball_mc:MovieClip;
    private var _findStageInstances:Boolean;
    
    public function Ground()
    {
        stage.addEventListener(Event.ENTER_FRAME, hitCheck);
        _findStageInstances = true;
    }
    
    public function hitCheck(event:Event):void
    {
        if (_findStageInstances)
        {
            _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
            _findStageInstances = false;
        }

        if (_ball_mc.hitTestObject(this))
        {
            _ball_mc.jump = false;
            _ball_mc.falling = false;
            _ball_mc.onGround = true;
            _ball_mc.velocity = 20;
            _ball_mc.onGround = true;
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
            function downDetect(event:KeyboardEvent):void
            {
                if(event.keyCode == Keyboard.UP)
                {
                    stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
                    _ball_mc.jump = true;
                }
            stage.addEventListener(Event.ENTER_FRAME, hitCheck);
            }
        }
        
        if (!_ball_mc.hitTestObject(this))
        {
            _ball_mc.onGround = false;
        }     
        
        if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump && !_ball_mc.onGround)
        {
            _ball_mc.y += 7;
            _ball_mc.falling = true;
        }
        
        if (_ball_mc.onGround)
        {
            _ball_mc.y = this.y - this.height - 13;
        }
    }
}
}
  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.ui.Keyboard;
  5. import flash.events.*;
  6. public class Ground extends MovieClip
  7. {
  8.     private var _ball_mc:MovieClip;
  9.     private var _findStageInstances:Boolean;
  10.     
  11.     public function Ground()
  12.     {
  13.         stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  14.         _findStageInstances = true;
  15.     }
  16.     
  17.     public function hitCheck(event:Event):void
  18.     {
  19.         if (_findStageInstances)
  20.         {
  21.             _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
  22.             _findStageInstances = false;
  23.         }
  24.         if (_ball_mc.hitTestObject(this))
  25.         {
  26.             _ball_mc.jump = false;
  27.             _ball_mc.falling = false;
  28.             _ball_mc.onGround = true;
  29.             _ball_mc.velocity = 20;
  30.             _ball_mc.onGround = true;
  31.             
  32.             stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
  33.             function downDetect(event:KeyboardEvent):void
  34.             {
  35.                 if(event.keyCode == Keyboard.UP)
  36.                 {
  37.                     stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
  38.                     _ball_mc.jump = true;
  39.                 }
  40.             stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  41.             }
  42.         }
  43.         
  44.         if (!_ball_mc.hitTestObject(this))
  45.         {
  46.             _ball_mc.onGround = false;
  47.         }     
  48.         
  49.         if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump && !_ball_mc.onGround)
  50.         {
  51.             _ball_mc.y += 7;
  52.             _ball_mc.falling = true;
  53.         }
  54.         
  55.         if (_ball_mc.onGround)
  56.         {
  57.             _ball_mc.y = this.y - this.height - 13;
  58.         }
  59.     }
  60. }
  61. }


Thank you!
  • ryrocks
  • Newbie
  • Newbie
  • ryrocks
  • Posts: 6

Post 3+ Months Ago

Screw you guys, I've fixed it! (joke!)

I was close before, I was just overcomplicating things. I needed to remove the eventlistener when the hittest was detected because there was a certain point where flash thought the objects were hitting and wern't hitting at the same time :-\ Therefore it set the ball position to the desired position, but then moved it down 7px as if it were falling.
I then reattached the eventlistener after the hittest true method.

That probably didn't make any sense to you! (hard to explain!)

Anywhoooo, heres the working code:

Code: [ Select ]
public function hitCheck(event:Event):void
    {
        if (_findStageInstances)
        {
            _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
            _findStageInstances = false;
        }

        if (_ball_mc.hitTestObject(this))
        {
            _ball_mc.jump = false;
            _ball_mc.falling = false;
            _ball_mc.y = (this.y);
            _ball_mc.velocity = 17;        
            stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
        }
        stage.addEventListener(Event.ENTER_FRAME, hitCheck);
        
        if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump)
        {
            _ball_mc.y += 4;
            _ball_mc.falling = true;    
        }
    }
  1. public function hitCheck(event:Event):void
  2.     {
  3.         if (_findStageInstances)
  4.         {
  5.             _ball_mc = parent.getChildByName("ball_mc") as MovieClip;
  6.             _findStageInstances = false;
  7.         }
  8.         if (_ball_mc.hitTestObject(this))
  9.         {
  10.             _ball_mc.jump = false;
  11.             _ball_mc.falling = false;
  12.             _ball_mc.y = (this.y);
  13.             _ball_mc.velocity = 17;        
  14.             stage.removeEventListener(Event.ENTER_FRAME, hitCheck);
  15.         }
  16.         stage.addEventListener(Event.ENTER_FRAME, hitCheck);
  17.         
  18.         if (!_ball_mc.hitTestObject(this) && !_ball_mc.jump)
  19.         {
  20.             _ball_mc.y += 4;
  21.             _ball_mc.falling = true;    
  22.         }
  23.     }
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1827
  • Loc: In the Great White North

Post 3+ Months Ago

Glad you fixed it. I just hadn't gotten this far down the list yet :-)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 3 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.