3 Replies - 355 Views - Last Post: 19 August 2013 - 06:48 PM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Awful movement

Posted 14 August 2013 - 04:41 AM

I am creating 2D game where user can play with kangaroo. It is something like mario game. When kangaroo walks, it works pretty nicely. But when in one time I want to jump and walk. The movement starts to shake. How can I fix that? Do i need to make more time between moving.?(Now it is 3pixels in 100 miliseconds)
Is This A Good Question/Topic? 0
  • +

Replies To: Awful movement

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 999
  • View blog
  • Posts: 4,177
  • Joined: 14-February 08

Re: Awful movement

Posted 14 August 2013 - 05:10 AM

Seeing some code would be nice :)
Was This Post Helpful? 0
  • +
  • -

#3 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: Awful movement

Posted 14 August 2013 - 05:40 AM

	/* Controls users input */
	@Override
	public void keyPressed(KeyEvent e) {
		
		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
			moveRight();
		}else if(e.getKeyCode() == KeyEvent.VK_LEFT){
			moveLeft();
		}else if(e.getKeyCode() == KeyEvent.VK_SPACE){
			if(!alreadyInAir)
				jump();
		}
		repaint();
	}
	/* Moves player right direction. */
	private void moveRight(){
		if(!player.isGoingRight()){
			player.goingRight();
		}
		if(isPossibleToMoveRight()){
			player.moveRight(KANGAROO_SPEED);
		}
		if(!alreadyInAir && isFalling()){
			fall();
		}
	}

       	/* Falls method. */
	private void fall(){
		fallingTimer = new Timer(MOVEMENT_MILISECONDS,this);
		fallingTimer.start();
	}
	
	/* Moves player left direction */
	private void moveLeft(){
		if(player.isGoingRight()){
			player.goingLeft();
		}
		if(isPossibleToMoveLeft()){
			player.moveLeft(KANGAROO_SPEED);
		}
		if(!alreadyInAir && isFalling()){
			fall();
		}
	}
	
	/* This method performs a jump */
	private void jump(){
		player.setPosY(player.getPosY() + player.getJumpPower());
		player.setJumpPower(player.getJumpPower() + GRAVITY);
		alreadyInAir = true;
		jumpingTimer = new Timer(MOVEMENT_MILISECONDS,this);
		jumpingTimer.start();
	}

	/* Controls timers */
	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getSource() == fallingTimer){
			if(!isCollidingWithFeets()){
				stillFall();
			}else{
				stopFalling();
			}
		}
		if(ae.getSource() == jumpingTimer){
			if(collisionWithHead()){
				fall();
				recoverAfterJumping();
			}
			if(!isCollidingWithFeets()){
				stillJump();
			}else{
				recoverAfterJumping();
				alreadyInAir = false;
			}
		}
	}

	/* Actual jumping */
	private void stillJump(){
		player.setPosY(player.getPosY() + player.getJumpPower());
		player.setJumpPower(player.getJumpPower() + GRAVITY);
		alreadyInAir = true;
		repaint();
	}
	
	/* Recovers after jumping */
	private void recoverAfterJumping(){
		jumpingTimer.stop();
		player.setJumpPower(-15);
	}
	
	/* Performs actual falling */
	private void stillFall(){
		if(gravityPower <= MAX_GRAVITY){
			gravityPower+= GRAVITY;
		}
		player.setPosY(player.getPosY() + gravityPower);
		alreadyInAir = true;
		repaint();
	}
	
	/* Stops falling */
	private void stopFalling(){
		fallingTimer.stop();
		gravityPower = 0;
		alreadyInAir = false;
	}



I hope this is what u need. I think it is better not to copy the whole class.
Was This Post Helpful? 0
  • +
  • -

#4 slehmann101  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 146
  • Joined: 30-November 11

Re: Awful movement

Posted 19 August 2013 - 06:48 PM

is this framerate related?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1