Hello and I'm trying to make my own game engine in Java. I have completed all the necessary ones but I can't figure it out with the TileGame class. It just can't scroll. Also there are no exceptions. Here I'm listing the code.
TileGame.java
@Override
public void draw(Graphics2D g) {
if (back!=null){
back.render(g);
}
if (follower!=null){
follower.render(g);
follower.draw(g);
}
for (int i=0; i
Actor actor = actors.get(i);
if (actor!=follower&&getVisibleRect().intersects(actor.getBounds())){
g.drawImage(actor.getAnimation().getFrameImage(), actor.x - OffSetX, actor.y - OffSetY, null);
actor.draw(g);
}
}
}
/**
* This method returns the visible rectangle
* @return The visible rectangle
*/
public Rectangle getVisibleRect(){
return new Rectangle(OffSetX, OffSetY, global.WIDTH, global.HEIGHT);
}
@Override
public void update(){
if (follower!=null){
if (scrollHorizontally){
OffSetX = global.WIDTH/2 - Math.round((float)follower.x) - tileSize;
OffSetX = Math.min(OffSetX, 0);
OffSetX = Math.max(OffSetX, global.WIDTH - mapWidth);
}
if (scrollVertically){
OffSetY = global.HEIGHT/2 - Math.round((float)follower.y) - tileSize;
OffSetY = Math.min(OffSetY, 0);
OffSetY = Math.max(OffSetY, global.HEIGHT - mapHeight);
}
}
for (int i=0; i Actor actor1 = actors.get(i);
if (getVisibleRect().contains(actor1.x, actor1.y)){
actor1.update();
for (int j=0; j Actor actor2 = actors.get(j);
if (actor1.isCollidingWith(actor2)){
actor1.collision(actor2);
actor2.collision(actor1);
}
}
}
}
}
but the problem is that all the actors are working, but it just won't scroll. Help Please.. Thanks in Advance.
Answer
Hopefully you have solved this yourself by now, if not here is some help to get you there.
That program you're using to type all your code in to? It's not just for typing code in and pressing "play". It's an IDE (Integrated Development Environment). This means it contains many tools to help you develop, one of those tools is the debugger. I suggest you read more about debuggers here.
How do you use the debugger?
It's going to depend on your IDE of choice (they likely contain very similar debuggers, but just go with it). For Java there are a few major ones: Eclipse, Netbeans and more. Regardless they all have some similar features. One of the most powerful features is breaking, breaking can happen automatically from an exception (as I'm sure you've seen), or it can happen manually via the use of breakpoints. For situations like yours, breakpoints are your best friend. They will allow you to stop execution of your code at a specific point, and examine the state of your program at that time.
How do you use breakpoints?
Again, this is IDE specific. Commonly it's a right click off to the left side column of the code window and selecting "Enable breakpoint". Yes, it's that easy. After setting a breakpoint, you'll start your program in debug mode. This mode lets the IDE know that it should pay attention to breakpoints and other debug features. Now, when the code execution gets to the point in your code where you set your breakpoint, it stops, and turns control over to you. This is awesome. You can now view what variable values are, many IDEs let you execute code snippets to see their result against the current state of your program, change values and more! This is SO powerful. Breakpoints rock.
Now that your program execution has stopped, it's not dead. It's waiting for you to tell it what to do next. Again, you have some powerful options. You can continue execution as normal, you can step into functions, step out of functions or step over line by line. Amazing. It's often useful to set a breakpoint at the beginning of a function, then step through the code line by line, seeing the variables change and following the execution of the code. The line by line stepping is by far the coolest way to follow the execution of your program.
Now, I hope you've read the above, followed some of the links, and looked up how to perform the actions described using your IDE of choice. If you've gotten this far, I'll offer you a further hint. Set a breakpoint on this line: if (follower!=null){
check your values. Is follower null
? How about scrollHorizontally
and scrollVertically
, are they both true
? Now, step line by line, watch how your OffsetX
and OffsetY
values change as you change them around, around in a full circle ;)
No comments:
Post a Comment