Copy link to clipboard
Copied
Hey there, so my game is a platformer. I have gravity working fine, but the only issue is, is that when I just and land on the ground my character goes through the ground a little and if the platform is thin enough, or Im falling fast enough, I go right through the platform, with obviousness, this is a huge problem, but I cannot solve the problem, even when I set the gravity to 0 once the floor is hit.
Who ever wants to help I am going to place the 3 as files that associate with the ground and the gravity, along with the hit testings below.
MAIN FILE
____________________________________________________________
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.events.KeyboardEvent;
import flash.ui.*;
import flash.utils.*;
import flash.geom.*;
import flash.net.*;
import flash.media.Sound;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Main extends Sprite
{
var Hero = new Character();
var platforms = new Platforms();
var BackGround = new Background();
var Keys1 = new Keys();
var nDir:int;
var nDirUpDown:int;
var tmrHitTest:Timer = new Timer(35);
var tmrCurrentStage:Timer = new Timer(35);
var kYmove:int=150;
var kXmove:int=400;
var i:int=0;
public function Main()
{
addChild(BackGround);
addChild(Hero);
addChild(platforms);
addChild(Keys1);
BackGround.y+=kYmove;
BackGround.x+=kXmove;
Hero.y+=kYmove;
Hero.x+=kXmove;
platforms.y+=kYmove;
platforms.x+=kXmove;
Keys1.y+=kYmove;
Keys1.x+=kXmove;
//Checks if the left key has been released;
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandlerLeft);
//Checks if the right key has been released;
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandlerRight);
stage.addEventListener(KeyboardEvent.KEY_DOWN,mover);
stage.addEventListener(KeyboardEvent.KEY_DOWN,moverUpDown);
stage.addEventListener(MouseEvent.CLICK,Clicking);
tmrHitTest.addEventListener("timer",timerHitTest);
tmrHitTest.start();
tmrCurrentStage.addEventListener("timer",timerCurrentStage);
tmrCurrentStage.start();
// constructor code
}
public function Clicking(e:MouseEvent):void
{
platforms.bClicked = true;
//trace(mouseX);
//trace(mouseY);
}
public function mover(event:KeyboardEvent)
{
nDir = event.keyCode;
if (nDir==37||nDir==37&&nDirUpDown==32)
{// left
Hero.bLeftKeyDown = true;
}
else if (nDir==39||nDir==39&&nDirUpDown==32)
{// right
Hero.bRightKeyDown = true;
}
}
function timerCurrentStage(eventArgs:TimerEvent)
{
if (platforms.LevelOne.CurrentStage == platforms.LevelOne.iStage)
{
if(platforms.LevelOne.bRemoveStage == true)
{
while(platforms.LevelOne.arFloor.length > 0){
platforms.LevelOne.removeChild(platforms.LevelOne.arFloor.pop());
}
Hero.StandRight.x=-320;
Hero.StandRight.y=400;
//platforms.LevelOne.arFloor.length = 0;
platforms.LevelOne.bRemoveStage = false;
}
}
if(platforms.LevelOne.CurrentStage == 1){
}else if(platforms.LevelOne.CurrentStage == 2){
platforms.LevelOne.GenerateLevel2();
}else if(platforms.LevelOne.CurrentStage == 3){
platforms.LevelOne.GenerateLevel3();
}else if(platforms.LevelOne.CurrentStage == 4){
platforms.LevelOne.GenerateLevel4();
}else if(platforms.LevelOne.CurrentStage == 5){
platforms.LevelOne.GenerateLevel5();
}else if(platforms.LevelOne.CurrentStage == 6){
platforms.LevelOne.GenerateLevel6();
}else if(platforms.LevelOne.CurrentStage == 7){
platforms.LevelOne.GenerateLevel7();
}else if(platforms.LevelOne.CurrentStage == 8){
platforms.LevelOne.GenerateLevel8();
}else if(platforms.LevelOne.CurrentStage == 9){
platforms.LevelOne.GenerateLevel9();
}else{
platforms.LevelOne.GenerateLevel10();
}
}
function timerHitTest(eventArgs:TimerEvent)
{
if(Hero.StandRight.hitTestObject(Keys1.key1)){
//if(bKeyFound==true){
//Keys1.removeChild(Keys1.key1);
//platforms.LevelOne.removeChild(platforms.LevelOne.arFloor[8]);
//bKeyFound=false;
////}
}
if (platforms.LevelOne.TouchingGround(Hero.HeroGroundTouch))
{
Hero.bTouchingGround = true;
Hero.vy = 0;
}
else if (platforms.LevelOne.TouchingRoof(Hero.HeroRoofTouch))
{
Hero.vy = 0;
Hero.StandRight.y += 4;
}
else
{
Hero.bTouchingGround = false;
}
if (platforms.LevelOne.TouchingLeft(Hero.HeroLeftTouch))
{
//Hero.bTouchingLeft = true;
//Hero.vx = 0;
//Hero.ax = 0;
}
else if (platforms.LevelOne.TouchingRight(Hero.HeroRightTouch))
{
//Hero.bTouchingRight = true;
//Hero.vx = 0;
//Hero.ax = 0;
}else{
Hero.bTouchingLeft = false;
Hero.bTouchingRight = false;
}
}
function keyUpHandlerLeft(event:KeyboardEvent)
{
if (event.keyCode == 37)
{
Hero.bLeftKeyDown = false;
}
}
function keyUpHandlerRight(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
Hero.bRightKeyDown = false;
}
}
private function moverUpDown(event:KeyboardEvent)
{
nDirUpDown = event.keyCode;
if (nDirUpDown==32)
{// Jump
Hero.GroundSnapped = false;
if (Hero.bDownKeyDown == false)
{
if (Hero.bTouchingGround == true)
{
// trace("Jump");
Hero.aGravity=1;
Hero.vy = -20;
Hero.bTouchingGround = false;
}
}
}
}
function keyUpHandlerDown(event:KeyboardEvent)
{
if (event.keyCode == 40)
{
Hero.bDownKeyDown = false;
}
}
}
}
CHARACTER FILE/PHYSICS
______________________________________________________________
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.events.KeyboardEvent;
import flash.ui.*;
import flash.utils.*;
import flash.geom.*;
import flash.net.*;
import flash.media.Sound;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Character extends Sprite
{
var tmrMove:Timer = new Timer(35);
var tmrCoordinateMaintain:Timer = new Timer(35);
var bLeftKeyDown:Boolean = false;
var bRightKeyDown:Boolean = false;
var bTouchingGround:Boolean = false;
var bTouchingLeft:Boolean = false;
var bTouchingRight:Boolean = false;
var bDownKeyDown:Boolean;
var StandLeft = new standLeft ;
var StandRight = new standRight ;
var jumpRight = new JumpRight ;
var jumpLeft = new JumpLeft ;
var RunRight = new runRight ;
var RunLeft = new runLeft ;
var HeroGroundTouch = new heroGroundTouch ;
var HeroRoofTouch = new HeroTouchRoof ;
var HeroRightTouch = new HeroTouchRight ;
var HeroLeftTouch = new HeroTouchLeft ;
var GroundSnap:int = 0;
var GroundSnapped:Boolean = false;
var i:int = 0;
var ax:int = 0;
var vx:int = 0;
var vy:int = 0;
var Stance:int = 2;
var maxVelocity:int = 18;
var minVelocity:int = -18;
var maxGravity:int = 20;
var minGravity:int = -20;
var aGravity:int = 1;
var HeroSize:Number = 1;
public function Character()
{
StandRight.x = -320;
StandRight.y = 400;
StandRight.scaleX = HeroSize;
StandRight.scaleY = HeroSize;
StandLeft.scaleX = HeroSize;
StandLeft.scaleY = HeroSize;
RunRight.scaleX = HeroSize;
RunRight.scaleY = HeroSize;
RunLeft.scaleX = HeroSize;
RunLeft.scaleY = HeroSize;
jumpLeft.scaleX = HeroSize;
jumpLeft.scaleY = HeroSize;
jumpRight.scaleX = HeroSize;
jumpRight.scaleY = HeroSize;
addChild(StandRight);
addChild(StandLeft);
addChild(RunRight);
addChild(RunLeft);
addChild(jumpLeft);
addChild(jumpRight);
addChild(HeroGroundTouch);
addChild(HeroRoofTouch);
addChild(HeroRightTouch);
addChild(HeroLeftTouch);
StandLeft.visible = false;
RunLeft.visible = false;
RunRight.visible = false;
jumpLeft.visible = false;
jumpRight.visible = false;
HeroGroundTouch.visible = false;
HeroRoofTouch.visible = false;
HeroLeftTouch.visible = false;
HeroRightTouch.visible = false;
// constructor code
tmrMove.addEventListener("timer",timerMove);
tmrMove.start();
tmrCoordinateMaintain.addEventListener("timer",timerCoordinateMaintain);
tmrCoordinateMaintain.start();
}
function timerMove(eventArgs:TimerEvent)
{
//trace(StandRight.y);
if (((bLeftKeyDown == true) && bRightKeyDown == true))
{
//if both are pressed then do nothing
}
else if ((bLeftKeyDown == true))
{
if ((bTouchingGround != false))
{
Stance = 1;
jumpRight.visible = false;
jumpLeft.visible = false;
StandRight.visible = false;
RunRight.visible = false;
StandLeft.visible = false;
RunLeft.visible = true;
ax = -1;
}
else
{
Stance = 1;
jumpRight.visible = false;
jumpLeft.visible = true;
StandRight.visible = false;
RunRight.visible = false;
StandLeft.visible = false;
RunLeft.visible = false;
ax = -1;
}
}
else if ((bRightKeyDown == true))
{
if ((bTouchingGround != false))
{
Stance = 2;
jumpRight.visible = false;
jumpLeft.visible = false;
StandRight.visible = false;
RunRight.visible = true;
StandLeft.visible = false;
RunLeft.visible = false;
ax = 1;
}
else
{
Stance = 1;
jumpRight.visible = true;
jumpLeft.visible = false;
StandRight.visible = false;
RunRight.visible = false;
StandLeft.visible = false;
RunLeft.visible = false;
ax = 1;
}
}
else
{
ax = 0;
if ((vx > 0))
{//slows down veloctiies when no keys are pressed
vx -= 1 / 3;
}
else if ((vx < 0))
{//returns velocities to zero when keys are released
vx += 1 / 3;
}
else
{
vx = 0;
if ((Stance == 1))
{//Left Stance
if ((bTouchingGround != false))
{
StandRight.visible = false;
RunRight.visible = false;
StandLeft.visible = true;
RunLeft.visible = false;
jumpLeft.visible = false;
jumpRight.visible = false;
}
else
{
StandRight.visible = false;
RunRight.visible = false;
StandLeft.visible = false;
RunLeft.visible = false;
jumpLeft.visible = true;
jumpRight.visible = false;
}
}
else if ((Stance == 2))
{//Right Stance
if ((bTouchingGround != false))
{
StandRight.visible = true;
RunRight.visible = false;
StandLeft.visible = false;
RunLeft.visible = false;
jumpLeft.visible = false;
jumpRight.visible = false;
}
else
{
StandRight.visible = false;
RunRight.visible = false;
StandLeft.visible = false;
RunLeft.visible = false;
jumpLeft.visible = false;
jumpRight.visible = true;
}
}
}
}//if velocity is a
if ((vx > maxVelocity))
{
vx = maxVelocity;//stop too high velocities
}
if ((vx < minVelocity))
{
vx = minVelocity;
}
if ((vy < minGravity))
{
vy = minGravity;
}
if ((vy > maxGravity))
{
vy = maxGravity;
}
vx += ax;
vy += aGravity;
if ((((vx < 0) && bTouchingLeft == false) || vx > 0 && bTouchingRight == false))
{
StandRight.x += vx / 2.2;
}
if (((bTouchingGround == false) && vy != 0))
{
StandRight.y += vy;
}
else
{
//aGravity=0;
vy = 0;
//aGravity=1;
}
//trace(StandRight.x);
//trace(vy+" is the Gravity");
}
function timerCoordinateMaintain(eventArgs:TimerEvent)
{
//trace( "HERO.X = "+StandRight.x + "HERO.Y =" + StandRight.y);
//trace( vy + " Velocity");
StandLeft.x = StandRight.x;
StandLeft.y = StandRight.y;
RunRight.x = StandRight.x;
RunRight.y = StandRight.y;
RunLeft.x = StandRight.x;
RunLeft.y = StandRight.y;
HeroGroundTouch.x = StandRight.x;
HeroGroundTouch.y = StandRight.y;
HeroRoofTouch.x = StandRight.x;
HeroRoofTouch.y = StandRight.y;
HeroRightTouch.x = StandRight.x;
HeroRightTouch.y = StandRight.y;
HeroLeftTouch.x = StandRight.x;
HeroLeftTouch.y = StandRight.y;
jumpLeft.x = StandRight.x;
jumpLeft.y = StandRight.y;
jumpRight.x = StandRight.x;
jumpRight.y = StandRight.y;
}
}
}
PLATFORMS/HITTESTINGS
________________________________________________________
package
{
import flash.display.*;
import flash.text.*;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.*;
import flash.utils.*;
import flash.geom.*;
import flash.net.*;
import flash.media.Sound;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Level1 extends Sprite
{
var i:int = 0;
var ExFloor=new exFloor();
var tFloor:exFloor = new exFloor ;
var arFloor = new Array();
var GroundSnapped:Boolean = false;
var bRemoveStage:Boolean = false;
var CurrentStage:int = 1;
var iStage:int = 1;
public function Level1()
{
GenerateLevel1();
}
public function PlaceFloor(nx:int,ny:int,nWidth:int,nHeight:int):void
{
tFloor = new exFloor ;
addChild(tFloor);
arFloor.push(tFloor);
tFloor.x = nx;
tFloor.y = ny;
tFloor.width = nWidth;
tFloor.height = nHeight;
}
public function TouchingGround(HeroGroundTouch:Sprite):Boolean
{
for (i=0; i<arFloor.length; i++)
{
if (HeroGroundTouch.hitTestObject(arFloor))
{
GroundSnapped = true;
return true;
}
if (HeroGroundTouch.hitTestObject(arFloor[1]))
{
bRemoveStage = true;
CurrentStage+=1;
iStage+=1;
//return false;
}
}
return false;
}
public function TouchingRoof(HeroRoofTouch:Sprite):Boolean
{
for (i=0; i<arFloor.length; i++)
{
if (HeroRoofTouch.hitTestObject(arFloor))
{
return true;
}
}
return false;
}
public function TouchingLeft(HeroLeftTouch:Sprite):Boolean
{
for (i=0; i<arFloor.length; i++)
{
if (HeroLeftTouch.hitTestObject(arFloor))
{
return true;
}
}
return false;
}
public function TouchingRight(HeroRightTouch:Sprite):Boolean
{
for (i=0; i<arFloor.length; i++)
{
if (HeroRightTouch.hitTestObject(arFloor))
{
return true;
}
}
return false;
}
function GenerateLevel1():void
{
PlaceFloor(435,7,519,36);
PlaceFloor(1055,522,100,19);//NextLevelPlatform
PlaceFloor(105,178,275,32);
PlaceFloor(-160,368,875,22);
PlaceFloor(-405,521,1360,18);// Ground
PlaceFloor(-405,8,480,35);//
PlaceFloor(-405,-140,12,679);//
PlaceFloor(943,-139,12,491);//
PlaceFloor(943,353,12,166);// Door
PlaceFloor(-393,-139,1339,6);//
PlaceFloor(957,522,95,19);
}
function GenerateLevel2():void
{
PlaceFloor(-398,507,1345,20);//Ground
PlaceFloor(978,509,189,15);
PlaceFloor(-397,190,363,17);
PlaceFloor(542,190,404,18);
PlaceFloor(151,314,218,11);
PlaceFloor(-49,8,16,199);
PlaceFloor(-34,153,135,13);
PlaceFloor(541,6,18,202);
PlaceFloor(211,38,98,9);
PlaceFloor(372,153,171,11);
PlaceFloor(-398,78,106,8);
PlaceFloor(-157,11,121,11);
PlaceFloor(558,7,98,13);
PlaceFloor(842,87,104,11);
}
function GenerateLevel3():void
{
PlaceFloor(-398,507,1345,20);//Ground
PlaceFloor(339,144,286,23);
PlaceFloor(-101,341,1047,13);
PlaceFloor(17,19,17,323);
PlaceFloor(19,0,743,18);
PlaceFloor(-398,207,129,15);
PlaceFloor(-101,72,119,14);
PlaceFloor(802,176,143,15);
}
function GenerateLevel4():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
function GenerateLevel5():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
function GenerateLevel6():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
function GenerateLevel7():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
function GenerateLevel8():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
function GenerateLevel9():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
function GenerateLevel10():void
{
PlaceFloor(-398,507,1345,20);//Ground
}
}
}
SORRY IF ITS REALLY MESSY AND CONFUSING
Copy link to clipboard
Copied
add an alpha=0 shape to your ground so it's detected even when your character is moving fast. also, when contract is detected immediately move your character so it does NOT appear as if he's penetrating the ground.
Copy link to clipboard
Copied
I don't really understand, would you be able to elaborated a little bit more?
Copy link to clipboard
Copied
make your ground larger so it's easier to detect contact between your character and the ground. ie, increase its height.
because you probably don't want to change the ground that the user sees, add an alpha=0 shape to your ground. it will look the same but contact will be detected with the current shapes that make up your ground and the alpha=0 shape.
then fix the glitch where it appears the character is falling below the top of the ground. do that by moving the character as soon as contact is detected.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now