-
CS4: hitTestObject allows MC to pass through..???
I am trying to develop a basic game engine for a slime soccer style game where a player can move around and jump. I am still fairly new at AS3 but managed to put together this working bit of code that the game currently runs on:
Code:
stop();
//vardefs
var runSpeed:Number = 10;
var blueGoals:Number = 0;
var redGoals:Number = 0;
var blueGotGoal:Boolean = false;
var redGotGoal:Boolean = false;
var blueLeftTrue:Boolean = false;
var blueRightTrue:Boolean = false;
var redLeftTrue:Boolean = false;
var redRightTrue:Boolean = false;
var blueOnGround:Boolean = false;
var redOnGround:Boolean = false;
//gravfunctions::
var gravPull:Number = 0.5;
var yPullSpeed:Number = 0;
//jumpfunctions::
var jumpSpeed:Number = 2.103;
//trace(String(blueLeftTrue));
function checkSideMotion(Event:KeyboardEvent){
if(Event.keyCode == 37){
blueLeftTrue = true;
}
if(Event.keyCode == 39){
blueRightTrue = true;
}
}
function sideMotionFalse(event:KeyboardEvent){
if(event.keyCode == 37){
event.keyCode = 0;
blueLeftTrue = false;
trace("false");
}
if(event.keyCode == 39){
event.keyCode = 0;
blueRightTrue = false;
trace("false");
}
}
//trace(String(blueLeftTrue));
function sideMovement(Event){
if(blueLeftTrue == true){
mc_Slime1.x-= runSpeed;
trace("Blue Left Is True")
}
if(blueRightTrue == true){
mc_Slime1.x+= runSpeed;
trace("Blue Right Is True")
}
}
function blueGravity(event:Event){
if(mc_Slime1.hitTestObject(mc_Grass)){
blueOnGround = true;
mc_Slime1.y = mc_Slime1.y;
trace("blueOnGround = true;");
}else{
if(blueOnGround == false){
yPullSpeed += gravPull;
mc_Slime1.y += yPullSpeed;
blueOnGround = false;
trace("blueOnGround = false;")
}
}
}
//trace(String(blueLeftTrue));
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkSideMotion);
stage.addEventListener(Event.ENTER_FRAME, sideMovement);
stage.addEventListener(KeyboardEvent.KEY_UP, sideMotionFalse);
stage.addEventListener(Event.ENTER_FRAME, blueGravity);
The problem is, the "slime" mc actually falls a little bit through the grass layer. what can I do to fix this??
ALSO. Can anybody point me in the right direction as far as a script that makes the MC "jump", still being affected by gravity?
-
Senior Member
You need to remove the event listener when hitting has occurred.
stage.removeEventListener(Event.ENTER_FRAME, blueGravity);
Also it is better to use the Timer class, since ENTER_FRAME is dependent on the framerate.
- The right of the People to create Flash movies shall not be infringed. -
-
I added that line of code into the blueGravity function but I still have the same problem.
-
Also, what can I do to add the ability of the mc_Slime1 to jump?
-
Senior Member
One question for one thread. Did you try the timer with 1ms delay? Enterframe is really not good to use. Also I don't know how the movieclip shapes look like.
- The right of the People to create Flash movies shall not be infringed. -
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|