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.../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:
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;
}
}
}
}
Ground.as class containing the hittest:
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;
}
}
}
}
Thank you!

New Topic/Question
Reply



MultiQuote




|