-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScroller.java
More file actions
60 lines (54 loc) · 1.48 KB
/
Scroller.java
File metadata and controls
60 lines (54 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This class is responsible for scrolling the background and hold information about how much we've scrolled.
* <p>
* Class inspired by https://www.greenfoot.org/scenarios/18226
*
* @author Roșca Paul-Teodor
* @version 1.0 (22/12/2020)
*/
public class Scroller extends Actor
{
/**
* The world which we scroll
*/
private World world;
/**
* The number of pixels we've scrolled on each axis (relative to the original world)
*/
private int scrolledX,scrolledY;
/**
* Constructor for our scroller.
*
* @param myWorld the world which we will scroll
*/
public Scroller(World myWorld)
{
world=myWorld;
}
/**
* Method for scrolling the world.
*
* @param dx the number of pixels we scroll on X axis
* @param dy the number of pixels we scroll on Y axis
*/
public void scroll(int dx,int dy)
{
// We update the ammout of pixels we've scrolled on each axis
scrolledX+=dx;
scrolledY+=dy;
for (Object obj : world.getObjects(null))
{
Actor actor = (Actor) obj;
actor.setLocation(actor.getX()+dx, actor.getY()+dy);// We move all the actors in the world by how much we're scrolling
}
}
public int getScrolledX()
{
return scrolledX;
}
public int getScrolledY()
{
return scrolledY;
}
}