I can scroll the ground left and right, but I can't scroll the ground up and down.
private function scrollStage():void {
if (lastPosX != lastPosX)
{
canScrollStage = false;
}
else if (lastPosX == lastPosX)
{
canScrollStage = true;
}
if (canScrollStage)
{
if (rightKey)
{
//move background left
//something.x +=(stage.stageWidth * 0.5) - character.x * 2;
}
else if (leftKey)
{
//move backgrounf Roight
}
for (var b:int = 0; b < childrenOnStage; b++)
{
if (getChildAt(b).name == "enemy" || getChildAt(b).name == "enemyRed" || getChildAt(b).name == "knife")
{
getChildAt(b).x += (stage.stageWidth * 0.5) - character.x;
//getChildAt(b).y += - character.y;
}
}
ground.x += (stage.stageWidth * 0.5) - character.x;
//ground.y += (stage.stageHeight * 0.5) - character.y;
//ground.y += (stage.stageHeight) - character.y;
}
else
{
//move the background
}
// do this last, everything moves around object
character.x = stage.stageWidth * 0.5;
lastPosX = character.x;
I have tried doing character.y = stage.stageHeight * 0.5;
But this just glues the characters y position to the center of the screen.
When the world can scroll I tried implementing this in to my code //ground.y += (stage.stageHeight * 0.5) - character.y;
but this doesn't really work either.
Can anyone please point me in the same direction or give me tips and advice, I have tried implementing this code
// A 'Camera' is really just a Point within the world we want to center on
// the screen.
var camera:Point = new Point();
// Set the camera coordinates to the char coordinates.
//camera.x = character.x;
camera.y = character.y;
// Adjust the world position on the screen based on the camera position.
// world.x = -camera.x + (stage.stageWidth / 2);
ground.y = -camera.y + (stage.stageHeight / 2);
It does work! But the screen shakes up and down violently
I traced the players y position and it was going up and down
but with out that code the y position stayed the same.
Thank you.
EDIT - GRAVITY
In my main class I have this which stops the player from falling.
for (var c:int = 0; c < childrenOnStage; c++)
{
if (getChildAt(c).name == "player")
{
if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
{ //if the boundary collides with the player or enemy
while (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
{
OnGround(getChildAt(c)).incrementUp();
//bump up the object until it isn't hitting the boundary;
if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
{
// do nothing
//touchingGround = false;
}
else
{ // once it isn't hitting the boundary, do this function for keeping the object on the boundary
OnGround(getChildAt(c)).keepOnGround();
touchingGround = true;
}
}
// ends while ( _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) ) {;
}
else
{
// ends if ( _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) )
touchingGround = false;
}
this is the the onGround class.
public class OnGround extends MovieClip
{
protected var grav:int = 1;
protected var charIsrunning:Boolean;
protected var isDefending:Boolean;
protected var isJumping:Boolean;
public function OnGround()
{
addEventListener(Event.ADDED_TO_STAGE, init)
charIsrunning = false;
isDefending = false;
//gotoAndStsop("jump");
}
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
grav = 1;
addEventListener(Event.ENTER_FRAME, fall);
}
private function fall(e:Event):void
{
grav ++;
if (grav > 15)
{
grav = 10;
}
this.y += grav;
}
public function incrementUp():void
{
//this.y -= 0.1;
}
public function keepOnGround():void
{
grav = 0;
positionOnLand();
}
public function positionOnLand():void
{
//overide
}
}
}
I fear that camera.y can't keep up with character.y or character.y changes despite the code telling the game "Stop falling, your y axis is being invcremented."