Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Follow player  (Read 2162 times)
0 Members and 1 Guest are viewing this topic.
Offline N1els5

Senior Newbie





« Posted 2012-08-27 19:50:39 »

Hi,

Again a question from me. Ive made a level from 2500*2500. But now I have a problem, Ive searched at google but didnt found a good explain. How can I make that the camera is following the player? (I dont want moving the whole level)

Thank you in advance!

N1els5
Offline jonjava
« Reply #1 - Posted 2012-08-27 20:00:07 »

By not wanting to move the whole level - what do you mean?

You're going to have to draw things at different places. Easiest is probably to just draw everything at an offset and have the offset determined either by the player or another camera object that solely exist to follow the player around.

1  
2  
3  
public void render(Graphics g, int hoff, int voff) {
   g.drawImage(sprite, x - hoff, y - voff);
}


Then by changing the hoff and voff values you can have the whole map shift in whatever direction you place. To follow  player for example.

Offline N1els5

Senior Newbie





« Reply #2 - Posted 2012-08-27 21:23:10 »

By not wanting to move the whole level - what do you mean?

You're going to have to draw things at different places. Easiest is probably to just draw everything at an offset and have the offset determined either by the player or another camera object that solely exist to follow the player around.

1  
2  
3  
public void render(Graphics g, int hoff, int voff) {
   g.drawImage(sprite, x - hoff, y - voff);
}


Then by changing the hoff and voff values you can have the whole map shift in whatever direction you place. To follow  player for example.
A good example what I want is the same as a mario level. But I dont want move the whole level only the player and the camera.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava
« Reply #3 - Posted 2012-08-27 21:27:49 »

The map won't move anywhere - it's just getting drawn at a different place in reference to the visible screen area.

Offline N1els5

Senior Newbie





« Reply #4 - Posted 2012-08-27 21:31:39 »

The map won't move anywhere - it's just getting drawn at a different place in reference to the visible screen area.
Ok, and whats the best option to do this? Somebody on google say with JPanels?
Offline ReBirth
« Reply #5 - Posted 2012-08-28 03:31:05 »

Play with the map's offsets, since you dont draw all visual to one window.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2012-08-28 04:03:21 »

I don't get why you would use JPanels, unless your map was in a JPanel but the player wasn't...and that's bad design anways...
Offline Quadro

Senior Newbie





« Reply #7 - Posted 2012-09-06 03:28:39 »

Use the translate method in Graphics2D

g.translate(x, y)

whenever you increase X of the player, you'll want to decrease the x of whatever variable you use in the translate method, the same goes for Y
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (12 views)
2014-08-01 22:53:16

CogWheelz (14 views)
2014-08-01 22:51:43

CopyableCougar4 (15 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!