0

Hey everyone this one has me stumped. So I have an Array of Movie clips called sharkthat are added to the stage from either the left or the right side of the stage. Now when they are added I have them move in either the positive or negative x position, so in a Horizontal line. But to make the effect that the player is moving forward in a stream of water and all the other objects are passing the player I also have the array of shark move clips moving in the positive y position so moving vertically downwards. But when I do this have the values in my ENTER_FRAME the shark movie clips in the array appear to be moving Diagonal which is what I don't want at all. Does anyone know how to fix this to have the Movie clip move downwards and also in a straight line across the screen.

Here is what I have so far in my shark class:

private function startPosition():void 
    {
        //Pick the frames for start position
        var nRandom:Number = randomNumber(1, 2);
        //Setup goto and stop on timeline
        this.gotoAndStop(nRandom);

        //Pick a random speed
        nSharkSpeed = randomNumber(3, 5);

        //Pick a random number for start position
        var nLeftOrRight:Number = randomNumber(1, 2);

        //If our nLeftOrRIght  == 1 start on the left side
        if (nLeftOrRight == 1)
        {
            //Start shark on left side and move to right
            this.x = (stage.stageWidth / 2) - 240;
            sSharkDirection = "R";
        }else
        {
            //Start shark on right side and move to left
            this.x = (stage.stageWidth / 2) + 240;
            sSharkDirection = "L";
        }

        //Set y position
        this.y = (stage.stageHeight / 2) - 400;

        //Move our shark
        startMoving();
    }

    private function startMoving():void 
    {
        addEventListener(Event.ENTER_FRAME, sharkLoop);
    }

    private function sharkLoop(e:Event):void 
    {
        //test what direction fish is moving in
        //If fish is moving right
        if (sSharkDirection == "R")
        {
            //Move Shark right
            this.x += nSharkSpeed;
        }else
        {
            //Move Shark left
            this.x -= nSharkSpeed;
        }

        this.y += nSharkSpeed;
    }

any help would be appreciated thanks!

Nathan
  • 536
  • 4
  • 21

1 Answers1

0

It looks like your code does what it means to do: you've told that, firstly, you want sharks to move horizontally, and, secondly, vertically. Well, changing X and Y coordinates gives us diagonal move =)

So, it looks like your code is OK.

Maybe, you should rethink the logic of your application. Also, you may remove/comment the part of code where you change the X position of sharks:

if (sSharkDirection == "R")
{
     //Move Shark right
     this.x += nSharkSpeed;
}else
{
     //Move Shark left
     this.x -= nSharkSpeed;
}

And all sharks will move only by Y.

Mark Dolbyrev
  • 1,887
  • 1
  • 17
  • 24