Sidescrolling Platformer Object and Level Movement Issues

Discuss issues specific to the Flash/AS3 port of Box2D
orangatang
Posts: 5
Joined: Tue Jun 19, 2012 3:40 pm

Sidescrolling Platformer Object and Level Movement Issues

Postby orangatang » Wed Jun 20, 2012 12:40 am

Hey guys I'm having some trouble with smooth character movement and object movement on my AS3 coded box2d flash game. I am actually utilizing the quickbox2d engine as it saves me alot of time coding-wise. Anyway, I have tried without much success to apply character movement through forces. I have much more success in implementing velocities to objects which need to move. My main problem is not really in my character movement, rather it is the effects of the moving level on the box2d objects which are created during runtime. Linked below is my game engine: create box2d circle objects by clicking and holding the mouse, then try to move around and jump with the arrow keys (the circle objects seem to bounce when they are not supposed to, gravity does not apply to them when certain keys are pressed, and since the main character is stationary it seems to cause unrealistic collisions with the character and the circle objects). Do I need to make an array that holds boolean values telling me which circles are touching the ground and which aren't; then apply gravity to those which are not on the ground and no gravity to those which are? Please help me as I have been working on this forever, I will appreciate any and all feedback.

PRESS PLAY FULLSCREEN:
My Game Engine

Relevant Code & Explanations:
//Everytime I do the for() loops I am going through all box2d circle objects.
//CurrentLevel is the whole level made of a group of box2d box objects.
//gravity = 9.8
//The onStep Function is Triggered Every Box2d Frame (and in extension so is the keyControls function).

Code: Select all

private function onStep(e:Event):void
      {
         
         //APPLY FRICTION TO X-VELOCITY
         currentLevel.body.SetLinearVelocity(new b2Vec2(currentLevel.body.GetLinearVelocity().x * 0.9, currentLevel.body.GetLinearVelocity().y) );
         for (var i:int = 0; i < circleArray.length; i++) {
            circleArray[i].body.SetLinearVelocity(new b2Vec2(circleArray[i].body.GetLinearVelocity().x * 0.9, circleArray[i].body.GetLinearVelocity().y) );
         }
         
         //SET X-VELOCITY TO ZERO IF TOO LOW
         if (Math.abs( currentLevel.body.GetLinearVelocity().x ) < 0.05) {
            currentLevel.body.SetLinearVelocity(new b2Vec2(0, currentLevel.body.GetLinearVelocity().y));
            for (var i:int = 0; i < circleArray.length; i++) {
            circleArray[i].body.SetLinearVelocity(new b2Vec2(0, circleArray[i].body.GetLinearVelocity().y));
            }
         }
         
         //LIMIT LEVEL Y-VELOCITY
         if (levelVelocity.y > 6.6) {
            levelVelocity.y = 6.6;
         }

         //JUMPING = TRUE IF FEET ARE TOUCHING THE LEVEL
         if(jumping) {
            levelVelocity.y -=  0.4;
              //APPLY GRAVITY THROUGH DECREASING VELOCITY
            currentLevel.body.SetLinearVelocity(new b2Vec2(0,levelVelocity.y*1) );
            for (var i:int = 0; i < circleArray.length; i++) {
            circleArray[i].body.SetLinearVelocity(new b2Vec2(circleArray[i].body.GetLinearVelocity().x, currentLevel.body.GetLinearVelocity().y) );
            }
         } else {
            levelVelocity.y = 0;
              //APPLY GRAVITY TO CIRCLES AS A FORCE IF FEET ARE TOUCHING THE GROUND
            for (var i:int = 0; i < circleArray.length; i++) {
            circleArray[i].body.ApplyForce(new b2Vec2(0, circleArray[i].body.GetMass() * gravity * 1), circleArray[i].body.GetWorldCenter() );
            }
         }
                       
        //More Keyboard and Movement Logic
         keyControls();
      }
      
      //canGoRight = true if the character's rightside is not touching the level, same for canGoLeft.
      private function keyControls():void
      {
            if (Input.kd("A", "LEFT")) {
               this.scaleX = -1;
               levelVelocity.x = levelVelocity.x > _max - 0.5 ? _max : levelVelocity.x + 0.5;
               if (LevelLoader.canGoLeft) {
                  currentLevel.body.SetLinearVelocity(new b2Vec2(levelVelocity.x, currentLevel.body.GetLinearVelocity().y));
                  for (var i:int = 0; i < circleArray.length; i++) {
                     circleArray[i].body.SetLinearVelocity(new b2Vec2(levelVelocity.x, circleArray[i].body.GetLinearVelocity().y));
                  }
               }
            }
            if (Input.kd("D", "RIGHT")) {
               this.scaleX = 1;
               levelVelocity.x = levelVelocity.x < 0.5 - _max ? _max * -1 : levelVelocity.x - 0.5;
               if(LevelLoader.canGoRight) {
                  currentLevel.body.SetLinearVelocity(new b2Vec2(levelVelocity.x, currentLevel.body.GetLinearVelocity().y));
                  for (var i:int = 0; i < circleArray.length; i++) {
                     circleArray[i].body.SetLinearVelocity(new b2Vec2(levelVelocity.x, circleArray[i].body.GetLinearVelocity().y));
                  }
               }
            }
            
            if (!Input.kd("A", "LEFT", "D", "RIGHT")) {
               if (levelVelocity.x > 0.5) {
                  levelVelocity.x = levelVelocity.x < 0.5 ? 0 : levelVelocity.x - 0.5;
               } else {
                  levelVelocity.x = levelVelocity.x > -0.5 ? 0 : levelVelocity.x + 0.5;
               }
            }
            
//IF UP KEY IS PRESSED APPLY EQUAL UPWARD VELOCITY TO BOTH THE LEVEL AND THE CIRCLES
            if (Input.kd("W", "UP")) {
               if (!jumping) {
               levelVelocity.y = 50;
               currentLevel.body.SetLinearVelocity(new b2Vec2(0, levelVelocity.y) );
               for (var i:int = 0; i < circleArray.length; i++) {
                  circleArray[i].body.SetLinearVelocity(new b2Vec2(circleArray[i].body.GetLinearVelocity().x, currentLevel.body.GetLinearVelocity().y) );
               }
               
               levelVelocity.y -=  0.4;
               jumping = true;
               }
            }
            
            if (Input.kd("S", "DOWN")) {
               //nothing here yet
            }

      }



-Thank You

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Sidescrolling Platformer Object and Level Movement Issue

Postby jayther » Thu Jun 21, 2012 5:24 pm

You shouldn't move the level itself to facilitate a pseudo "camera" movement. You should simply move the "camera" by shifting the sprite in which the graphics are being drawn on. If it's being drawn directly onto the stage, add a container sprite and move the sprite around instead of moving the level itself.

orangatang
Posts: 5
Joined: Tue Jun 19, 2012 3:40 pm

Re: Sidescrolling Platformer Object and Level Movement Issue

Postby orangatang » Tue Jun 17, 2014 3:19 am

Thank you, I took me too long to finally figure this out. :D


Return to “Flash”



Who is online

Users browsing this forum: No registered users and 6 guests