Jump to content

  • Log In with Google      Sign In   
  • Create Account

rubsnick

Member Since 14 Aug 2010
Offline Last Active Sep 03 2013 08:58 AM

Posts I've Made

In Topic: How can I get my character to come back down even if the user still has the b...

19 December 2012 - 07:20 PM

Thanks everyone for your responses I've been working on it for a while and I just cannot seem to get past my problem. He stays up in the air regardless of what I do. :/ Here is my code What the hell am I doing wrong...


[source lang="java"] if(Gdx.input.isKeyPressed(Keys.SPACE)&&man.y<=150&&AllowJump==true) { stateTime += Gdx.graphics.getDeltaTime(); // #15 currentFrame = walkAnimation.getKeyFrame(stateTime, true); man.y += 450 * Gdx.graphics.getDeltaTime(); if(man.y>150) { AllowJump=false; } }//This is my Release if(!Gdx.input.isKeyPressed(Keys.SPACE)) { if(man.y>=50) { man.y -= 350 * Gdx.graphics.getDeltaTime(); } if(man.y<=50) { AllowJump=true; } }[/source]

In Topic: How can I get my character to come back down even if the user still has the b...

10 December 2012 - 03:40 PM

You could create a bool where jumping only works if it's false. Set it to true once they reach the 50 pixel limit, and then reset it back to false when they're touching the ground again. Does that make sense?


Yes it makes sense, that would actually work quite nicely. I have no idea why I didn't think of it any sooner.

In Topic: How can I get my character to come back down even if the user still has the b...

10 December 2012 - 03:00 PM

You might have to use some math. It won't be as complex as a physics engine, so the code will still be efficient.

Basically, define a number of pixels per frame for gravity, like -20. Then apply this to your character's y-position each frame, clamping to the current height of the platform he is standing on (so he doesn't sink through the ground).

Each time they press jump button add a positive number of pixels per frame to counteract gravity. Like +30 per jump button press. Manage the height of the character so it doesn't go over +50 pixels above the ground and you've got it!


That's exactly how I emplemented it. but If I keep the button pressed he never goes down. My gravity get cancelled out.

In Topic: Need some help with my Code here, I want to add functionalities to the button...

02 April 2012 - 07:06 AM

http://pastebin.com/i4pG9N5b

Alright that's my new Code, I managed to learn some stuff and now whenever I click it it changes background, It's pretty awesome... Now I"m over the hurdle and no longer need help. Thanks man tough.

In Topic: Trying to do ball to ball collicions.... and it's not working.

21 February 2012 - 12:31 PM

Hey thanks everyone... I'm still having difficulty. I"m understanding the theory behind it no problem. I just don't know how to update it... or Don't understand why it's not working. :/ I'm going to post my entire code everything works and I'm just kinda tired after trying to quite some time... Anyone have any ideas? Here is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Breakout
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public partial class Game1 : Microsoft.Xna.Framework.Game
    {
	    GraphicsDeviceManager graphics;
	    SpriteBatch spriteBatch;
	    Texture2D paddle;
	    Vector2 paddlePosition = Vector2.Zero;
	    Texture2D ball1;
	    Vector2 ball1Position = Vector2.Zero;
	    Texture2D ball2;
	    Vector2 ball2Position = Vector2.Zero;
	    Boolean movingUp, movingLeft, movingUp2,movingLeft2;
	    public Game1()
	    {
		    graphics = new GraphicsDeviceManager(this);
		    Content.RootDirectory = "Content";
	    }
	   
	    protected override void Initialize()
	    {
  
		    base.Initialize();
	    }
	    protected override void LoadContent()
	    {
		    movingUp = true;
		    movingLeft = true;
		    movingUp2 = true;
		    movingLeft2 = true;

		    spriteBatch = new SpriteBatch(GraphicsDevice);
		    paddle = Content.Load<Texture2D>("Paddle");
		    paddlePosition= new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(paddle.Width/2), (graphics.GraphicsDevice.Viewport.Height-80));
		   
		    ball1 = Content.Load<Texture2D>("ball");
		    ball1Position=new Vector2((graphics.GraphicsDevice.Viewport.Width/2)-(ball1.Width/2), graphics.GraphicsDevice.Viewport.Height-115);
		   
		    ball2 = Content.Load<Texture2D>("ball");
		    ball2Position = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - ((ball2.Width / 2)+250), graphics.GraphicsDevice.Viewport.Height - 115);
	    }

	    protected override void UnloadContent()
	    {
	  
	    }

	    protected override void Update(GameTime gameTime)
	    {
		   
		    PaddleMover();
		    Ball1mover();
		    Ball2mover();
		    BalltoBallCollision();
		 
		    base.Update(gameTime);
	    }
	 
	    protected override void Draw(GameTime gameTime)
	    {
		    GraphicsDevice.Clear(Color.CornflowerBlue);
		    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
		    spriteBatch.Draw(paddle, paddlePosition, Color.White);
		    spriteBatch.Draw(ball1, ball1Position, Color.White);
		    spriteBatch.Draw(ball2, ball2Position, Color.White);
		    spriteBatch.End();
		    base.Draw(gameTime);
	    }
	

    }
}

Here is where all of the important stuff happends (movement etc) it's in the other partial class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Breakout
{
    public partial class Game1
    {
	    public void PaddleMover()
	    {
		    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
			    this.Exit();
		    if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && paddlePosition.X >= 0)
		    {
			    paddlePosition.X -= 15;
		    }
		    if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && paddlePosition.X <= (graphics.GraphicsDevice.Viewport.Width - 150))
		    {
			    paddlePosition.X += 15;
		    }
	    }
	    public void Ball1mover()
	    {
		    if (movingUp)
		    {
			    ball1Position.Y -= 3;
		    }
		    else
		    {
			    ball1Position.Y += 3;
		    }
		    if (movingLeft)
		    {
			    ball1Position.X -= 3;
		    }
		    else
		    {
			    ball1Position.X += 3;
		    }
		    if (ball1Position.X <= 0 && movingLeft) movingLeft = false;
		    if (ball1Position.Y <= 0 && movingUp) movingUp = false;
		    if (ball1Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft) movingLeft = true;
		    if (DetectPaddleBallCollision())
		    {
			    movingUp = true;
		    }
	    }
	    public void Ball2mover()
	    {
		    if (movingUp2)
		    {
			    ball2Position.Y -= 3;
		    }
		    else
		    {
			    ball2Position.Y += 3;
		    }
		    if (movingLeft2)
		    {
			    ball2Position.X -= 3;
		    }
		    else
		    {
			    ball2Position.X += 3;
		    }

		    if (ball2Position.X <= 0 && movingLeft2) movingLeft2 = false;
		    if (ball2Position.Y <= 0 && movingUp2) movingUp2 = false;
		    if (ball2Position.X >= (graphics.GraphicsDevice.Viewport.Width - ball1.Width) && !movingLeft2) movingLeft2 = true;
		    if (DetectPaddleBallCollision2())
		    {
			    movingUp2 = true;
		    }
	    }
	    public Boolean DetectPaddleBallCollision()
	    {
		    if ((ball1Position.Y + ball1.Height) >= paddlePosition.Y &&
			    (ball1Position.Y + ball1.Height) < (paddlePosition.Y + 4) &&
			    (ball1Position.X + ball1.Width) > paddlePosition.X &&
			    ball1Position.X < (paddlePosition.X + paddle.Width))
		    {
			    return true;
		    }
		    else
		    {
			    return false;
		    }
	    }
	    public Boolean DetectPaddleBallCollision2()
	    {
		    if ((ball2Position.Y + ball2.Height) >= paddlePosition.Y &&
			    (ball2Position.Y + ball2.Height) < (paddlePosition.Y + 4) &&
			    (ball2Position.X + ball2.Width) > paddlePosition.X &&
			    ball2Position.X < (paddlePosition.X + paddle.Width))
		    {
			    return true;
		    }
		    else
		    {
			    return false;
		    }
	    }
	    public void BalltoBallCollision()
	    {
		    //Controlls ball 1 movement
		    if (ball1Position.Y == ball2Position.Y && ball1Position.X == ball2Position.X)
		    {
			    ball2Position.Y = -ball2Position.Y;
			    ball1Position.Y = -ball1Position.Y;
			 
		    }

		  
	    }
    }
}

Any help would be appreciated.... I obviousl have no idea what I"m doing. :(

PARTNERS