1
votes

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."

2
Hey, at least you know how to create an earthquake effect if you ever need one. :DCodeMouse92
Hahah, I can do that with tweening. The problem is that I don't know what's causing the earthquake. Thanks for lightning up my mood thoughMoynul
I guess you could say, the earthquake in your game world is caused by a "dragon" (synonym for bug) in the code, ha ha. In all seriousness, it sounds like the camera is adjusting wayyyy too often...check out my (possible) answer.CodeMouse92

2 Answers

1
votes

Glancing at the code, I'll throw this out as a possible answer. It would seem that the camera's Y position changes with even the slightest change in the player's Y position. (Why that isn't happening on X beats me.)

The first thing that comes to mind is that we need to make the camera a lot less sensitive. We can do this by forcing the function to use an estimation of the position. (By the way, this will only work as long as the character is NOT glued to the center of the screen.)

Since I don't have the whole project, I can't test this. If this line doesn't fix the problem, play with the concept.

camera.y = Math.round(character.y);

Essentially, I'd be forcing the camera to work with whole Y values, not decimal values. That could theoretically cut down on some of the shaking.

If helps some, but not enough, we could implement this to make it go based off of ten pixels, not one. Again, untested (math could be a little off), but you get the idea.

camera.y = Math.round(character.y/10) * 10;

The one theoretical downside of that second method is that the screen will jump when it readjusts. You may need to do some additional coding to make it smooth.

1
votes

This is a potential answer, whaT I have done is changed this block of code to...

              /*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
                    getChildAt(c).y --;
                    while (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                    {
                        //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;
                        //  getChildAt(c).y --;
                        }

                    }
                        //  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! I removed the while!

if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                {
                    getChildAt(c).y --;

                    //OnGround(getChildAt(c)).incrementUp();
                    OnGround(getChildAt(c)).keepOnGround();

                    touchingGround = true;

                }
                else
                {
                    touchingGround = false;
                }

I can't explain why it works, (makes me look like a bad programmer) but it doesn't changed the characters Y position when he is on the ground, thus no more earthquake meaning the dragon can sleep for now.

Credits to JasonMC92 for being there for me each step of the way for helping me realize this is a solution for now.