Hi there folks…
i have a small problem, i have to simulate gravity on a character with collision detection, now every thing works fine, just a little problem occurs when i character has gravity and it hit the top object when key is not pressed , the character passes right through the top hurdle…
here is the image of swf file…
The code which moves the character is here, but first i would like to thank SENOCULAR for his KeyObject Class which makes life easier …
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import com.senocular.utils.KeyObject;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.geom.Rectangle;
public class Character extends MovieClip
{
private var stageRef:Stage;
private var key:KeyObject;
public var speed:Number = 0.5;
public var vx:Number = 0;
public var vy:Number = 0;
public var friction:Number = 0.9;
public var maxSpeed:Number = 8;
public var gravity:Number = 0.98;
public var bounds:Rectangle;
public var xCollide:Boolean;
public var yCollide:Boolean;
public function Character ()
{
addEventListener (Event.ADDED_TO_STAGE, addedToStage);
}
// when added to stage.....
private function addedToStage (eve:Event):void
{
stageRef =this.stage;
//get key input on stage..
key = new KeyObject(stageRef);
//define the boundries of the character;
bounds = new Rectangle(0,0,stageRef.stageWidth,stageRef.stageHeight);
//event listener to move character...
addEventListener (Event.ENTER_FRAME, moveChar, false, 0, true);
}
private function moveChar (eve:Event):void
{
//accelerate the character when key is pressed...
if (key.isDown(Keyboard.LEFT))
{
vx -= speed;
}
else if (key.isDown(Keyboard.RIGHT))
{
vx += speed;
}
else
{
// reduce the speed when key is up...
vx *= friction;
}
if (key.isDown(Keyboard.UP))
{
gravity = 0;
vy -= speed;
}
else if (key.isDown(Keyboard.DOWN))
{
gravity = 0;
vy += speed;
}
else
{
gravity = 0.98;
//reduce the speed when key is up...
vy *= friction;
}
//determine the max speed of character so it should not exceed that..
if (vx > maxSpeed)
{
vx = maxSpeed;
}
else if (vx < -maxSpeed)
{
vx = -maxSpeed;
}
if (vy > maxSpeed)
{
vy = maxSpeed;
}
else if (vy < -maxSpeed)
{
vy = -maxSpeed;
}
//determin the boundries of our character
if (this.x > bounds.width)
{
this.x = bounds.width;
}
else if (this.x < bounds.x)
{
this.x = bounds.x;
}
if (this.y > bounds.height)
{
this.y = bounds.height;
}
else if (this.y < bounds.y)
{
this.y = bounds.y;
}
//check for collisions to bounce back the character.
if (xCollide)
{
vx = -vx;
xCollide = false;
}
if (yCollide)
{
if (gravity != 0)
{
vy = -2;
}
else
{
vy = -vy;
}
yCollide = false;
}
// update the position of character on stage...
this.x += vx;
this.y += vy;
//rotate the object on stage
this.rotation = vx*2 ;
this.y += gravity;
}
}
}
and here is the document class for main movie
package
{
import flash.display.*;
import flash.events.*;
import com.dataflat.HitTestObj;
public class CollisionTest extends Sprite
{
public function CollisionTest():void
{
init();
}
private function init():void
{
addEventListener(Event.ENTER_FRAME, doEnterFrame);
}
private function doEnterFrame(eve:Event):void
{
var hitTest = HitTestObj.checkCollisions(circle_mc, hurdle_mc);
if (hitTest)
{
circle_mc.xCollide = true;
circle_mc.yCollide = true;
trace ("got collision.....");
}
}
}
}
if some one can please help me out with this, i will be dearly thankful…