7 Replies - 3809 Views - Last Post: 05 August 2012 - 06:05 PM

#1 miguel71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-September 11

Question - XNA View like super mario.

Posted 04 August 2012 - 08:05 AM

I'm making a game where there is a large map(much bigger than the screen size). Can anyone help me making something so when i moved the character it would move the part of the map that is visible, just like mario.

I had an ideia that when i moved the player the other stuff on the map would move for the oposite direction instead of moving the player, but i would prefer a better way of doing that.
Is This A Good Question/Topic? 0
  • +

Replies To: Question - XNA View like super mario.

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Question - XNA View like super mario.

Posted 04 August 2012 - 08:46 AM

Assuming you're using a Vector2 for Position in your Player class, and have an instance called playerCharacter, and you apply a momentum to it to move it around, you might do this:

public void MoveCharacter(Vector2 momentum)
        {
//Create a "frame" in which the player will move; otherwise, the map will move.
            Rectangle frame = new Rectangle(10, 10, graphics.PreferredBackBufferWidth-20, graphics.PreferredBackBufferHeight-20);
//Create a Point showing where the player would be
            Point futurePosition = new Point(playerCharacter.Position.X + momentum.X, playerCharacter.Position.Y + momentum.Y);
//Checks if the futurePosition is in the frame. If so, the player moves. If not, the map moves.
            if(frame.Contains(futurePosition))
                playerCharacter.Position += momentum;
            else
                levelMap.Position += momentum;
        }


This post has been edited by MrShoes: 04 August 2012 - 11:52 AM

Was This Post Helpful? 2
  • +
  • -

#3 miguel71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-September 11

Re: Question - XNA View like super mario.

Posted 04 August 2012 - 05:32 PM

Well thanks for the reply, but... I'm not sure how to implement that to my code. First i tried to add it to the game1.cs class, i changed "playerCharacter" to my player object( that is called "player") and it gave an error. Second what do i replace "levelMap" with? And finaly when do i need to call that method?
Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Question - XNA View like super mario.

Posted 05 August 2012 - 02:26 AM

Well, that's sample code, since I don't know the structure of your project. You don't need to use that exactly.

Quote

i changed "playerCharacter" to my player object( that is called "player") and it gave an error.


What was the error? Does your Player class have a Vector2 named position? Most XNA Developers use Vector2 for position in 2D games, for 2 reasons: 1) The SpriteBatch.Draw() method uses a Vector2 as a parameter for where to draw the sprite. 2) You can apply another Vector2 (such as momentum, as shown in the example) to affect the Player.

Quote

Second what do i replace "levelMap" with?


"levelMap" is an object of the Map class. This was another assumption, that you had a class for your maps. If you don't, make one. You have the power of OOP at your command. For me, good practice in XNA is to have absolutely everything in classes, and only use your Game.cs class to call the .Update() and .Draw() methods of existing classes. You might also add some game logic methods in here, but I would put them elsewhere.

Quote

And finaly when do i need to call that method?


The method is ideally called in the .Update() stage. I say, give every object a .Draw() and .Update() method, and call them in the Game.Draw() and Game.Update() methods. Perhaps the MovePlayer method should be in the Player class, and be called in the .Update() method, passing in the momentum to be applied (a vector of 0,0 if no movement is to be applied). Really this is a matter of preference.
Was This Post Helpful? 0
  • +
  • -

#5 miguel71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-September 11

Re: Question - XNA View like super mario.

Posted 05 August 2012 - 06:12 AM

Yes, i am using a Vector2 called Position, for that error i chaged all "playerCharacter" to "player" and i got this error(as shown in this picture) http://screensnapr.com/v/j8hHHB.png .

How should i create an object of levelMap. Should i add there the level objects(like enemies, blocks, etc...), draw them from there and the positions of them draw relatively to the levelMap object?

And finaly thanks for the help, i'm a bit noob in XNA, i already programed games but not in c#. :D
Was This Post Helpful? 0
  • +
  • -

#6 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Question - XNA View like super mario.

Posted 05 August 2012 - 08:09 AM

My apologies, you'll have to cast the results to int as Vector2 is made up of floats.

The rest of your question is big... I think you probably need to follow some guided tutorials before attempting your own game. I'm out at the moment, but I'll tackle the essence of it later.
Was This Post Helpful? 0
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: Question - XNA View like super mario.

Posted 05 August 2012 - 10:59 AM

OK, so what I'm basically talking about is this:

First, you need a Player class. As well as the usual things like a Texture2D for the sprite, int for health etc, you need a Vector2 named Position. Let's say we also have a Vector2 named Momentum, which is affected by the keyboard/joystick input from the player, as well as effects like gravity, wind etc. The Player class has a Draw() method to draw the sprite at Position. The Player class also has an Update() method where the changes to the Player occur; in this method, call a method like the MoveCharacter() method given earlier (although momentum doesn't need to be passed as a parameter as it's part of the same class).

Now, we need a Map class. This might also have a Texture2D for the Background. As you rightly mentioned, this class would hold a collection of all the objects (enemies, hazards etc.) and you might also have collections of Tile objects if you're using a tiled level. Anyway, say it also has a relative Position Vector2. You'll notice the Player.MoveCharacter() method will move the map but, really, you'd probably want a method that moves ALL the items within the Map's collections.

I hope that makes sense. In reality, there are many ways to do this. You might even have a Camera class that tracks the Player and renders him correctly on the screen, moving the Map around as needed. It's totally your call.
Was This Post Helpful? 1
  • +
  • -

#8 miguel71  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 21-September 11

Re: Question - XNA View like super mario.

Posted 05 August 2012 - 06:05 PM

Thanks for all the help that you gave :) . It is now working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1